JAVA实验题笔记 - 类的继承实验
747访客 279字under Notes of Programming tag Java Published on April 10th , 2020 at 05:33 pm
问题描述
设计名为Person的类和它的两个子类Student和Employee。Employee有子类Faculty和Staff。
- Person有name,address,telephone,email
- Student有grade(4个年级,定义为enum)
- Employee有officer,salary,entryDate(入职日期,MyDate类型)
- 定义MyDate类,包含year,month,day
- Faculty有officeHour,rank(级别)
- Staff有jobTitle(职称)
覆盖每个类中的toString方法显示相应的类名和人名。
编写测试程序,创建Person、Student、Employee、Faculty、Staff对象,并调用他们的toString方法。
实现对Student类的clone方法,要求创建stu01学生,并将其信息拷贝给stu02;修改stu01的姓名和年级,观察stu02学生的状态。
实验对Staff类的使用初始化块实现初始化工作,要求是所有的Staff类对象的职称都初始化为newStaff。
输出形式示例(部分)
Person [name=zhangsan]
Student [name=lisi]
Employee [name=wangwu]
Faculty [name=zhaoliu]
Staff [name=Tom]
Programing Time
package cn.matce.hw7;
public class PersonSystemTest {
public static void main(String[] args) {
Person zhangsan = new Person();
zhangsan.name = "zhangsan";
zhangsan.toString();
Student lisi = new Student();
lisi.name = "lisi";
lisi.toString();
Employee wangwu = new Employee ();
wangwu.name = "wangwu";
wangwu.toString();
Faculty zhaoliu = new Faculty ();
zhaoliu.name = "zhaoliu";
zhaoliu.toString();
Staff Tom = new Staff ();
Tom.name = "Tom";
Tom.toString();
System.out.print("\n\n");
//第二题的主方法从此开始↓
Student stu01 = new Student();
Student stu02 = new Student();
stu01.name = "Sherlock Holmes";
stu01.grade = Student.Grade.grade1;
System.out.printf("=====================================\n"
+ "Before clone"
+ "\n=====================================\n\n");
stu01.showInfo("stu01");
stu02.showInfo("stu02");
stu02.clone(stu01); System.out.printf("\n");
stu01.name = "Dr. Watson";
stu01.grade = Student.Grade.grade2;
System.out.printf("=====================================\n"
+ "After clone & stu01 changed its info"
+ "\n=====================================\n\n");
stu01.showInfo("stu01");
stu02.showInfo("stu02");
System.out.printf("=====================================\n"
+ "测试 jobTitle 初始化的部分"
+ "\n=====================================\n");
Staff staff01 = new Staff();
System.out.println(staff01.jobTitle + "\n\n");
System.out.printf("=====================================\n"
+ "测试 MyDate 类的使用"
+ "\n=====================================\n");
Employee Wang = new Employee();
Wang.entryDate.setMyDate(2020,4,10);
System.out.println("Wang入职于" + Wang.entryDate.getMyDate());
}
}
class Person {
String name = "No Name";
String address;
String telephone;
String email;
Person () {
}
@Override
public String toString() {
System.out.println(getClass().getSimpleName() + " [name=" + name + "]");
return null;
}
}
class Student extends Person {
enum Grade {
grade1,grade2,grade3,grade4,gradeUnSet
}
Grade grade = Grade.gradeUnSet;
//clone方法重写
public void clone(Student s1) {
name = s1.name;
address = s1.address;
telephone = s1.telephone;
email = s1.email;
grade = s1.grade;
}
public void showInfo(String sName) {
System.out.println(sName + "----------");
System.out.println("Name:" + name);
System.out.println("Grade:" + grade);
System.out.println();
}
}
class Employee extends Person { //难点笔记↓ 花了好几h才弄懂怎么解决【捂脸】
String officer; //职位
double salary; //薪水
MyDate entryDate; //定义MyDate类的变量entryDate,但此时指向Null
Employee() {
entryDate = new MyDate();
//构造MyDate对象,用entryDate这个变量指向新建的对象
}
}
class Faculty extends Employee{
double officeHour; //工作小时
int rank; //级别
}
class Staff extends Employee{
String jobTitle; //职称
{
jobTitle = "newStaff";
}
}
class MyDate {
private int year = 1970, month = 1, day = 1 ;
MyDate() {
}
public String getMyDate() {
return String.valueOf(year) + "年"
+ String.valueOf(month) + "月"
+ String.valueOf(day) + "日";
}
public void setMyDate(int y,int m,int d) {
year = y; month = m; day = d;
}
}
输出结果
Person [name=zhangsan]
Student [name=lisi]
Employee [name=wangwu]
Faculty [name=zhaoliu]
Staff [name=Tom]
=====================================
Before clone
=====================================
stu01----------
Name:Sherlock Holmes
Grade:grade1
stu02----------
Name:No Name
Grade:gradeUnSet
=====================================
After clone & stu01 changed its info
=====================================
stu01----------
Name:Dr. Watson
Grade:grade2
stu02----------
Name:Sherlock Holmes
Grade:grade1
=====================================
测试 jobTitle 初始化的部分
=====================================
newStaff
=====================================
测试 MyDate 类的使用
=====================================
Wang入职于2020年4月10日
本页由 matcha 整理和发布,采用 知识共享署名4.0 国际许可协议进行许可,转载前请务必署名
文章最后更新时间为:October 28th , 2020 at 04:28 am
分享到:Twitter Weibo Facebook