亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

調用類時接收 null

調用類時接收 null

函數式編程 2023-04-26 14:02:07
我必須創建一個狗對象并賦予它屬性和行為。我已經做了屬性,但行為沒有按我想要的那樣工作。我收到空。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 為“空”
查看完整描述

5 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

你打算做什么:


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())設置這些值,因為它不會自動發生。


查看完整回答
反對 回復 2023-04-26
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

這是沒有意義的:


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;

}


查看完整回答
反對 回復 2023-04-26
?
白衣非少年

TA貢獻1155條經驗 獲得超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"



}


查看完整回答
反對 回復 2023-04-26
?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

您在 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, "", "");


}


查看完整回答
反對 回復 2023-04-26
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

問題是 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 字段未在此構造函數中初始化。您還應該調用第二個構造函數。


查看完整回答
反對 回復 2023-04-26
  • 5 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號