You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java编程中如何通过子Person实例反向获取关联Father实例属性

实现方案

你当前的代码是单向关联结构:仅Father实例持有子对象的引用,子对象本身没有存储关联的Father实例信息,因此无法反向访问。要实现通过子对象获取关联Father实例的需求,只要建立双向关联即可,具体实现步骤如下:

  • 先修复现有代码的基础编译/设计问题
    你当前写的代码存在几个会直接导致编译失败的问题,需要先修正:

    1. Father类的setName方法参数类型错误写为Name,实际应为String类型
    2. Person类缺失setName、getName方法,测试类中调用p1.setName()会报错
    3. 测试类中p1.setName("Kai")行尾缺失分号
    4. 不建议将Father类的child1/child2/child3设为公有属性允许外部直接赋值,容易漏写关联逻辑,建议改为私有属性通过setter方法管控
  • 在Person基类中新增Father关联属性
    所有子对象都是Person类型,直接在Person类中添加Father类型的成员变量存储关联的父亲实例,同时补全对应getter/setter:

    public class Person {
        String name;
        int age;
        String addresse;
        // 存储当前对象关联的父亲实例
        protected Father father;
    
        public Person (){
        }
    
        public Person(String name, int age, String addresse) {
            this.name = name;
            this.age = age;
            this.addresse = addresse;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return this.name;
        }
    
        public Father getFather() {
            return this.father;
        }
    
        public void setFather(Father father) {
            this.father = father;
        }
    }
    
  • 改造Father类,自动维护双向关联关系
    给三个孩子属性编写专属的setter方法,在给Father设置子对象时,自动将子对象的father属性指向当前Father实例,无需手动重复赋值,避免漏配:

    public class Father extends Person {
        private Person child1;
        private Person child2;
        private Person child3;    
    
        public Father() {
        }
    
        public Father(String name, int age, String addresse) {
            this.name = name;
            this.age = age;
            this.addresse = addresse;
        }
    
        @Override
        public void setName(String name) {
            this.name = name;
        }
    
        public void setChild1(Person child) {
            this.child1 = child;
            if (child != null) {
                child.setFather(this);
            }
        }
    
        public void setChild2(Person child) {
            this.child2 = child;
            if (child != null) {
                child.setFather(this);
            }
        }
    
        public void setChild3(Person child) {
            this.child3 = child;
            if (child != null) {
                child.setFather(this);
            }
        }
    
        public Person getChild1() {
            return child1;
        }
    
        public Person getChild2() {
            return child2;
        }
    
        public Person getChild3() {
            return child3;
        }
    }
    
  • 调整测试类代码验证效果
    改造完成后,直接调用子对象的getFather()方法即可拿到关联的Father实例,测试代码如下:

    public class PersonTest {
        public static void main(String[] args) {
            Father f = new Father();
            f.setName("Joe");
    
            Person p1 = new Person();
            p1.setName("Kai");
    
            f.setChild1(p1);
            // 直接通过p1获取关联父亲的name属性
            System.out.println(p1.getFather().getName()); // 输出结果:Joe
        }
    }
    

补充说明:如果后续需要支持解除父子关联,在将child属性置空时,记得同步把对应子对象的father属性也置空,避免产生脏引用。如果后续还要实现通过子对象访问关联Mother实例的逻辑,只要在Person类中新增Mother类型的关联属性,按同样的双向关联逻辑实现即可。

内容的提问来源于stack exchange,提问作者Jonathan Zimmermann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 16:21:22