如何解决如何使用Java中的另一个对象制作一个对象?
我正在定义Dog类的特征,其中包含定义其名称和年龄的实例数据,为该数据创建构造函数,setter-getter方法等。我也有一种返回“ dog years”值的方法(也就是人类同等年龄的7倍。我有一个驱动程序类Kennel,该类的主要方法将实例化并更新这些Dog对象,并将其打印出来。
在Kennel类中,我的最后一条语句中可以看到一条注释,内容为““在某点上询问”。
即://Dog dog3 = dog2.setName("Krypto");
在评论中,我尝试通过setName方法向它分配其余dog2对象数据的同时创建一个名为“ Kryto”的新Dog对象。换句话说,我希望我的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岁。
这只狗叫贝拉(Bella),年龄49岁。
这只狗叫Krypto,年龄49岁。
您可以在Dog
类中定义一个新方法:
public Dog createTheSameAgeDog(String name) {
return new Dog(name,this.age);
}
然后称呼它:
Dog dog3 = dog2.createTheSameAgeDog("Krypto"); // creates the Dog (age 7,name "Krypto")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。