java中关于继承的题目4

4.请用面向对象的思想,设计自定义类描述演员和运动员的信息 设定
1)演员类:
属性包括:姓名,年龄,性别,毕业院校,代表作 方法包括:自我介绍
2)运动员类:
属性包括:姓名,年龄,性别,运动项目,历史最好成绩 方法包括:自我介始
要求
3)分析演员和运动员的公共成员,提取出父类—人类
4)利用继承机制,实现演员类和运动员类

5)编写测试类,分别测试人类,演员类和运动员类对象及相关方法
6)定义名为 act 的包存人类,演员类,运动员类和测试类运行效果

编写父类

class People {
    private String name;
    private int age;
    private String sex;
    public People() {
    }
    public People(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

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

    //方法包括:自我介始
    public void Initiation(){
        System.out.println("我就是一个普通的老百姓");
    }
}

运动员类

class Sport extends People {
    private String score;
    private String project;
    public Sport() {  //有参构造
    }
    Sport(String name, int age, String project,String sex, String score){ //无参构造
        super(name,age,sex);
        this.project=project;
        this.score=score;

    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }


    public void Initiation(){  //自我介绍的方法,父类也有同样的方法,属于方法的重写
        System.out.println("大家好!我是"+getName()+"\n"+"今年"+getAge()+"\n"+"我擅长的运动项目是:"+project+"\n"+"历史最好成绩是:"+score);
    }
}

演员类

class Actor extends People {
    private String school;
    private String work;

    public Actor() {  //无参构造
    }

    Actor(String name, int age, String sex , String school, String work){ //有参构造
        super(name, age, sex);
        this.school=school;
        this.work=work;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }


    public void Initiation(){ //父类也有同样的方法,属于方法的重写
        System.out.println("大家好!我是"+getName()+"\n"+"今年"+getAge()+"\n"+"我毕业于:"+school+"\n"+"代表作有:"+"《"+work+"》");
    }
}

测试类

public class Test04 {
    public static void main(String[] args) {
        People p1 = new People();
        p1.Initiation();
        System.out.println("====================");
        Sport s1 = new Sport("刘小翔", 23, "两百米短跑","男", "22秒30");
        s1.Initiation();
        System.out.println("====================");
        Actor a1 = new Actor("吴总", 34, "男","宿州学院", "寄往天堂的书");
        a1.Initiation();
    }
}
posted @ 2024-09-25 10:22  你的镁偷走了我的锌  阅读(20)  评论(0)    收藏  举报