该类Cat
有 3 个构造函数:
public class Cat {
private double originWeight;
private double weight;
private double minWeight;
private double maxWeight;
private double weightFoodEaten;
private static int count = 0;
public static final int COUNT_CAT_EYES = 2;
public static final double MIN_CAT_WEIGHT = 1000.0;
public static final double MAX_CAT_WEIGHT = 9000.0;
private Color catColor;
public Cat() {
weight = 1500.0 + 3000.0 * Math.random();
originWeight = weight;
minWeight = 1000.0;
maxWeight = 9000.0;
weightFoodEaten = 0;
count++;
}
public Cat(double weight) {
this();
this.weight = weight;
}
public Cat(Cat n) {
weight = n.weight;
originWeight = n.originWeight;
minWeight = n.minWeight;
maxWeight = n.maxWeight;
weightFoodEaten = n.weightFoodEaten;
count++;
}
}
构造函数 Cat(Cat n)
是构造函数的副本Cat(double weight)
。
在类Test_2
中,我首先创建了一个构造函数对象Cat(double weight)
,然后创建了它的副本Cat(Cat n)
。
告诉我为什么我不能创建副本。我需要使用类中的复制构造函数准确地创建一个副本。
public class Test_2 {
public static void main(String[] args) {
Cat bb = new Cat(5000);
Сat cat7 = new Cat(Cat bb);
}
}