韩银虎
2021-05-07 22:15
采纳率: 66.7%
浏览 44
已采纳

Java抽象类问题中关于子类成员输出问题

package Day02Work;

public  class Athletes extends Person{
    private String banji;

    public Athletes() {
    }

    public Athletes(String id, String name, String sex, String age) {
        super(id, name, sex, age);
    }
    



    @Override
    public void eat() {
        System.out.println("运动员吃营养餐");
    }
}

package Day02Work;

public class Test {
    public static void main(String[] args) {
        // 无参
        Athletes athletes = new Athletes();
        athletes.setAge("18");
        athletes.setName("刘翔");
        athletes.setId("itheima01");
        athletes.setSex("女");
        System.out.println(athletes.getId() + ", " + athletes.getName() +
                ", " + athletes.getSex() + ", " + athletes.getAge());
        athletes.eat();

        // 有参
        Coach coach = new Coach("金龙", "itheima02", "男同", "20");
        System.out.println(athletes.getId() + ", " + athletes.getName() +
                ", " + athletes.getSex() + ", " + athletes.getAge());
        coach.eat();
    }
}

package Day02Work;

public abstract class Person {
    private String id;
    private String name;
    private String sex;
    private String age;

    public Person() {
    }

    public Person(String id, String name, String sex, String age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public abstract void eat();
}


如何在在Test类中输出抽象子类定义的String banji 这个成员变量?

求指点!

3条回答 默认 最新

相关推荐 更多相似问题