呼叫课程时收到null

时间:2019-07-27 06:55:44

标签: 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() + ", 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 class Dog{

    private String name;
    private String breed;
    private char sex;
    //In months
    private int age;
    //In pounds
    private double weight;
    private String eating;
    private String barking;

    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 eating, String barking){
        this.eating = "Chomp, chomp, chomp";
        this.barking = "Woof, woof, woof";
    }


    public String getName(){
        return name;
    }

    public String getBreed(){
        return breed;
    }

    public char getSex(){
        return sex;
    }

    public int getAge(){
        return age;
    }

    public double getWeight(){
        return weight;
    }

    public String getEating(){
        return eating;
    }

    public String getBarking(){
        return barking;
    }
}

我应该得到“当Dog1进食时发出的声音会发出刺耳的声音,发出刺耳的声音,而当它发出吠声时,发出的声音是Wo 、,、,”,但是我会收到“ Dog1进食时,它会使发出的声音为零,当它发出声音时,吠叫的声音是“空”

5 个答案:

答案 0 :(得分:4)

您打算做什么:

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())来设置这些值,因为它不会自动发生。

答案 1 :(得分:1)

这没有道理:

public Dog(String eating, String barking){
    this.eating = "Chomp, chomp, chomp";
    this.barking = "Woof, woof, woof";
}

参数不用于对字段进行赋值:实际上,您对具有一些编译时间常数值的字段进行赋值:“合唱,合唱,合唱”和“低音,低音,低音”。
根据您提到的问题,您认为Dogeating字段的任何barking都具有默认值:

  

我应该得到“当Dog1吃东西时,它会发出刺耳的声音,刺耳的声音,   而且当它吠叫时发出的声音是Woof,woof,woof”。

     

Dog dog1 = new Dog ("Luca", "mutt", '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;
}

答案 2 :(得分:0)

问题在于,第一个Dog构造函数创建了dog1对象

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;
}

饮食和吠叫字段未在此构造方法中初始化。您还应该调用第二个构造函数。

答案 3 :(得分:0)

在进食和吠叫字段中您将获得null,因为您正在调用该类的第一个构造函数,并且在这些字段中未分配任何值。 您需要从第一个构造函数调用第二个构造函数。

public Dog(String name, String breed, char sex, int age, double weight){
        this("", "");
        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, String eating, String barking){
        this("", "");
        this.name = name;
        this.breed = breed;
        this.sex = sex;
        this.age = age;
        this.weight = weight;
        this.eating = eating;
        this.barking = barking;
}

public Dog(String name, String breed, char sex, int age, double weight){
      this(name, breed, sex, age, weight, "", "");

}

答案 4 :(得分:0)

我建议您在第一个构造函数中设置进食和吠叫,并删除第二个构造函数,因为任何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;
    this.eating = "Chomp, chomp, chomp";
    this.barking = "Woof, woof, woof"


}