Java 调用类时接收null
我必须创建一个狗对象,并赋予它属性和行为。我已经做了属性,但是行为没有按照我想要的那样工作。我收到空的Java 调用类时接收null,java,Java,我必须创建一个狗对象,并赋予它属性和行为。我已经做了属性,但是行为没有按照我想要的那样工作。我收到空的 public class JavaProgram{ public static void main (String [] args){ Dog dog1 = new Dog ("Luca", "mutt", 'M', 22, 5 ); System.out.println("Dog1's name is " + dog1.getName() + ",
public class JavaProgram{
public static void main (String [] args){
Dog dog1 = new Dog ("Luca", "mutt", 'M', 22, 5 );
System.out.println("Dog1's name is " + dog1.getName() + ", its breed is " +
dog1.getBreed() + ", its sex is " + dog1.getSex() + ", its age in months is " +
dog1.getAge() + ", its weight in pounds is " + dog1.getWeight());
System.out.println("When Dog1 eats it makes the noise " + dog1.getEating() +
", and when its barks the noise made is " + dog1.getBarking());
}
}
我应该得到“当狗狗吃东西时,它发出的声音发出咯咯声,咯咯声,咯咯声,当它吠叫时,发出的声音是呜呜,呜呜,呜呜”,但我得到“当狗狗吃东西时,它发出的声音是空的,当它吠叫时,发出的声音是空的”你的意思是:
public Dog(String name, String breed, char sex, int age, double weight){
this("Chomp, chomp, chomp", "Woof, woof, woof");
this.name = name;
this.breed = breed;
this.sex = sex;
this.age = age;
this.weight = weight;
}
public Dog(String eating, String barking){
this.eating = eating;
this.barking = barking;
}
您需要调用构造函数(使用
this()
)设置这些值,因为它不会自动发生。问题是dog1对象是用第一个Dog构造函数创建的
public Dog(String name, String breed, char sex, int age, double weight){
this.name = name;
this.breed = breed;
this.sex = sex;
this.age = age;
this.weight = weight;
}
此构造函数中的eating和barking字段未初始化。您还应调用第二个构造函数。您在eating和barking字段中得到null,因为您正在调用类的第一个构造函数,并且这些字段未分配任何值。 您需要从第一个构造函数调用第二个构造函数
公犬(字符串名称、字符串品种、字符性别、整数年龄、双倍体重){
这个(“,”);
this.name=名称;
这个品种;
这个。性=性;
这个。年龄=年龄;
重量=重量;
}
最好创建一个包含所有字段的构造函数,并使用特定值从其他构造函数调用该构造函数
public Dog(串名、串品种、字符性别、整数年龄、双倍体重、串食、串吠){
这个(“,”);
this.name=名称;
这个品种;
这个。性=性;
这个。年龄=年龄;
重量=重量;
这个。吃=吃;
这个。吠叫=吠叫;
}
公犬(犬串名称、犬串品种、性别、年龄、双倍体重){
这(姓名、品种、性别、年龄、体重,“,”);
}
这毫无意义:
public Dog(String eating, String barking){
this.eating = "Chomp, chomp, chomp";
this.barking = "Woof, woof, woof";
}
参数不用于为字段赋值:实际上,您可以使用一些编译时常量值为字段赋值:“Chomp,Chomp,Chomp”和“Woof,Woof,Woof”。根据您所陈述的问题,您认为任何<代码>狗>代码>默认值为<代码>吃< /代码>和<代码>吠叫< /代码>字段: 我应该说“当狗吃东西的时候,它会发出咯咯,咯咯,咯咯的声音, 当它吠叫时,发出的声音是呜呜,呜呜,呜呜“
dogdog1=新狗(“卢卡”、“马特”、“M”、22、5);
在这种情况下,一种更简单的方法是使用字段初始值设定项对这些字段进行赋值。
另一种方法:链接构造函数(在Simon的回答中提供)也是正确的,但这里我们不是耦合构造函数的情况,所以可以更简单地进行
private String eating = "Chomp, chomp, chomp";
private String barking = "Woof, woof, woof";
public Dog(String name, String breed, char sex, int age, double weight){
this.name = name;
this.breed = breed;
this.sex = sex;
this.age = age;
this.weight = weight;
}
我建议你在第一个构造器中设置进食和吠叫,并删除第二个构造器,因为任何狗都会发出相同的噪音,在我看来,这样的构造器是没有意义的
public Dog(String name, String breed, char sex, int age, double weight){
this.name = name;
this.breed = breed;
this.sex = sex;
this.age = age;
this.weight = weight;
this.eating = "Chomp, chomp, chomp";
this.barking = "Woof, woof, woof"
}
你真的应该能够自己弄清楚。getEating()返回什么?该字段何时初始化?你是否调用过初始化该字段的方法?这是你在阅读和分析代码时应该问自己的问题。嘿,你,你传递了吃东西和吠叫参数pass in Dog dog1=new Dog(“Luca”,“mutt”)“,‘M’,22,5);
public Dog(String name, String breed, char sex, int age, double weight){
this.name = name;
this.breed = breed;
this.sex = sex;
this.age = age;
this.weight = weight;
this.eating = "Chomp, chomp, chomp";
this.barking = "Woof, woof, woof"
}