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()) ;
}
}
运行结果:
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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()) ;
}
}
运行结果:
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
相关文章推荐
- java方法计算1到20的阶乘的和
- Java编程中“为了性能”尽量要做到的一些地方
- java打印三角星图
- 一个简单的Java范例
- 第一个java程序
- JavaFX ScrollPane滚动到指定位置
- Java各种容器比较 (图例)
- 排序算法后记
- 解决 eclipse 启动报 ”failed to load the jni shared library“ 问题
- android studio在添加了jdk环境变量后依然无法打开问题解决
- Java中的流与文件:正则表达式
- JAVA深复制(深克隆)与浅复制(浅克隆)
- Java用Commons-Net实现Telnet协议后台远程登录控制
- 5、Mybatis-Spring入门
- Spring MVC 教程,快速入门,深入分析(转)
- Java集合类 课后练习
- Java基础--流程控制
- Java枚举类型
- 邻接表(Java)
- 20145311 《Java程序设计》第2周学习总结