• java类一对多,多对多


    1.一对多

    class Person{
        private String name;
        private String phone;
        private Car[] cars;
        public Person(){}
        public Person(String name,String phone){
            this.name = name;
            this.phone = phone;
        }
        public String getName(){
            return name;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getPhone(){
            return phone;
        }
        public void setPhone(String phone){
            this.phone = phone;
        }
        public Car[] getCars(){
            return cars;
        }
        public void setCars(Car[] cars){
            this.cars = cars;
        }
        public String getInfo(){
            return "姓名:"+name+",电话:"+phone;
        }
    }
    class Car{
        private String brand;
        private String color;
        private Person person;
        public Car(){}
        public Car(String brand,String color){
            this.brand = brand;
            this.color = color;
        }
        public String getBrand(){
            return brand;
        }
        public void setPhand(String brand){
            this.brand = brand;
        }
        public String getColor(){
            return color;
        }
        public void setColor(String color){
            this.color = color;
        }
        public Person getPerson(){
            return person;
        }
        public void setPerson(Person person){
            this.person = person;
        }
        public String getInfo(){
            return "品牌:"+brand+",颜色:"+color;
        }
    }
    
    class Lei{
        public static void main(String[] args){    
            Person p1 = new Person("李三","05333-6543887");
            Person p2 = new Person("赵四","05333-6543000");
            Car c1 = new Car("奥迪","白色");
            Car c2 = new Car("大众","黑色");
            Car c3 = new Car("吉利","红色");
            p1.setCars(new Car[]{c1,c2});
            p2.setCars(new Car[]{c3});
            c1.setPerson(p1);
            c2.setPerson(p1);
            c3.setPerson(p2);
            System.out.println(p1.getInfo()+", 所拥有的汽车及汽车颜色:");
            
            for(int i = 0;i < p1.getCars().length;i++){
                System.out.println(p1.getCars()[i].getInfo());
            }
            
            System.out.println(c1.getPerson().getInfo());
    
        }
    }

    2.多对多

    class Shop
    {
        private String name;
        private String city;
        private Goods[] goods;
        public Shop(){}
        public Shop(String name,String city){
            this.name = name;
            this.city = city;
        }
        public String getName(){
            return name;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getCity(){
            return city;
        }
        public void setCity(String city){
         this.city = city;
        }
        public Goods[] getGoods(){
            return goods;
        }
        public void setGoods(Goods[] goods){
            this.goods = goods;
        }
        public String getInfo(){
            return "商场:"+name+", 区域:"+city;
        }
    }
    class Goods
    {
        private String name;
        private double price;
        private Shop[] shops;
        public Goods(){}
        public Goods(String name,double price){
            this.name = name;
            this.price = price;
        }
        public String getName(){
            return name;
        }
        public void setName(String name){
            this.name = name;
        }
        public double getPrice(){
            return price;
        }
        public void setPrice(double price){
            this.price = price;
        }
        public Shop[] getShop(){
            return shops;
        }
        public void setShop(Shop[] shops){
            this.shops = shops;
        }
        public String getInfo(){
            return "商品名:"+name+",价格:"+price;
        }
    }
    class  Zio
    {
        public static void main(String[] args) 
        {
            Shop s1 = new Shop("家乐福","青岛");
            Shop s2 = new Shop("大润发","淄博");
            Shop s3 = new Shop("家家乐","日照");
    
            Goods g1 = new Goods("衬衣",189.5);
            Goods g2 = new Goods("红酒",489.99);
            Goods g3 = new Goods("汇源饮料",12.0);
            
             //一个商场可以售卖多种商品
            s1.setGoods(new Goods[]{g1,g2,g3});
            s2.setGoods(new Goods[]{g2,g3});
            s3.setGoods(new Goods[]{g1,g3});
            
             //一种商品可以在多个商场售卖
            g1.setShop(new Shop[]{s1,s3});
            g2.setShop(new Shop[]{s1,s2});
            g3.setShop(new Shop[]{s1,s2,s3});
             
            System.out.println(s1.getInfo() +"所售商品及价格:"); //s1对象调用getInfo()方法
            for(int i = 0;i < s1.getGoods().length;i++){ //数组遍历
                System.out.println(s1.getGoods()[i].getInfo());
            }
            System.out.println();
            System.out.println(g3.getInfo()+"所在商场及城市:"); //g3对象调用getInfo()方法
            for(int i = 0;i < g3.getShop().length;i++){ //数组遍历
                System.out.println(g3.getShop()[i].getInfo());
            }
        }
    }
  • 相关阅读:
    微信公众号,点击菜单,跳转到小程序
    tomcat启动时启动窗口出现乱码的解决方案
    SpringBoot实现HandlerInterceptor拦截器的接口没有需要重写的方法也不报错是怎么回事
    ZooKeeper下载安装(Windows版本)
    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could
    mysql连接出现java.sql.sql exception:服务器时区值'''_''''无法识别或表示多个时区
    angular执行ng build进行压缩。
    angular新建项目时没有新建路由时,后来新建路由
    关于ng-zorro-antd阿里的UI框架
    angular 项目创建打包过程
  • 原文地址:https://www.cnblogs.com/lxy151/p/8137137.html
Copyright © 2020-2023  润新知