本质上,我正在制作一个小程序,用于处理 double 值和枚举作为输入并返回一些输出。问题是我需要创建一个新对象:
对于这个问题,我无法导入类或使用静态方法,因此我一直尝试在没有任何导入类的情况下克隆这两个对象。我不确定我的“Example z = new Example(t)”行是否为假或什么。我可以将“t”的值设置为另一个设置的私有(private)“Example”对象,但我不知道如何将该信息发送到对象“z”。
public class Example{
private double temp;
private Scale scale;
private char start;
public Example(double temp){...}
public Example(double temp, Scale scale){...}
public Example(Example input){
/*I don't know what to put in here in order
in order to copy the input to a new object*/
}
}
public class Test{
public static void main(String[] args){
Example t = new Example(-10000.1, Scale.FAHRENHEIT);
Example z = new Example(t);
}
最佳答案
public Example(Example input){
this.temp = input.temp;
this.scale = input.scale;
this.start = input.start;
}
可能有帮助;)
关于java - 不确定如何初始化一个新对象,该对象是 JAVA 中另一个对象的对象参数的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505460/