编写父类
package com.zhao.test;
//父类
public class Employee {
private String empId;
private String name;
private double salary;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee(String empId, String name, double salary) {
this.empId = empId;
this.name = name;
this.salary = salary;
}
public Employee() {
}
//方法
public void work(){
System.out.println("在工作");
}
public void eat(){
System.out.println("吃米饭");
}
}
编写子类
package com.zhao.test;
public class Manager extends Employee{
private double bouns;
//带全部参数的构造
//父类+子类
public Manager(String empId, String name, double salary, double bouns) {
super(empId, name, salary);
this.bouns = bouns;
}
//空参构造
public Manager() {
this.bouns = bouns;
}
//get/set方法(子类中特有的)
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
//重写工作方法
@Override
public void work() {
System.out.println("管理其他人");
}
}
package com.zhao.test;
public class Cooker extends Employee{
public Cooker(String empId, String name, double salary) {
super(empId, name, salary);
}
public Cooker() {
}
//重写工作方法
@Override
public void work() {
System.out.println("炒菜");
}
}
标签:empId,salary,String,继承,bouns,练习,JavaSE,public,name
From: https://www.cnblogs.com/javaHane/p/17184469.html