谁帮我修改下这三个类

来源:百度知道 编辑:UC知道 时间:2024/05/22 10:25:59
父类
public class Animal {
public String sex;
public int height;
public int weight;
public int age;

public Animal(String a, int b,int c,int d) {
a= sex;
b= height;
c= weight;
d= age;
}

public void ok() {
System.out.println("Sex= "+sex+"Height= "+height+"Weight= "+weight+"Age= "+age);
}

}
子类
class Cat extends Animal{
public String sound;

public cat(String a,int b,int c,int d,String e){
super.Animal(sex,height,weight,age);
e= sound;
}

public void yes(){
System.out.println("Sex= "+sex+"Height= "+height+"Weight= "+weight+"Age= "+age+"Sound= "+sound);
}

}
主控
public class C{
public static void main(String[]args){

Cat h=new Cat("female",10,12,4,"miao miao mia

构造方法的赋值语句写反了。
要把 成员变量写在左边,参数写右边。
public Animal(String a, int b,int c,int d) {
sex=a;
height=b;
weight=c;
age=d;
}
后面那个Cat类一样改 ,不要调用父类带参数的构造函数,要调用无参数的构造函数,或不写也可以

public class Animal {
public String sex;

public int height;

public int weight;

public int age;

public void Animal(String a, int b, int c, int d) {
sex = a;
height = b;
weight = c;
age = d;
}

public void ok() {
System.out.println("Sex= " + sex + "Height= " + height + "Weight= "
+ weight + "Age= " + age);
}

}

class Cat extends Animal {
public String sound;

public Cat(String a, int b, int c, int d, String e) {
super.Animal(sex, height, weight, age);
e = sound;
}

public void yes() {
System.out.println("Sex= " + sex + "