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

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 C++实现更大的整数类
      • ¥15 Python怎么实现加减乘除乘方和阶乘呀
      • ¥15 centos8-arm64平台编译安装ray分布式计算框架。
      • ¥20 求帮做一个安卓app,用Android Studio制作。需要整个项目文件。
      • ¥15 使用深度学习对于楼梯尺寸进行检测
      • ¥15 特别是3和4 帮帮忙
      • ¥15 C++完成两个子类的构建,基类已给出
      • ¥66 求拉格朗日插值某点的值
      • ¥15 简易的机票预定信息系统
      • ¥15 C++实现对称图形小程序