实例需求:实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一 个类Add()用于涨工资,普通员工一次能涨10%,经理能涨20%。
具体要求如下:
(1)Person类中的属性有:姓名name String(类型),地址address(String类型),定义该类的构造方法;(2)Employee 类中的属性有:工号ID(String类型),工资wage(double类型),工龄(int 型),定义该类的构造方法;
(3)Manager类中的属性有:级别level(String类型)定义该类的构造方法;
(4)编写一个测试类,产生一个员工和一个经理,给该员工和经理涨工资。
1、利用继承的特性
当子类没有相应的成员方法时,调用父类的同名方法。
Person.java
package person_employee;
public class Person {
private String name = "";
private String address = "";
//定义构造方法
public Person(String name, String address){
this.name = name;
this.address = address;
}
}
Employee.java
package person_employee;
// 继承Person类
public class Employee extends Person {
private String ID = "";
private double wage = 0;
private int age = 0;
public Employee(String name, String address, String ID, double wage, int age){
super(name, address); //调用父类的构造方法
this.ID = ID;
this.wage = wage;
this.age = age;
}
public double add(String position){// 判断职位
if(position == "0"){
wage = wage*1.2;
}
else{
wage = wage*1.1;
}
return wage;
}
//设置get/set方法
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
}
Manager.java
package person_employee;
//继承Employee类
public class Manager extends Employee{
private String level = ""; //1为普通员工,0为经理
public Manager(String name, String address, String ID, double wage, int age, String level){
super(name, address, ID, wage, age);
this.level = level;
}
//设置get/set方法
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
Test.java
package person_employee;
public class Test {
public static void main(String[] args) {
// 新建对象
Manager normal = new Manager("wsl", "jit", "12", 1000, 2, "1");
Manager manager = new Manager("ctl", "jitt", "123", 10000, 10, "0");
// 传入职位,Manager类型的对象没有add()方法,所以自动搜寻其父类,调用add()方法
normal.add(normal.getLevel());
manager.add(manager.getLevel());
System.out.println("normal wage is:"+normal.getWage());
System.out.println("manager wage is:"+manager.getWage());
}
}
打印结果:
打印结果
2、定义抽象类
定义抽象类,在子类中实现不同的抽象方法。
Person.java
package person_employee;
public class Person {
private String name = "";
private String address = "";
//定义构造方法
public Person(String name, String address){
this.name = name;
this.address = address;
}
}
Employee.java
package person_employee;
public abstract class Employee extends Person {
private String ID = "";
private double wage = 0;
private int age = 0;
public Employee(String name, String address, String ID, double wage, int age){
super(name, address);
this.ID = ID;
this.wage = wage;
this.age = age;
}
//定义抽象方法
public abstract void add(String position);
//设置get/set方法
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
}
Manager.java
package person_employee;
public class Manager extends Employee{
private String level = "";
public Manager(String name, String address, String ID, double wage, int age, String level){
super(name, address, ID, wage, age);
this.level = level;
}
//实现抽象方法
public void add(String position){
double wage = super.getWage();
if(position == "0"){
super.setWage(wage*1.2);
}
else{
super.setWage(wage*1.1);;
}
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
Test.java
package person_employee;
public class Test {
public static void main(String[] args) {
Manager normal = new Manager("wsl", "jit", "12", 1000, 2, "1");
Manager manager = new Manager("ctl", "jitt", "123", 10000, 10, "0");
normal.add(normal.getLevel());
manager.add(manager.getLevel());
System.out.println("normal wage is:"+normal.getWage());
System.out.println("manager wage is:"+manager.getWage());
}
}
打印结果一样。
3、在2的基础上利用多态实现方法的重载
Manager.java
package person_employee;
public class Manager extends Employee{
private String level = "";
public Manager(String name, String address, String ID, double wage, int age, String level){
super(name, address, ID, wage, age);
this.level = level;
}
//实现抽象方法
public void add(){
double wage = super.getWage();
super.setWage(wage*1.1);
}
public void add(String position){
double wage = super.getWage();
super.setWage(wage*1.2);
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
Test.java
package person_employee;
public class Test {
public static void main(String[] args) {
Manager normal = new Manager("wsl", "jit", "12", 1000, 2, "1");
Manager manager = new Manager("ctl", "jitt", "123", 10000, 10, "0");
normal.add();
manager.add(manager.getLevel());
System.out.println("normal wage is:"+normal.getWage());
System.out.println("manager wage is:"+manager.getWage());
}
}
打印结果一样
网友评论