Java class One-to-many, many-to-many

Source: Internet
Author: User

1. One-to-many

classperson{PrivateString name; PrivateString Phone; Privatecar[] cars;  PublicPerson () {} PublicPerson (String name,string phone) { This. Name =name;  This. Phone =phone; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getphone () {returnphone; }     Public voidSetphone (String phone) { This. Phone =phone; }     Publiccar[] Getcars () {returncars; }     Public voidSetcars (car[] cars) { This. Cars =cars; }     PublicString GetInfo () {return"Name:" +name+ ", Phone:" +phone; }}classcar{PrivateString brand; PrivateString color; Privateperson person ;  PublicCar () {} PublicCar (String brand,string color) { This. Brand =brand;  This. color =color; }     PublicString Getbrand () {returnbrand; }     Public voidSetphand (String brand) { This. Brand =brand; }     PublicString GetColor () {returncolor; }     Public voidsetcolor (String color) { This. color =color; }     PublicPerson Getperson () {returnPerson ; }     Public voidSetperson (person person) { This. person =Person ; }     PublicString GetInfo () {return"Brand:" +brand+ ", Color:" +color; }}classlei{ Public Static voidMain (string[] args) {person P1=NewPerson ("Lie Triple", "05333-6543887"); Person P2=NewPerson ("Zhao Si", "05333-6543000"); Car C1=NewCar ("Audi", "white"); Car C2=NewCar ("VW", "BLACK"); Car C3=NewCar ("Geely", "Red"); P1.setcars (Newcar[]{c1,c2}); P2.setcars (Newcar[]{c3});        C1.setperson (p1);        C2.setperson (p1);        C3.setperson (p2); System.out.println (P1.getinfo ()+ ", Car and car color:");  for(inti = 0;i < P1.getcars (). length;i++) {System.out.println (P1.getcars () [I].getinfo ());    } System.out.println (C1.getperson (). GetInfo ()); }}

2. Many-to-many

classshop{PrivateString name; PrivateString City; Privategoods[] Goods;  PublicShop () {} PublicShop (String name,string city) { This. Name =name;  This. City =City ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }     Publicgoods[] Getgoods () {returngoods; }     Public voidsetgoods (goods[] Goods) { This. Goods =goods; }     PublicString GetInfo () {return"Arcade:" +name+ ", Area:" +City ; }}classgoods{PrivateString name; Private DoublePrice ; Privateshop[] shops;  PublicGoods () {} PublicGoods (String name,DoublePrice ) {         This. Name =name;  This. Price =Price ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public DoubleGetPrice () {returnPrice ; }     Public voidSetprice (DoublePrice ) {         This. Price =Price ; }     Publicshop[] Getshop () {returnshops; }     Public voidSetshop (shop[] shops) { This. Shops =shops; }     PublicString GetInfo () {return"Product Name:" +name+ ", Price:" +Price ; }}classzio{ Public Static voidMain (string[] args) {shop S1=NewShop ("Carrefour", "Qingdao"); Shop S2=NewShop ("Big Run Hair", "Zibo"); Shop S3=NewShop ("Family Fun", "Sunshine"); Goods G1=NewGoods ("shirts", 189.5); Goods G2=NewGoods ("Red wine", 489.99); Goods G3=NewGoods ("Huiyuan beverage", 12.0); //a shopping mall can sell a variety of goodsS1.setgoods (Newgoods[]{g1,g2,g3}); S2.setgoods (Newgoods[]{g2,g3}); S3.setgoods (Newgoods[]{g1,g3}); //a product can be sold in multiple storesG1.setshop (NewSHOP[]{S1,S3}); G2.setshop (Newshop[]{s1,s2}); G3.setshop (NewSHOP[]{S1,S2,S3}); System.out.println (S1.getinfo ()+ "Goods and Prices sold:");//S1 Object Call GetInfo () method         for(inti = 0;i < S1.getgoods (). length;i++) {//Array TraversalSystem.out.println (S1.getgoods () [I].getinfo ());        } System.out.println (); System.out.println (G3.getinfo ()+ "Location of the Mall and City:");//G3 Object Call GetInfo () method         for(inti = 0;i < G3.getshop (). length;i++) {//Array TraversalSystem.out.println (G3.getshop () [I].getinfo ()); }    }}

Java class One-to-many, many-to-many

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.