您的位置:首页 > 编程语言 > Java开发

java 一对一范例

2016-03-11 23:02 253 查看
源程序:

class Person{

 private String name ;

 private int age ;

 private Book book ;

 public Person(String name , int age){

  this.setName(name) ;

  this.setAge(age) ;

 }

 public String getName(){

  return this.name ;

 }

 public void setName(String name){

  this.name = name ;

 }

 public int getAge(){

  return this.age ;

 }

 public void setAge(int age){

  this.age = age ;

 }

 public Book getBook(){

  return this.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.setTitle(title) ;

  this.setPrice(price) ;

 }

 public String getTitle(){

  return this.title ;

 }

 public void setTitle(String title){

  this.title = title ;

 }

 public float getPrice(){

  return this.price ;

 }

 public void setPrice(float price){

  this.price = price ;

 }

 public Person getPerson(){

  return this.person ;

 }

 public void setPerson(Person person){

  this.person = person ;

 }

}

public class Test{

 public static void main(String args[]){

  Person per = null ;

  per = new Person("张三" , 30)
;

  Book bk = new Book("JAVA
SE核心开发" , 90.3f) ;

  per.setBook(bk) ;

  bk.setPerson(per) ;

  System.out.println("从人找到书 ---
姓名:" + per.getName() + "  年龄:" + per.getAge() +
"  书名:" + per.getBook().getTitle() +
"  价格:" +per.getBook().getPrice()) ;

  System.out.println("从书找到书 ---
姓名:" + bk.getPerson().getName() + "  年龄:" +
bk.getPerson().getAge() + "  书名:" + bk.getTitle()
+ "  价格:" +bk.getPrice()) ;

 }

}

运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: