继承的练习

定义一个学生类.是一个父类,包含学生的基础信息,可以沿用上午案例的代码。

定义一个学习的方法。

在子类(小学生,中学生,大学生)中对学习的方法重写.

定义测试类,测试不同学生对象的学习方法.

package com.etc.day01.lianxi2;

public class Student {
    private int sno;
    private String name;
    private String sex;

    public Student() {
    }

    public Student(int sno, String name, String sex) {
        this.sno = sno;
        this.name = name;
        this.sex = sex;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    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 void study(){
        System.out.println( "学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex()+"我的学习方法" );

    }

    public void sports(){
        System.out.println("学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex() + "我的运动方法");

    }
}
package com.etc.day01.lianxi2;

public class MinStudent extends Student{
    public MinStudent() {
    }

    public MinStudent(int sno, String name, String sex) {
        super(sno, name, sex);
    }

    @Override
    public void study() {
        System.out.println("学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex()+"我的学习方法是开心的学");
    }

    @Override
    public void sports() {
        System.out.println("学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex() + "我的运动方法是死命的玩");

    }
}
package com.etc.day01.lianxi2;

public class MaxStudent extends Student{
    public MaxStudent() {
    }

    public MaxStudent(int sno, String name, String sex) {
        super(sno, name, sex);
    }

    @Override
    public void study() {
        System.out.println("学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex()+"我的学习方法是不用学");

    }

    @Override
    public void sports() {
        System.out.println("学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex() + "我的运动方法是死命的玩");

    }
}
package com.etc.day01.lianxi2;

public class BigStudent extends Student{
    public BigStudent() {
    }

    public BigStudent(int sno, String name, String sex) {
        super(sno, name, sex);
    }

    @Override
    public void study() {
        System.out.println("学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex()+"我的学习方法是死命的学");

    }

    @Override
    public void sports() {
        System.out.println("学号:"+ getSno() + "姓名:" + getName() + "性别" + getSex() + "我的运动方法是不敢玩");
    }
}
package com.etc.day01.lianxi2;

public class Demo1 {
    public static void main(String[] args) {
        MinStudent minStudent = new MinStudent(20230101,"张三","女");
        minStudent.study();
        minStudent.sports();
        BigStudent bigStudent = new BigStudent(20230103,"李四","女");
        bigStudent.study();
        bigStudent.sports();
        MaxStudent maxStudent = new MaxStudent(20230104,"王五","女");
        maxStudent.study();
        maxStudent.sports();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值