/*
一个人有一个孩子,还有一本书,
*/
class RefDemo
{
public static void main(String[] args)
{
Person p=new Person("弹指",30);
Person c=new Person("小韩",10);
Book1 b=new Book1(38f,"三国志");
Book1 bo=new Book1(20f,"黑手党");
p.setBook(b);
b.setPerson(p);
c.setBook(bo);
bo.setPerson(c);
p.setChlid(c);
System.out.println("名字:"+p.getName()+"\t"+"年龄"+p.getAge()+"\t"+"孩子姓名"+p.getChlid().getName()+"\t"+"孩子年龄"+p.getChlid().getAge()+"\t"+"书名"
+p.getBook().getTitle()+"\t"+"书的价格"+p.getBook().getPrice());
}
public static void ff(Object obj)
{
System.out.println(obj);
}
}
class Person
{
private String name;//姓名
private int age;//年龄
private String sex;//性别
private Person chlid;//一个人有一个孩子
private Book1 book;//一个人有一本书
public Person(String name,int age)//通过构造函数设置属性类容
{
this.setName(name);
this.setAge(age);
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setChlid(Person chlid)
{
this.chlid=chlid;
}
public Person getChlid()
{
return chlid;
}
public void setBook(Book1 book)
{
this.book=book;
}
public Book1 getBook()
{
return book;
}
}
class Book1
{
private float price;
private String title;
private Person person;
public Book1(float price,String title)//通过构造函数设置属性类容
{
this.setPrice(price);
this.setTitle(title);
}
public void setPrice(float p)
{
price=p;
}
public float getPrice()
{
return price;
}
public void setTitle(String q)
{
title=q;
}
public String getTitle()
{
return title;
}
public void setPerson(Person person)
{
this.person=person;
}
public Person getPerson()
{
return person;
}
} |