/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testdemo;
class Member{
private String name;
private int age;
private Car car;
public Member(String name, int age){
this.age = age;
this.name = name;
}
public Car getCar(){
return this.car;
}
public void setCar(Car car){
this.car = car;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getMemberInfo(){
return "Member name" + this.name +
"Member age" + this.age;
}
}
class Car {
private String name;
private double price;
private Member member;
public Car() {};
public Car(String name, double price){
this.name = name;
this.price = price;
}
public Member getMember(){
return this.member;
}
public void setMember(Member member){
this.member = member;
}
public void setName(String name){
this.name = name;
}
public void setPrice(double price){
this.price = price;
}
public String getName(){
return this.name;
}
public double getPrice(){
return this.price;
}
public String getCarInfo(Car car){
return "car name" + car.name +
"car price" + car.price;
}
}
/**
*
* @author Jone
*/
public class TestDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//一:根据关系设置相应的数据
Member member = new Member("刘钊", 22);
Car car = new Car("玛莎拉蒂", 9999999999999.0);
//二:根据关系取出数据设置对象间的引用关系
member.setCar(car);
car.setMember(member);
//三,通过人可以找到车,也可以通过车找到人
System.out.println(car.getMember().getMemberInfo()+member.getCar().getCarInfo(car));
}
}
java中的引用传递实际应用
最新推荐文章于 2022-05-01 22:40:32 发布