Java 继承练习

分析以下需求,并用代码实现
//注意:子类也叫做派生类
(1)定义一个人类Person

        a.包括属性:姓名name、性别gender、年龄age、国籍nationality;

        b.包括方法:吃饭eat、睡觉sleep,工作work。
(2)根据人类,派生一个学生类Student.

        a.增加属性:学校school、学号stuNumber;

        b.重写工作方法(学生的工作是学习)。

(3)根据人类,派生一个工人类Worker.

        a.增加属性:单位unit、工龄workAge;

        b.重写工作方法(工人的工作是盖房子)。

(4)根据学生类,派生一个学生干部类 StudentLeader.

        a.增加属性:职务job;

        b.增加方法:开会meeting。

(5)编写主函数分别对上述3类具体人物进行测试。
(6)要求运行结果:
        a.学生需要学习!
        b.工人的工作是盖房子!
        c.学生干部喜欢开会!

嗯…上代码,魔改版…

/*
    (1)定义一个人类Person

            a.包括属性:姓名name、性别gender、年龄age、国籍nationality;

            b.包括方法:吃饭eat、睡觉sleep,工作work。
 */
public class Person {
    private String name;
    private String gender;
    private int  age;
    private String  nationality;

    public Person() {
    }

    public Person(String name, String gender, int age, String nationality) {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.nationality = nationality;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

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

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public void eat(){
        System.out.println("吃饭");
    }
    public void sleep(){
        System.out.println("睡觉");
    }
    public void work(){
        System.out.println("工作");
    }
}

/*
    (2)根据人类,派生一个学生类Student.

            a.增加属性:学校school、学号stuNumber;

            b.重写工作方法(学生的工作是学习)。
 */
public class Student extends  Person{
    private String school;
    private String stuNumber;


    public Student(String name, String gender, int age, String nationality, String school, String stuNumber) {
        super(name, gender, age, nationality);
        this.school = school;
        this.stuNumber = stuNumber;
    }

    public void work(){
        System.out.println("好好学习,天天向上");
    }
    public void show(){
        System.out.println("姓名:"+super.getName()+",性别:"+super.getGender()+",年龄:"+super.getAge()+",国籍:"
                +super.getNationality()+",学校:"+this.getSchool()+",学号:"+this.getStuNumber());
        work();
    }

    public String getSchool() {
        return school;
    }

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

    public String getStuNumber() {
        return stuNumber;
    }

    public void setStuNumber(String stuNumber) {
        this.stuNumber = stuNumber;
    }
}

/*
    (3)根据人类,派生一个工人类Worker.

            a.增加属性:单位unit、工龄workAge;

            b.重写工作方法(工人的工作是盖房子)。
 */
public class Worker extends  Person{
    private String unit;
    private int workAge;

    public Worker(String name, String gender, int age, String nationality, String unit, int workAge) {
        super(name, gender, age, nationality);
        this.unit = unit;
        this.workAge = workAge;
    }
    public void work(){
        System.out.println("特长:九阴白骨爪");
    }
    public void show(){
        System.out.println("姓名:"+super.getName()+",性别:"+super.getGender()+",年龄:"+super.getAge()+",国籍:"
                +super.getNationality()+",单位:"+this.unit+",工龄:"+this.workAge);
        work();
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public int getWorkAge() {
        return workAge;
    }

    public void setWorkAge(int workAge) {
        this.workAge = workAge;
    }
}

/*
    (4)根据学生类,派生一个学生干部类 StudentLeader.

            a.增加属性:职务job;

            b.增加方法:开会meeting。
 */
public class StudentLeader extends  Student{
    private String job;

    public StudentLeader(String name, String gender, int age, String nationality,
                         String school, String stuNumber, String job) {
        super(name, gender, age, nationality, school, stuNumber);
        this.job = job;
    }
    public void meeting(){
        System.out.println("特长:降龙十八掌");
    }
    public void show(){
        System.out.println("姓名:"+super.getName()+",性别:"+super.getGender()+",年龄:"+super.getAge()+",国籍:"
        +super.getNationality()+",学校:"+super.getSchool()+",学号:"+super.getStuNumber()+",职务:"+this.job);
        meeting();
    }
}

最后测试类:

/*
     测试类
 */
public class text {
    public static void main(String[] args) {
        StudentLeader st = new StudentLeader("郭靖","男",25,"中国",
                "射雕英雄传","15","武林盟主");
        st.show();

        Worker w = new Worker("林超风","女",30,"中国", "桃花岛",28);
        w.show();


        Student s = new Student("小明","男",8,"课本","语文课本","150");
        s.show();

    }

}

测试结果:
Java 继承练习_第1张图片

嗯…觉得加上吃饭睡觉不文雅,就没有调用那个功能。

你可能感兴趣的:(Java基础练习)