目录
一、实验目的
1、了解和掌握类的构造方法,继承方法的相关知识。
2、能够使用类的继承及其应用解决实际应用问题,如设计一个雇员类,并实现雇员对象基本信息的管理。
二、雇员类设计实验的内容及步骤
实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,
(1)Person类中的属性有:姓名(name,String),地址(address,String),定义该类的构造方法。
(2)Employee类中的属性有:工号(ID,String),工资(wage,double),工龄(workingyears,int),级别(level,String),定义该类的构造方法;
(3)Manager类中的属性有:部门(department,String),定义该类的构造方法;
(4)要求使用方法的重写(人,员工,经理类信息的输出方法);要求实现类的构造方法(super关键字)
(5)编写一个测试类,产生一个员工和一个经理对象,输出员工和经理的基本信息;
(6)设计一个Add类用于涨工资,普通员工一次能涨10%,经理能涨20%。
(7)员工工龄超过3年的涨一次工资,部门经理工龄超过1年的涨一次工资。
三、实验代码
-
Person类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package employee_test;
/**
*
* @author caiqi
*/
public class Person {
private String stringName;
private String stringAddress;
public Person(String stringName, String stringAddress) {
this.stringName = stringName;
this.stringAddress = stringAddress;
}
public String getStringAddress() {
return stringAddress;
}
public void setStringAddress(String stringAddress) {
this.stringAddress = stringAddress;
}
public String getStringName() {
return stringName;
}
public void setStringName(String stringName) {
this.stringName = stringName;
}
@Override
public String toString() {
System.out.println(super.toString());
return "Person{" + "姓名=" + stringName + ", 地址=" + stringAddress + "};";
}
}
-
Employee类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package employee_test;
/**
*
* @author caiqi
*/
public class Employee extends Person{
private String ID;
private double wage;
private int workingyears;
private String level;
public Employee(String ID, double wage, int workingyears, String level, String stringName, String stringAddress) {
super(stringName, stringAddress);
this.ID = ID;
this.wage = wage;
this.workingyears = workingyears;
this.level = level;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public int getWorkingyears() {
return workingyears;
}
public void setWorkingyears(int workingyears) {
this.workingyears = workingyears;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
@Override
public String toString() {
System.out.println(super.toString());
return "Employee{" + "工号=" + ID + ", 工资=" + wage + "元, 工龄=" + workingyears + "年, 级别=" + level + "}";
}
}
-
Manager类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package employee_test;
/**
*
* @author caiqi
*/
public class Manager extends Employee{
private String department;
public Manager(String department, String ID, double wage, int workingyears, String level, String stringName, String stringAddress) {
super(ID, wage, workingyears, level, stringName, stringAddress);
this.department = department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
System.out.println(super.toString());
return "Manager{" + "部门=" + department + "}";
}
}
-
Add类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package employee_test;
/**
*
* @author caiqi
*/
public class Add {
String AddWageJg="工资没变";
public String AddWage(Employee M){
if (M.getLevel().equals("普通员工")){
if (M.getWorkingyears()>=3){
M.setWage(M.getWage()*1.1);
AddWageJg="工资涨了,现在的工资为"+M.getWage();
}
}
if (M.getLevel().equals("经理")){
if (M.getWorkingyears()>=1){
M.setWage(M.getWage()*1.2);
AddWageJg="工资涨了,现在的工资为"+M.getWage()+"元";
}
}
return AddWageJg;
}
}
-
Employee_Test类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package employee_test;
/**
*
* @author caiqi
*/
public class Employee_Test {
public static void main(String[] args) {
Employee EmployeeA=new Employee("22222", 3000.00, 2, "普通员工", "李四","中国上海");
Manager ManagerA=new Manager("市场营销部","111",8000.00,5,"经理","张三","中国台湾");
Add AddA=new Add();
System.out.println(EmployeeA.toString());
System.out.println("---------------------------------------------------------------------------------------");
System.out.println(ManagerA.toString());
System.out.println("---------------------------------------------------------------------------------------");
System.out.println(EmployeeA.getLevel()+EmployeeA.getStringName()+":"+AddA.AddWage(EmployeeA));
System.out.println(ManagerA.getLevel()+ManagerA.getStringName()+":"+AddA.AddWage(ManagerA));
}
}