猿问

调用类时接收 null

我必须创建一个狗对象并赋予它属性和行为。我已经做了属性,但行为没有按我想要的那样工作。我收到空。


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 吃东西时它发出噪音 Chomp、chomp、chomp,当它吠叫时发出的声音是 Woof、woof、woof”,但我得到“当 Dog1 吃东西时它发出噪音,当它吠叫时发出噪音” made 为“空”


函数式编程
浏览 127回答 5
5回答

手掌心

你打算做什么: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())设置这些值,因为它不会自动发生。

慕运维8079593

这是没有意义的:public Dog(String eating, String barking){    this.eating = "Chomp, chomp, chomp";    this.barking = "Woof, woof, woof";}参数不用于对字段进行赋值:实际上,您对具有一些编译时常量值的字段进行赋值:“Chomp, chomp, chomp”和“Woof, woof, woof”。根据您陈述的问题,您认为 any和字段Dog具有默认值: eatingbarking我应该得到“当 Dog1 吃东西时,它会发出咀嚼、咀嚼、咀嚼的声音,而当它吠叫时,发出的声音是汪汪汪汪汪汪”Dog dog1 = new Dog ("Luca", "mutt", 'M', 22, 5 );在这种情况下,一种更简单的方法是使用字段初始值设定项来为这些字段赋值。另一种方法:链接构造函数(在西蒙的回答中提供)也是正确的,但这里我们不是在耦合构造函数的情况下。所以你可以做得更简单。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;}

白衣非少年

我建议您在第一个构造函数本身中设置进食和吠叫并删除第二个构造函数,因为任何 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"}

隔江千里

您在 eating and barking 字段中得到 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, "", "");}

三国纷争

问题是 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 字段未在此构造函数中初始化。您还应该调用第二个构造函数。
随时随地看视频慕课网APP

相关分类

Java
我要回答