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

Java实现Dog与Owner类双向关联:修改一方自动同步另一方属性

狗狗拍卖程序双向关联修改方案

1. 改造Dog类的setOwner方法

修改逻辑:

  • 先判断新owner和当前owner是否一致,避免重复操作触发死循环
  • 如果当前dog已经绑定了原owner,自动从原owner的狗数组中移除该dog
  • 更新dog的owner属性后,自动将该dog添加到新owner的狗数组中
    修改后的setOwner代码如下,Dog类其他代码无需改动:
public Owner setOwner(Owner newOwner) {
    // 相同owner无需重复操作
    if (this.owner == newOwner) {
        return this.owner;
    }
    // 从原主人的狗列表中移除当前狗
    if (this.owner != null) {
        this.owner.removeDog(this);
    }
    // 更新绑定的owner
    this.owner = newOwner;
    // 给新主人添加当前狗
    if (newOwner != null) {
        newOwner.addDog(this);
    }
    return this.owner;
}

2. 改造Owner类,补充数组操作逻辑

作业要求用array存储Dog,需手动实现数组动态扩容、添加、移除方法,同时把dogList改为私有避免外部直接修改破坏关联逻辑:

public class Owner {
    private String ownerName;
    // 改为私有属性,禁止外部直接修改
    private Dog[] dogList = new Dog[0]; // 初始化为空数组,后续动态扩容
    
    public Owner(String ownerName) {
        this.ownerName = ownerName;
    }
    
    // 新增添加狗的方法
    public void addDog(Dog dog) {
        // 去重判断,避免重复添加同一只狗
        for (Dog existDog : dogList) {
            if (existDog == dog) {
                return;
            }
        }
        // 数组扩容1位
        Dog[] newDogList = new Dog[dogList.length + 1];
        System.arraycopy(dogList, 0, newDogList, 0, dogList.length);
        newDogList[dogList.length] = dog;
        this.dogList = newDogList;
    }
    
    // 新增移除狗的方法
    public void removeDog(Dog dog) {
        int targetIndex = -1;
        // 查找要移除的狗的下标
        for (int i = 0; i < dogList.length; i++) {
            if (dogList[i] == dog) {
                targetIndex = i;
                break;
            }
        }
        if (targetIndex == -1) {
            return;
        }
        // 数组缩容
        Dog[] newDogList = new Dog[dogList.length - 1];
        System.arraycopy(dogList, 0, newDogList, 0, targetIndex);
        System.arraycopy(dogList, targetIndex + 1, newDogList, targetIndex, dogList.length - targetIndex - 1);
        this.dogList = newDogList;
    }
    
    // 对外提供只读的狗列表,避免外部修改原数组
    public Dog[] getDogList() {
        return dogList.clone();
    }
    
    public String toString(){
        return "Name: " + ownerName;
    }
    
    public String getName(){
        return ownerName;
    }            
}

3. 修正giveDog业务方法

原有代码存在变量名错误,且不需要手动给owner的dogList赋值,setOwner已经自动完成双向关联:

public void giveDog(){
    System.out.print("Enter the name of the dog?>");
    String name = input.nextLine();
    Dog dog = findDog(name);

    if (dog == null){
        System.out.print("Error: No dog with that name");
        return; 
    }
   
    if (dog.getOwner() !=null){                    
        System.out.print("Error: Dog already has owner");
        return; 
    }

    System.out.print("Enter the name of the new owner?>");
    String nameOfOwner = input.nextLine().toUpperCase();
    Owner o = findOwner(nameOfOwner);

    if (o!= null) {
        // 仅调用setOwner即可自动完成双向绑定
        dog.setOwner(o);
        // 修正原代码变量名错误,把owner改为查询到的o
        System.out.println(o.getName()+" now owns "+ dog.getName()); 
    } else {
        System.out.print("Error: No such owner");
    } 
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 20:00:04