2021-04-26

实验六  类的继承编程练习

一、实验目的

1.掌握父类的定义方法

2.掌握子类的定义方法

3.掌握继承的使用方式

二、实验硬、软件环境

Windows计算机和Eclipse软件

三、实验内容及步骤

1.假设教师工作证包括编号、姓名、性别、出生年月、职务和签发时间;学生证包括编号、姓名、性别、出生年月、系别、入校时间。请为教师工作证与学生工作证设计一个类结构,尽可能保证代码的复用率,并输出教师工作证和学生工作证信息。

首先创建一个父类,共性抽取老师和学生的信息:

public class Fu {

    private String num;

    private String name;

    private String sex;

    private String birth;

    private String date;

    public Fu() {

    }

    public Fu(String num, String name, String sex, String birth, String date) {

        this.num = num;

        this.name = name;

        this.sex = sex;

        this.birth = birth;

        this.date = date;

    }

    public String getNum() {

        return num;

    }

    public void setNum(String num) {

        this.num = num;

    }

    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 getBirth() {

        return birth;

    }

    public void setBirth(String birth) {

        this.birth = birth;

    }

    public String getDate() {

        return date;

    }

    public void setDate(String date) {

        this.date = date;

    }

}

教师类Teacher继承父类:

public class Teacher extends Fu {

    private String work;

    public Teacher() {

        this.work = work;

    }

    public Teacher(String num, String name, String sex, String birth, String date, String work) {

        super(num, name, sex, birth, date);

        this.work = work;

    }

    public String getWork() {

        return work;

    }

    public void setWork(String work) {

        this.work = work;

    }

}

学生类Student继承父类:

public class Student extends Fu {

    private String xb;

    public Student() {

        this.xb = xb;

    }

    public Student(String num, String name, String sex, String birth, String date, String xb) {

        super(num, name, sex, birth, date);

        this.xb = xb;

    }

    public String getXb() {

        return xb;

    }

    public void setXb(String xb) {

        this.xb = xb;

    }

}

通过主函数创造新对象,并通过全参构造传入各个参数:

public class School {

    public static void main(String[] args) {

        Teacher tc=new Teacher("9527","张三","男","1919.2.3","2021.2.4","书记");

        Student sd=new Student("9999","李四","女","2000.3.3","2021.3.4","计算机");

        System.out.println("教师工作证:"+"编号:"+tc.getNum()+" "+"姓名:"+tc.getName()

                           +" "+"性别:"+tc.getSex()+" "+"出生年月:"+tc.getBirth()

                           +" "+"职务:"+tc.getWork()+" "+"签发时间:"+tc.getDate());

        System.out.println("学生工作证:"+"编号:"+sd.getNum()+" "+"姓名:"+sd.getName()

                           +" "+"性别:"+sd.getSex()+" "+"出生年月:"+sd.getBirth()

                           +" "+"系别:"+sd.getXb()+" "+"入校时间:"+sd.getDate());

    }

}

2.定义一个人的类,属性有名字、年龄,写一个能输出各个属性值的方法showinfo()。定义一个学生类,属性有性别。学生类继承人类。

要求:

(1)父类的属性赋值用构造方法来实现(分别用有参数构造方法和无参数构造方法实现)。

(2)子类的属性也用构造方法来赋值。

(3)在子类中重写父类的showinfo()方法。

(4)声明学生类的对象,调用学生的显示信息的方法。

定义一个人的类Person:

public class Person {

    private String name;

    private int age;

    public Person() {

    }

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }

    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 void showinfo(){

        System.out.print("姓名:"+this.name+" "+"年龄:"+this.age);

    }

}

定义一个学生类StudentP,属性有性别,学生类继承人类:

public class StudentP extends Person{

    private String sex;

 

    public StudentP() {

        this.sex = sex;

    }

    public StudentP(String name, int age, String sex) {

        super(name, age);

        this.sex = sex;

    }

    public String getSex() {

        return sex;

    }

    public void setSex(String sex) {

        this.sex = sex;

    }

    @Override

    public void showinfo() {

        super.showinfo();

        System.out.print("性别:"+this.sex);

    }

}

使用学生类StudentP:

public class StudentS {

    public static void main(String[] args) {

        StudentP sp = new StudentP();

        sp.setName("张三");

        sp.setAge(22);

        sp.setSex("男");

        sp.showinfo();

    }

}

四、实验结果及分析

实验结果:

 

实验分析:

通过本次实验,掌握了父类的定义方法,子类的定义方法以及继承的使用方式。继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的属性和行为。extends是表示继承关系的关键字。

上一篇:Mybatis的动态SQL的语句


下一篇:avenue