学习笔记——引用传递的应用(类的关联结构)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45176509/article/details/102747896

相互关联

通过引用传递,两个类之间可以通过参数传递,将其中一个类的属性传递给另一个类打印输出。
例如:现在有两个类,一个为汽车类,另一个为人类(车主)。可以通过汽车得到车主信息,也可以通过车主知道汽车信息。
代码实现:

class  Person{  //车主类
private String name;
private int age;
private Car car;  //定义车主的属性车
public Person(){}   //构造方法
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 getInfo1(){
  return "车主信息***姓名:"+this.name+"、年龄:"+this.age;
  }
 }
class Car{  //车类
private String name;
private String color;
private Person person;  //定义车的属性车主
public Car(){}
public Car(String name,String color){
  this.name=name;
  this.color=color;
  }
public void setPerson(Person person){
  this.person=person;
  }
public Person getPerson(){
  return this.person;
  }
public String getInfo2(){
  return "汽车信息***车名:"+this.name+"、颜色:"+this.color;
  }
}
public class Java{
public static void main(String args[]){
  Person a=new Person("魏武咸",26);//车主实例化
  Car b=new Car("随便","red");//汽车实例化
  a.setCar(b);//汽车参数传入
  b.setPerson(a);
  System.out.println(a.getCar().getInfo2());//车主对象调用显示汽车信息
  System.out.println(b.getPerson().getInfo1());//汽车对象调用显示车主信息
  }
}

输出:
汽车信息***车名:随便、颜色:red
车主信息***姓名:魏武咸、年龄:26


自身关联

已经确定了人和车的关系,假如车主有孩子,他的孩子也有车,则可以通过相互关联来实现。

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;
 }
}
class	Person{//定义工具类
 private String name;
 private int age;
 private Car car;//一个人一辆车
 private Person child[];//一个人有一或多个孩子 
 public Person(String name,int age){
   this.name=name;
	this.age=age;
 }
 public void setChild(Person child[]){
 this.child =child ;
 }
 public void setCar(Car car){
 this.car=car;
 }
public	Person [] getChild(){
 return this.child;
 }
 public Car getCar(){
 return this.car;
 }
 public String getInfo(){
 return "姓名:"+this.name+"、年龄:"+this.age;
 }
}
public class Java{
public static void main (String args[]){
	//第一步:声明对象实例化,设置彼此关系
	Person person=new Person ("魏颖",49);
    Person child1=new Person ("魏子虚",22);
    Person child2=new Person ("魏子夫",23);
	child1.setCar(new Car("星辰",30000));//匿名对象
    child2.setCar(new Car("大海",29000));
	person.setChild(new Person [] {child1,child2});
	Car car=new Car("随便",1000000.00);
	person.setCar(car);//一人一车
	car.setPerson(person);//一辆车属于一个人
   //第二步:根据关系获取数值
   System.out.println("车主信息:"+car.getPerson().getInfo());
   System.out.println("驾驶的车:"+person.getCar().getInfo());
	//根据人找到孩子与车
	for (int x=0; x<person.getChild().length;x++ ){
		System.out.println(person.getChild()[x].getInfo());
       System.out.println("******"+person.getChild()[x].getCar().getInfo());
	}
	}
}

输出结果:
车主信息:姓名:魏颖、年龄:49
驾驶的车:车名:随便、价格:1000000.0
姓名:魏子虚、年龄:22
******车名:星辰、价格:30000.0
姓名:魏子夫、年龄:23
******车名:大海、价格:29000.0

猜你喜欢

转载自blog.csdn.net/weixin_45176509/article/details/102747896