java中的引用传递实际应用

/*
 * 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));
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值