Java 简单类和数据的映射

    xiaoxiao2021-03-25  9

    #此代码包含:

    1.一对一的映射

    2.一对多的映射

    3.多对多的映射

     

     

    package yingshe; public class book { private String bookid; private String bookname; private String bookauthor; private double bookprice; private String bookpress; //出版社 private String bookkind; //类别 private Owners owner; //拥有者 private book[] books; private Owners[] owners; public Owners[] getOwners() { return owners; } public void setOwners(Owners[] owners) { this.owners = owners; } public book[] getBooks() { return books; } public void setBooks(book[] books) { this.books = books; } public Owners getOwner() { return owner; } public void setOwner(Owners owner) { this.owner = owner; } public book(String bookid,String bookname,String bookauthor,double bookprice,String bookpress,String bookkind) { // TODO Auto-generated constructor stub this.bookid=bookid; this.bookname=bookname; this.bookauthor=bookauthor; this.bookprice=bookprice; this.bookpress=bookpress; this.bookkind=bookkind; } public String getBookid() { return bookid; } public void setBookid(String bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getBookauthor() { return bookauthor; } public void setBookauthor(String bookauthor) { this.bookauthor = bookauthor; } public double getBookprice() { return bookprice; } public void setBookprice(double bookprice) { this.bookprice = bookprice; } public String getBookpress() { return bookpress; } public void setBookpress(String bookpress) { this.bookpress = bookpress; } public String getBookkind() { return bookkind; } public void setBookkind(String bookkind) { this.bookkind = bookkind; } public String getInfo(){ return "书的信息为:\n \t"+" id:"+this.bookid+", 书名:"+this.bookname+" ,作者:"+this.bookauthor+", 价格:"+this.bookprice+" ,出版社:"+this.bookpress+" ,类别:"+this.bookkind; } } package yingshe; public class Owners { private String ownerName; private String ownerId; private book book; private book [] books; private Owners [] owners; public Owners[] getOwners() { return owners; } public void setOwners(Owners[] owners) { this.owners = owners; } public book[] getBooks() { return books; } public void setBooks(book[] books) { this.books = books; } public Owners(String ownerName,String ownerId) { // TODO Auto-generated constructor stub this.ownerName=ownerName; this.ownerId=ownerId; } public String getOwnerName() { return ownerName; } public void setOwnerName(String ownerName) { this.ownerName = ownerName; } public String getOwnerId() { return ownerId; } public void setOwnerId(String ownerId) { this.ownerId = ownerId; } public book getBook() { return book; } public void setBook(book book) { this.book = book; } public String getInfo(){ return "拥有者的信息为:\n \t"+"拥有者的id:"+this.ownerId+",拥有者的名字:"+this.ownerName; } } package yingshe; public class bookDemo { public bookDemo() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub bookDemo bookDemo=new bookDemo(); System.out.println("一对一的映射:\n"); //bookDemo.yiduiyi(); System.out.println("一对多的映射:\n"); //bookDemo.yiduiduo(); System.out.println("多对多的映射:\n"); bookDemo.duoduiduo(); } public void yiduiyi(){ book book=new book("001", "java核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); Owners owners=new Owners("王源", "9527"); //System.out.println(book.getInfo());//输出书的信息 //System.out.println(owners.getInfo()); //输出拥有者的信息 book.setOwner(owners); //设置书的拥有者 owners.setBook(book); //设置拥有者的书 System.out.println("通过书的拥有者 输出拥有者的信息:\n"+book.getOwner().getInfo()); //通过书的拥有者 输出拥有者的信息 System.out.println("通过拥有者的书 输出书的信息:\n"+owners.getBook().getInfo());//通过拥有者的书 输出书的信息 } public void yiduiduo(){ book book=new book("001", "java核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book book1=new book("002", "c核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book book2=new book("003", "c++核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book books[]=new book[]{book,book1,book2}; //某人可能拥有这批书 Owners owners=new Owners("王源", "9527"); owners.setBooks(books); //设置三本书拥有者 book.setOwner(owners); book1.setOwner(owners); book2.setOwner(owners); System.out.println("通过拥有者找出他所拥有的书:"); for(int i=0;i<owners.getBooks().length;i++) { System.out.println();System.out.println(owners.getBooks()[i].getInfo()); } System.out.println("通过书找出拥有者的信息:\n"); for(int i=0;i<owners.getBooks().length;i++){ System.out.println(books[i].getOwner().getInfo()); } } public void duoduiduo(){ //定义六本书 book book=new book("001", "java核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book book1=new book("002", "c核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book book2=new book("003", "c++核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book book3=new book("004", "c#核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book book4=new book("005", "python核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book book5=new book("006", "linux核心技术", "沈雪冰", 69.8, "清华大学出版社","程序设计"); book bookss []=new book[]{book,book1,book2,book3,book4,book5}; book books[]=new book[]{book,book1,book2}; //某人可能拥有这批书 book books1[]=new book[]{book3,book4};//某人可能拥有这批书 book books2[]=new book[]{book2,book4};//某人可能拥有这批书 //定义三个拥有者 Owners owner=new Owners("王源", "9527"); Owners owner1=new Owners("李四", "9528"); Owners owner2=new Owners("张三", "9529"); Owners owners[]=new Owners[]{owner,owner1,owner2}; Owners owners1[]=new Owners[]{owner}; Owners owners2[]=new Owners[]{owner1,owner2}; owner.setBooks(books); //owner拥有books这批书 owner1.setBooks(books1);//owner1拥有books1这批书 owner2.setBooks(books2);//owner2拥有books2这批书 //设置每本书的多位拥有者 book.setOwners(owners); book1.setOwners(owners); book2.setOwners(owners2); book3.setOwners(owners1); book4.setOwners(owners1); //book5.setOwners(owners2); System.out.println("通过书找到书的拥有者:\n"); for(int i=0;i<bookss.length;i++) { try { if(bookss[i].getOwners()!=null) System.out.println(bookss[i].getInfo()+"\n 拥有者信息:\n"); for(int j=0;j<bookss[i].getOwners().length;j++) System.out.println(bookss[i].getOwners()[j].getInfo()); } catch (Exception e) { // TODO: handle exception System.out.println(bookss[i].getInfo()+"\n还没有拥有者哦~!"); } } System.out.println(); System.out.println("通过拥有者找到书的信息:\n"); for(int i=0;i<owners.length;i++) { System.out.println(owners[i].getInfo()+"\n 拥有的书的信息:"); for(int j=0;j<owners[i].getBooks().length;j++) System.out.println(owners[i].getBooks()[j].getInfo()); } } }

     

     

     

    一对一的映射结果:

    一对多的映射结果:

    多对多的映射结果:

     

    转载请注明原文地址: https://ju.6miu.com/read-200175.html

    最新回复(0)