[Java] 纯文本查看 复制代码
//员工类
public abstract class Employee {
//姓名
private String name;
//工号
private String code;
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(String name, String code) {
super();
this.name = name;
this.code = code;
}
//get/set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
//工作方法
public abstract void work();
}
//教师类
public class Teacher extends Employee{
public Teacher() {
// TODO Auto-generated constructor stub
}
public Teacher(String name,String code) {
super(name,code);
}
//工作
public void work() {
System.out.println("工号为"+this.getCode()+"的"+this.getName()+"教师在授课");
}
}
public class Test {
public static void main(String[] args) {
Teacher t = new Teacher("1001","zhangsan");
t.work();
System.out.println("===========");
new Employee("1001","zhangsan") {
public void work() {
System.out.println("工号为"+this.getCode()+"的"+this.getName()+"教师在授课");
}
}.work();
}
}