定义一个学生类.是一个父类,包含学生的基础信息,可以沿用上午案例的代码。
定义一个学习的方法。
在子类(小学生,中学生,大学生)中对学习的方法重写.
定义测试类,测试不同学生对象的学习方法.
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();
}
}