我必须创建一个狗对象并赋予它属性和行为。我已经做了属性,但行为没有按我想要的那样工作。我收到空。
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 为“空”
手掌心
慕运维8079593
白衣非少年
隔江千里
三国纷争
相关分类