如何使用 Java 中的另一个对象创建一个对象?

How can I make an object using another object in Java?

我正在定义狗 class 的特征,其中包含定义其名称和年龄的实例数据,为该数据创建构造函数,setter-getter 方法等。我也有一种方法 returns “狗年”的值(又名,七年乘以人类一年,相当于他们的年龄)。我有一个驱动程序 class,Kennel,主要方法实例化和更新这些 Dog 对象,并将它们打印出来。

在 Kennel class 中,您可以看到我最后的声明之一,上面写着“在某个时间点查询”的评论。 即://Dog dog3 = dog2.setName("Krypto"); 在评论中,我试图创建一个名为“Kryto”的新 Dog 对象,同时通过 setName 方法将 dog2 对象的其余数据分配给它。换句话说,我希望我的 dog3 对象具有 dog2 的所有特征,但名称除外。

实现这个的方法是什么?我知道我不能使用对象作为参数,对吗?

下面是我的两个 class 代码:

Kennel (Driver):

    public class Kennel {
    //instance data
    int age;
    public static void main(String[] args) {
        //dog objects
        Dog dog1 = new Dog("Bear", 3);
        Dog dog2 = new Dog("Bella", 7);
        
        //calling the method required to calculate the dog's human years, so that when I print these objects out, they actually have their traits
        //(that being, their "dogman" age.
        dog1.calcDogmanYears();
        dog2.calcDogmanYears();
        
        //output
        System.out.println(dog1);
        System.out.println(dog2);
        dog2.setName("Krypto");
        //Dog dog3 = dog2.setName("Krypto"); //INQUIRE AT SOME POINT
        System.out.println(dog2);

    }

}

狗:

public class Dog {
    //instance data
    String name;
    int age;
    
    //constructor (normal) to allow objects to have values by default (i.e. no requirement of forcing parameters down the methods from the driver class)
    public Dog()    {
        name = "";
        age = 0;
        return;
    }
    
    //constructor, keep in mind for future reference, that I used different variables to allow the constructor to be overloaded in a driver class to
    //follow Chapter 4's constructor Java syntax. We could've overloaded the constructor with formal parameters of the same name as the instance data
    //with the "this.(insert variable here)" modifier. I did this for shits and giggles.
    public Dog(String name, int age){
        this.name = name;
        this.age = age;
        return;
    }
    
    //method to convert dog to human years
    public int calcDogmanYears() {
        age = age * 7;
        return age;
    }
        
    //setters and getters for name
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    
    //mutators and accessors for age}
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    
    public String toString() {
        String result = "The name of the dog is " + name + ", and their age is " + age + " years old.";
        return result;
    }
}

您可以在 Dog class 中定义一个新方法:

public Dog createTheSameAgeDog(String name) {
    return new Dog(name, this.age);
}

然后调用它:

Dog dog3 = dog2.createTheSameAgeDog("Krypto"); // creates the Dog (age 7, name "Krypto") 

一种选择是使 Dog class 可克隆。克隆原始文件,然后适当设置 name

Dog dog3 = dog2.clone();
dog3.setName("Krypto");

犬舍 (Driver) class:

public class Kennel {
    //instance data
    int age;
    public static void main(String[] args) throws CloneNotSupportedException {
        //dog objects
        Dog dog1 = new Dog("Bear", 3);
        Dog dog2 = new Dog("Bella", 7);
        
        //calling the method required to calculate the dog's human years, so that when I print these objects out, they actually have their traits
        //(that being, their "dogman" age.
        dog1.calcDogmanYears();
        dog2.calcDogmanYears();
        
        //output
        System.out.println(dog1);
        System.out.println(dog2);
        dog2.setName("Krypto");
        Dog dog3 = dog2.clone();
        dog3.setName("Krypto"); //INQUIRE AT SOME POINT
        System.out.println(dog2);

    }

}

更新狗class:

public class Dog implements Cloneable {
    //instance data
    String name;
    int age;
    
    //constructor (normal) to allow objects to have values by default (i.e. no requirement of forcing parameters down the methods from the driver class)
    public Dog()    {
        name = "";
        age = 0;
    }
    // Overriding clone() method of Object class
    public Dog clone() throws CloneNotSupportedException{  
    return (Dog) super.clone();  
    }
    
    public Dog(String name, int age){
        this.name = name;
        this.age = age;
    }
    
    //method to convert dog to human years
    public int calcDogmanYears() {
        age = age * 7;
        return age;
    }
        
    //setters and getters for name
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    
    //mutators and accessors for age}
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    
    public String toString() {
        String result = "The name of the dog is " + name + ", and their age is " + age + " years old.";
        return result;
    }
}

输出:

The name of the dog is Bear, and their age is 21 years old.
The name of the dog is Bella, and their age is 49 years old.
The name of the dog is Krypto, and their age is 49 years old.