class Person{private String name ;private int age ;private Book book;public Person(String name,int age){this.name=name;this.age=age;}public String getName(){return name;}public void setName(String name){this.name=name;}public int getAge(){return age;}public void setAge(int age){this.age=age;}public Book getBook(){return book;}public void setBook(Book book){this.book=book;}}class Book{private String title;private float price;private Person person;public Book(String title,float price){this.title=title;this.price=price;}public String getTitle(){return title;}public void setTitle(String title){this.title=title;}public float getPrice(){return price;}public void setPrice(float price){this.price=price;}public Person getPerson(){return person;}public void setPerson(Person person){this.person=person;}}public class RefDemo{public static void main(String[] args){Person per=new Person("张三",30);Book bk=new Book("java开发实战经典",59.0f);per.setBook(bk);bk.setPerson(per);System.out.println("从人找到书-->姓名:"+per.getName()+",年龄:"+per.getAge()+",书名:"+per.getBook().getTitle()+",价格:"+per.getBook().getPrice()+"元。");System.out.println("从书到人-->书名:"+bk.getTitle()+",价格:"+bk.getPrice()+"元,姓名:"+bk.getPerson().getName()+",年龄:"+bk.getPerson.getAge());}}
添加回答
举报
0/150
提交
取消