package animal;
class AnimalHumanDogDemo
{
public static void main(String[] args)
{
Animal chicken = new Animal(1.53, 53.2, 14.2, "red junglefowl");
Human Alex = new Human(1.73, 62.3, 52.0, "Ape", "Greek", "Programmer", 1234);
Dog Betty = new Dog(0.53, 21.6, 8.3, "wolf", "chiouaoua", 214, false, "white");
String a = chicken.toString();
System.out.println(a);
double b = Alex.Yearly_salary();
System.out.println(b);
String c = Alex.toString();
System.out.println(c);
Boolean d = Betty.Expensive_Purebred();
System.out.println(d);
String e = Betty.toString();
System.out.println(e);
}
}
当我运行main时,字符串和bollean变量的结果总是为null, 和' 0'其他一切。 我在动物,人类和狗类中创建了构造函数,就像我一直在做的那样。 我可以提供课程'代码如果需要。
答案 0 :(得分:1)
您必须将变量存储在构造函数中。
public class Animal
{
private double x;
private double y;
private String des;
public Animal(double x, double y, String des)
{
this.x = x;
this.y = y;
this.des = des;
}
}
如cricket_007 x = x
所述,可能会导致问题。