java - 如何使用 Java 中的另一个对象创建一个对象?
问题描述
我正在定义 Dog 类的特征,该类包含定义其名称和年龄的实例数据,为此数据创建构造函数,setter-getter 方法等。我还有一个返回“狗年”值的方法(又名,七年乘以他们同等年龄的人类年)。我有一个驱动程序类 Kennel,它的主要方法实例化和更新这些 Dog 对象,并将它们打印出来。
在 Kennel 类中,我的最后一条语句中您可以看到一条评论,上面写着“在某个点查询”。即://Dog dog3 = dog2.setName("Krypto");
在评论中,我尝试创建一个名为“Kryto”的新 Dog 对象,同时分配其余的通过 setName 方法将 dog2 对象的数据传递给它。换句话说,我希望我的 dog3 对象具有 dog2 的所有特征,但名称除外。
实现这一目标的方法是什么?我知道我不能将对象用作参数,对吗?
这是我下面两个类的代码:
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
类可克隆。克隆原件,然后name
适当设置。
Dog dog3 = dog2.clone();
dog3.setName("Krypto");
犬舍(司机)类:
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);
}
}
更新的狗类:
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;
}
}
输出:
这只狗的名字叫熊,它们的年龄是21岁。
这只狗的名字叫贝拉,它们的年龄是49岁。
这只狗的名字叫Krypto,它们的年龄是49岁。
推荐阅读
- python - 什么是批量收集的聪明方法?
- influxdb - 增加 influxdb 的字节计数器
- r - 构建一个专门训练用于预测 R 中的数值的 CNN
- angular - Angular 服务中服务
- xcode - Swift ui mac os 安装/使用 git-macOS 和 carthage
- r - 在 R 中使用数据框作为过滤条件
- java - 使用 launch4j 为 JAR 应用程序设置自定义进程名称?
- ruby - 更改类和模块的子定义样式后未初始化的常量错误
- snowflake-cloud-data-platform - 雪花查询配置文件中的排序与 SortWithLimit
- python - 格式化的 Python 字符串既不使用 repr 也不使用 str - 发生了什么?