重温java知识(十三、引用传递应用案例之二:自身关联结构)

实现自身关联的例子:

package com.mydemo;

// 创建一个“PersonDemo”类
public class PersonDemo {
    public static void main(String args[]) {

        // 实例化Person类对象
        Person person = new Person("张三", 18);
        Person childA = new Person("张三儿子", 2);  // 孩子(人)对象
        Person childB = new Person("张三女儿", 1);  // 孩子(人)对象

        childA.setCar(new Car("婴儿车", 200.00));  // 匿名对象
        childB.setCar(new Car("摇摇车", 1500.00)); // 匿名对象

        person.setChildren(new Person[]{childA, childB});      // 一个人有多个孩子

        // 实例化Car类对象
        Car car = new Car("宝马", 20000.00);
        // 一个人有一辆车
        person.setCar(car);
        // 一辆车属于一个人
        car.setPerson(person);

        // 通过人获取汽车的信息
        System.out.println(person.getCar().getInfo());    // 代码链式写法
        System.out.println("-----------------------");
        // 通过汽车获取人的信息
        System.out.println(car.getPerson().getInfo());    // 代码链式写法
        System.out.println("-----------------------");
        // 获取孩子信息
        for (int i = 0; i < person.getChildren().length; i++) {
            System.out.println("\t|- " + person.getChildren()[i].getInfo());
            System.out.println("\t\t|- " + person.getChildren()[i].getCar().getInfo());
        }

    }
}

// 定义一个“人”类
class Person {

    private String name;        // 人的姓名
    private int age;            // 人的年龄
    private Car car;            // 一个人有一辆车,如果没有车则为null
    private Person children[];  // 一个人有多个孩子

    /**
     * 双参构造函数------构造传入人的信息
     *
     * @param name
     * @param age
     */
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取人员信息
     *
     * @return
     */
    public String getInfo() {
        return "姓名:" + this.name + "、年龄:" + this.age;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取人对应的车的信息
     *
     * @return
     */
    public Car getCar() {
        return car;
    }

    /**
     * 设置人与车的关系
     *
     * @param car
     */
    public void setCar(Car car) {
        this.car = car;
    }

    /**
     * 获取人的孩子的信息
     *
     * @return
     */
    public Person[] getChildren() {
        return children;
    }

    /**
     * 设置人的孩子信息
     *
     * @param children
     */
    public void setChildren(Person[] children) {
        this.children = children;
    }

}

// 定义一个“车”类
class Car {
    private String name;        // 汽车名称
    private double price;       // 汽车价值
    private Person person;      // 车应该属于一个人

    /**
     * 双参构造函数------构造传入汽车的信息
     *
     * @param name
     * @param price
     */
    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getInfo() {
        return "汽车型号:" + this.name + "、汽车价值:" + this.price;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    /**
     * 获取汽车所属人信息
     *
     * @return
     */
    public Person getPerson() {
        return person;
    }

    /**
     * 配置汽车与人的关系
     *
     * @param person
     */
    public void setPerson(Person person) {
        this.person = person;
    }
}
运行结果:
汽车型号:宝马、汽车价值:20000.0
-----------------------
姓名:张三、年龄:18
-----------------------
	|- 姓名:张三儿子、年龄:2
		|- 汽车型号:婴儿车、汽车价值:200.0
	|- 姓名:张三女儿、年龄:1
		|- 汽车型号:摇摇车、汽车价值:1500.0
发布了149 篇原创文章 · 获赞 4 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/yuzhiboyouzhu/article/details/105605572