java類一對多,多對多
阿新 • • 發佈:2017-12-28
info sys stat strong ble inf static etc for
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()); } } }
java類一對多,多對多