Java面向对象编程 -7

类关联结构
引用传递是整个Java开发与设计过程之中最为重要的技术组成,对于引用传递也与我们实际生活密切相关。

class Car{
	private float value;
	private String name;
	private Person person;
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return this.name;
	}
	public void setValue(float value){
		this.value = value;
	}
	public float getValue(){
		return this.value;
	}
	public void setPerson(Person person){
		this.person = person;
	}
	public Person getPerson(){
		return this.person;
	}
	public String getInfo(){
		return "name"+name+"value"+value;
	}
	public Car(String name,float value){
		this.name = name;
		this.value = value;
	}
}


class Person{
	private String name;
	private int age;
	private Car car;
	
	public void setName(String name){
		this.name= name;
	}
	public String getName(){
		return this.name;
	}
	public void setAge(int age){
		this.age = age;
	}
	public int getAge(){
		return this.age;
	}
	public void setCar(Car car){
		this.car = car;
	}
	public Car getCar(){
		return this.car;
	}
	public String getInfo(){
		return "name"+name+"age"+age;
	}
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}
}


public class name {	
	public static void main(String args[]){
		Person person = new Person("张三",18);
		Car car = new Car("宾利", 20000000.00f);
		person.setCar(car);
		car.setPerson(person);
		System.out.println(person.getCar().getInfo());//代码链
		System.out.println(car.getPerson().getInfo());
	}
}

Car和Person都可以明确地描述某一类群体,现在是对于群体的关系做出的一种设置 这就实现了一种基础的引用过程

posted @ 2020-03-15 20:37  上杉家主-上杉绘梨衣  阅读(93)  评论(0编辑  收藏  举报