Java 如何将祖先复制到后代

Java 如何将祖先复制到后代,java,inheritance,copy,clone,Java,Inheritance,Copy,Clone,假设我有一只动物,现在我想把它变成一只狗。我如何在java中实现这一点 现在我有一个构造函数 public Dog(Animal animal) { this.setProperty(animal.getProperty); ... } 虽然这起作用,但它是脆弱的。还有其他建议吗 是否要对动物类进行子类化? 您还可以使用: public class Dog extends Animal { public Dog () { super(); //

假设我有一只动物,现在我想把它变成一只狗。我如何在java中实现这一点

现在我有一个构造函数

public Dog(Animal animal) {
  this.setProperty(animal.getProperty);
  ...
}

虽然这起作用,但它是脆弱的。还有其他建议吗

是否要对动物类进行子类化? 您还可以使用:

public class Dog extends Animal { 
    public Dog () {
        super();
        // other constructor stuff
    }
}

那么您的Dog对象将已经继承属性。

是否要将Animal类划分为子类? 您还可以使用:

public class Dog extends Animal { 
    public Dog () {
        super();
        // other constructor stuff
    }
}

然后您的狗对象将已经继承属性。

如果您的狗扩展了Animal,您可以创建一个构造函数,该构造函数接受动物并初始化超级(父)构造函数:

public class Dog extends Animal {
    public Dog(Animal animal) {
        super(animal);
    }
}
假设您有一个动物类,该类具有以下形式的复制构造函数:

public class Animal {
    public Animal(Animal animal) {
        // copies all properties from animal to this
    }
}
您可以通过以下操作从动物创建狗:

Dog newDog = new Dog(myExistingAnimal);
Animal animal = getAnAnimalFromSomewhere();
// `animal` has generic Animal behaviour
animal.becomeADog();
// `animal` now has Dog behaviour

如果您的狗扩展了Animal,您可以创建一个构造函数,该构造函数接受动物并初始化超级(父)构造函数:

public class Dog extends Animal {
    public Dog(Animal animal) {
        super(animal);
    }
}
假设您有一个动物类,该类具有以下形式的复制构造函数:

public class Animal {
    public Animal(Animal animal) {
        // copies all properties from animal to this
    }
}
您可以通过以下操作从动物创建狗:

Dog newDog = new Dog(myExistingAnimal);
Animal animal = getAnAnimalFromSomewhere();
// `animal` has generic Animal behaviour
animal.becomeADog();
// `animal` now has Dog behaviour

尝试使用工厂。与其基于构造函数,不如使用工厂根据您的约束返回特定类型的动物

尝试使用工厂。与其基于构造函数,不如使用工厂根据您的约束返回特定类型的动物

我不确定您到底想要什么,所以我假设您想要将动物对象升级为狗对象

class AnimalImpl {
    // ...
}

class DogImpl extends AnimalImpl {
    // ...
}

class Animal {
    private AnimalImpl implementation;
    public Animal() {
        implementation = new AnimalImpl;
    }
    public void becomeADog() {
        implementation = new DogImpl(implementation);
    }
    // ...
}
像这样使用它:

Dog newDog = new Dog(myExistingAnimal);
Animal animal = getAnAnimalFromSomewhere();
// `animal` has generic Animal behaviour
animal.becomeADog();
// `animal` now has Dog behaviour

这可能不是您想要的,但当对象根据其状态应该具有实质性不同的行为时,它会很有用。

我不确定您想要的是什么,因此我假设您希望将动物对象升级为狗对象

class AnimalImpl {
    // ...
}

class DogImpl extends AnimalImpl {
    // ...
}

class Animal {
    private AnimalImpl implementation;
    public Animal() {
        implementation = new AnimalImpl;
    }
    public void becomeADog() {
        implementation = new DogImpl(implementation);
    }
    // ...
}
像这样使用它:

Dog newDog = new Dog(myExistingAnimal);
Animal animal = getAnAnimalFromSomewhere();
// `animal` has generic Animal behaviour
animal.becomeADog();
// `animal` now has Dog behaviour

这可能不是您想要的,但当一个对象根据其状态应该具有实质性不同的行为时,它会很有用。

您感兴趣的是:1)获取一个动物的通用实例,并使用它创建一个更专门的狗的实例?或者2)获取除Dog以外的某些动物亚类的特定实例,并使用它创建Dog的新实例。我假设狗是动物的一个子类,因为你问的是“将祖先复制到后代”我有一个“动物”的实例,想把它变成一个“狗”的实例,然后添加我的狗的所有特定属性。你感兴趣的是什么:1)获取一个通用的动物实例,并使用它创建一个更专门的狗实例?或者2)获取除Dog以外的某些动物亚类的特定实例,并使用它创建Dog的新实例。我假设狗是动物的一个子类,因为你问的是“[复制]祖先到后代”我有一个“动物”的实例,想把它变成一个“狗”的实例,然后添加我的狗特有的所有属性。这或多或少和我现在的解决方案相同,只是复制代码在后代中。这确实改进了设计,但仍然感觉应该有更好的解决方案可用。使用反射怎么样?这和我现在的解决方案差不多,只是复制代码在子体中。这确实改进了设计,但仍然感觉应该有更好的解决方案可用。使用反射怎么样?