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 getCarInfo(){
return "汽车品牌:" + name + ",价格:" + price;
}
}
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 getPersonInfo(){
return "姓名:" + this.name + ",年龄:" + this.age;
}
// setter,getter 略
}
public class HelloWorld {
public static void main(String[] args){
// 第一步.声明对象并且设置彼此的关系
Person person = new Person("林强",20);
Car car = new Car("宾利",100000.00);
person.setCar(car); // 一个人有一辆车
car.setPerson(person); // 一辆车属于一个人
// 第二步:根据关系获取数据
System.out.println(person.getCar().getCarInfo());// 从人找车
System.out.println(car.getPerson().getPersonInfo());// 从车找人;
}
}
标签:name,car,6.11,关联,person,String,Car,public,结构
From: https://www.cnblogs.com/pansidong/p/17459141.html