如何使用私有成员 Java 创建一个带有子 class 的工作超级 class
How to create a working super class with a sub class using private members Java
我正在参加一个编码训练营并创建了一个 Superclass "Animal" 和一个 subclass "Lion"。所有变量都是私有变量。
当我尝试传递所需的值时,我收到一条错误消息,指出 Lion 无法解析为类型。这是我在 Whosebug 上提出的第一个问题,如果没有正确遵循正常约定,我们深表歉意。
///动物class与子class狮子///
public class Animal {
private int numTeeth;
private boolean spots;
private int weight;
public Animal(int numTeeth, boolean spots, int weight) {
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
int getNumTeeth() {
return numTeeth;
}
boolean getSpots() {
return spots;
}
int getWeight() {
return weight;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public void setWeight(int weight) {
this.weight = weight;
}
class Lion extends Animal {
private boolean maine;
private String region;
private int type;
public Lion(boolean maine, String region, int type) {
super(numTeeth, spots, weight);
this.setMaine(maine);
this.setRegion(region);
this.setType(type);
}
boolean getMaine() {
return maine;
}
String getRegion() {
return region;
}
int getType() {
return type;
}
public void setMaine(boolean maine) {
this.maine = maine;
}
public void setRegion(String region) {
this.region = region;
}
public void setType(int type) {
this.type = type;
}
void showAnimal() {
System.out.println("The number of teeth is: " + getNumTeeth());
System.out.println("Does the animal have spots!: " + getSpots());
System.out.println("The animals weight!: " + getWeight());
System.out.println("Do the animal have a maine!: " + getMaine());
System.out.println("The animal is from: " + getRegion());
System.out.println("The animal is a: " + getType());
}
}
}
///我试图传入 "AnimalDetails" 的动物统计数据,这是一个新的 class 文件///
public class AnimalStats {
public static void main(String[] args) {
Lion stats = new Lion();
stats.setMaine(true);
stats.setNumTeeth(20);
stats.setRegion("South Africa");
stats.setSpots(false);
stats.setType(2);
stats.setWeight(150);
}
}
我发现的错误在/Lion stats = new Lion(); / Lion 不能被归类为一个类型
Lion 未声明为静态
为了按照您的方式构建 Lion,Lion 应该是静态内部 class。
Lion 中声明的构造函数不包含 super
的参数
为调用 super 添加参数,或者在调用 super 时在构造函数实现中为 numTeeth、spots 和 weight 提供默认值。
Lion 没有 zero-argument 构造函数
您的代码试图实例化一个没有参数的 Lion。如果你想要一个 zero-argument 构造函数,添加一个并为所有成员设置默认值。
否则,调用您已经定义的构造函数。
类似于:
public class Animal {
private int numTeeth;
private boolean spots;
private int weight;
public Animal(int numTeeth, boolean spots, int weight) {
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
int getNumTeeth() {
return numTeeth;
}
boolean getSpots() {
return spots;
}
int getWeight() {
return weight;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public void setWeight(int weight) {
this.weight = weight;
}
static class Lion extends Animal {
private boolean maine;
private String region;
private int type;
public Lion(boolean maine, String region, int type, int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
this.setMaine(maine);
this.setRegion(region);
this.setType(type);
}
boolean getMaine() {
return maine;
}
String getRegion() {
return region;
}
int getType() {
return type;
}
public void setMaine(boolean maine) {
this.maine = maine;
}
public void setRegion(String region) {
this.region = region;
}
public void setType(int type) {
this.type = type;
}
void showAnimal() {
System.out.println("The number of teeth is: " + getNumTeeth());
System.out.println("Does the animal have spots!: " + getSpots());
System.out.println("The animals weight!: " + getWeight());
System.out.println("Do the animal have a maine!: " + getMaine());
System.out.println("The animal is from: " + getRegion());
System.out.println("The animal is a: " + getType());
}
}
public static void main(String[] args) {
Lion stats = new Lion(true, "South Africa", 2, 20, false, 150);
}
}
这是一个嵌套 class 的情况,其中一个外部 class 和一个内部 class。
您必须使用外部 class 来实例化内部 class。
由于没有可用的默认构造函数,您必须在实例化时输入值。
Animal.Lion stats = new Animal(20, false, 150).new Lion(true, "South Africa", 2);
AnimalStats class:
public class AnimalStats {
public static void main(String[] args) {
Animal.Lion stats = new Animal(20, false, 150).new Lion(true, "South Africa", 2);
}
}
注意:此解决方案假设 class Animal 必须保持原样并且只需要修复调用者代码。
我正在参加一个编码训练营并创建了一个 Superclass "Animal" 和一个 subclass "Lion"。所有变量都是私有变量。
当我尝试传递所需的值时,我收到一条错误消息,指出 Lion 无法解析为类型。这是我在 Whosebug 上提出的第一个问题,如果没有正确遵循正常约定,我们深表歉意。
///动物class与子class狮子///
public class Animal {
private int numTeeth;
private boolean spots;
private int weight;
public Animal(int numTeeth, boolean spots, int weight) {
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
int getNumTeeth() {
return numTeeth;
}
boolean getSpots() {
return spots;
}
int getWeight() {
return weight;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public void setWeight(int weight) {
this.weight = weight;
}
class Lion extends Animal {
private boolean maine;
private String region;
private int type;
public Lion(boolean maine, String region, int type) {
super(numTeeth, spots, weight);
this.setMaine(maine);
this.setRegion(region);
this.setType(type);
}
boolean getMaine() {
return maine;
}
String getRegion() {
return region;
}
int getType() {
return type;
}
public void setMaine(boolean maine) {
this.maine = maine;
}
public void setRegion(String region) {
this.region = region;
}
public void setType(int type) {
this.type = type;
}
void showAnimal() {
System.out.println("The number of teeth is: " + getNumTeeth());
System.out.println("Does the animal have spots!: " + getSpots());
System.out.println("The animals weight!: " + getWeight());
System.out.println("Do the animal have a maine!: " + getMaine());
System.out.println("The animal is from: " + getRegion());
System.out.println("The animal is a: " + getType());
}
}
}
///我试图传入 "AnimalDetails" 的动物统计数据,这是一个新的 class 文件///
public class AnimalStats {
public static void main(String[] args) {
Lion stats = new Lion();
stats.setMaine(true);
stats.setNumTeeth(20);
stats.setRegion("South Africa");
stats.setSpots(false);
stats.setType(2);
stats.setWeight(150);
}
}
我发现的错误在/Lion stats = new Lion(); / Lion 不能被归类为一个类型
Lion 未声明为静态
为了按照您的方式构建 Lion,Lion 应该是静态内部 class。
Lion 中声明的构造函数不包含 super
的参数为调用 super 添加参数,或者在调用 super 时在构造函数实现中为 numTeeth、spots 和 weight 提供默认值。
Lion 没有 zero-argument 构造函数
您的代码试图实例化一个没有参数的 Lion。如果你想要一个 zero-argument 构造函数,添加一个并为所有成员设置默认值。
否则,调用您已经定义的构造函数。
类似于:
public class Animal {
private int numTeeth;
private boolean spots;
private int weight;
public Animal(int numTeeth, boolean spots, int weight) {
this.setNumTeeth(numTeeth);
this.setSpots(spots);
this.setWeight(weight);
}
int getNumTeeth() {
return numTeeth;
}
boolean getSpots() {
return spots;
}
int getWeight() {
return weight;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public void setWeight(int weight) {
this.weight = weight;
}
static class Lion extends Animal {
private boolean maine;
private String region;
private int type;
public Lion(boolean maine, String region, int type, int numTeeth, boolean spots, int weight) {
super(numTeeth, spots, weight);
this.setMaine(maine);
this.setRegion(region);
this.setType(type);
}
boolean getMaine() {
return maine;
}
String getRegion() {
return region;
}
int getType() {
return type;
}
public void setMaine(boolean maine) {
this.maine = maine;
}
public void setRegion(String region) {
this.region = region;
}
public void setType(int type) {
this.type = type;
}
void showAnimal() {
System.out.println("The number of teeth is: " + getNumTeeth());
System.out.println("Does the animal have spots!: " + getSpots());
System.out.println("The animals weight!: " + getWeight());
System.out.println("Do the animal have a maine!: " + getMaine());
System.out.println("The animal is from: " + getRegion());
System.out.println("The animal is a: " + getType());
}
}
public static void main(String[] args) {
Lion stats = new Lion(true, "South Africa", 2, 20, false, 150);
}
}
这是一个嵌套 class 的情况,其中一个外部 class 和一个内部 class。
您必须使用外部 class 来实例化内部 class。
由于没有可用的默认构造函数,您必须在实例化时输入值。
Animal.Lion stats = new Animal(20, false, 150).new Lion(true, "South Africa", 2);
AnimalStats class:
public class AnimalStats {
public static void main(String[] args) {
Animal.Lion stats = new Animal(20, false, 150).new Lion(true, "South Africa", 2);
}
}
注意:此解决方案假设 class Animal 必须保持原样并且只需要修复调用者代码。