目录
引用分析实例一(类关联结构)
引用分析实例二(自身关联)
引用分析案例三(合成设计模式)
学习笔记
引用传递是Java开发中最重要的实际组成部分,对于引用传递也与实际生活密切相关。
引用分析实例一(类关联结构)
假设说现在的生活比较好,于是有的人可以有一辆汽车,有的人就没有。要求可以通过对象设计来解决以上的这种关系转换。
class Car{
private String name ;
private double price ;
private Person person ; // 车属于一个人
public Car(String name, double price){
this.name = name ;
this.price = price ;
}
public void setPerson(Person person){
this.person = person ;
}
public Person getPerson(){
return this.person ;
}
public String getInfo(){
return "车名:" + this.name +
"、价格:" + this.price ;
}
// setter getter 略
}
class Person{
private String name ;
private int age ;
private Car car ; //一个人有一辆车
public Person(String name, int age){// 车不能加入构造方法,有的没有
this.name = name ;
this.age = age ;
}
public void setCar(Car car){
this.car = car ;
}
public Car getCar(){
return this.car ;
}
public String getInfo(){
return "姓名:" + this.name +
"、年龄:" + this.age ;
}
// setter getter略
}
public class ArrayDemo{
public static void main(String args[]){
// 第一步:声明对象并设置彼此关系
Person person = new Person("林强", 29) ;
Car car = new Car("宾利", 8000000.00);
person.setCar(car) ; // 一个人有一辆车
car.setPerson(person) ; // 一个车被一个人所有
// 第二步:根据关系获取数据
System.out.println(person.getCar().getInfo()) ; //代码链
System.out.println(car.getPerson().getInfo()) ;
}
}
Person与Car都是自定义类型,但是Person与Car都可以描述出某一类群体,现在是针对于群体关系做出的一种设置。
引用分析实例二(自身关联)
现在已经去定好人与车的关系,需要进一步完善具体的操作。例如:某个人有孩子,孩子成年后也可以有车。
class Car{
private String name ;
private double price ;
private Person person ; // 车属于一个人
public Car(String name, double price){
this.name = name ;
this.price = price ;
}
public void setPerson(Person person){
this.person = person ;
}
public Person getPerson(){
return this.person ;
}
public String getInfo(){
return "车名:" + this.name +
"、价格:" + this.price ;
}
// setter getter 略
}
class Person{
private String name ;
private int age ;
private Car car ; //一个人有一辆车
private Person children []; //一个人可以有多个孩子
public Person(String name, int age){// 车不能加入构造方法,有的没有
this.name = name ;
this.age = age ;
}
public void setCar(Car car){
this.car = car ;
}
public void setChildren(Person [] children ){ //引用传递
this.children = children ;
}
public Car getCar(){
return this.car ;
}
public Person[] getChildren(){
return this.children ;
}
public String getInfo(){
return "姓名:" + this.name +
"、年龄:" + this.age ;
}
// setter getter略
}
public class ArrayDemo{
public static void main(String args[]){
// 第一步:声明对象并设置彼此关系
Person person = new Person("张三", 29) ;
Person childA = new Person("张小三", 18) ;
Person childB = new Person("张小", 19) ;
childA.setCar(new Car("BMW X1", 30000.00) ) ;
childB.setCar(new Car("法拉利", 150000.00) ) ;
person.setChildren(new Person[] {childA, childB}) ; // 注意需要实例化对象数组
Car car = new Car("宾利", 8000000.00);
person.setCar(car) ; // 一个人有一辆车
car.setPerson(person) ; // 一个车被一个人所有
// 第二步:根据关系获取数据
System.out.println(person.getCar().getInfo()) ; //代码链
System.out.println(car.getPerson().getInfo()) ;
// 根据人找所有的孩子以及对应的车
Person children [] = person.getChildren() ;
for (Person child : children){
System.out.println("\t|-" + child.getInfo()) ;
System.out.println("\t\t|- " + child.getCar().getInfo()) ;
}
}
}
这些关系的匹配都是通过引用数据类型的关联来实现的。对象数组能描述多这个概念。
引用分析案例三(合成设计模式)
假设要求定义描述电脑组成的类,那么在这种情况下,就必须拆分:主机(主机上有一系列硬件)、显示器。
class 电脑{
private 显示器 对象数组 [] ;
private 主机 对象 ;
}
class 显示器
{
}
class 主机{
private 主板 对象 ;
private 鼠标 对象 ;
private 键盘 对象 ;
}
class 主板{
private 内存 对象数组 [] ;
private cpu 对象数组 [] ;
private 显卡 对象 ;
private 硬盘 对象数组 [] ;
}
class 内存{
}
class cpu{
}
class 显卡{
}
class 硬盘{
}
class 键盘{
}
class 鼠标{
}
任何的科技产品都可以进行拆分,然后重组,在这样的设计在java之中,被称为合成设计。拆分后的每一个模块都是独立的,按照标准整合在一起。