引用传递的实际应用 - Java基础知识 5

                                                目录

引用分析实例一(类关联结构)

引用分析实例二(自身关联)

引用分析案例三(合成设计模式)


学习笔记

引用传递是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之中,被称为合成设计。拆分后的每一个模块都是独立的,按照标准整合在一起。

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值