一、创建带有继承结构的标准JavaBean类(1)
public class Worker {private String name;private int workid;private int salary;public Worker(){}public Worker(String name,int workid,int payment){this.name=name;this.salary=payment;this.workid=workid;}public void eat(){System.out.println("吃米饭");}public void setName(String name) {this.name = name;}public void setPayment(int payment) {this.salary = payment;}public void setWorkid(int workid) {this.workid = workid;}public int getPayment() {return salary;}public int getWorkid() {return workid;}public String getName() {return name;}public void work(){System.out.println("正在工作");}
}
public class Manager extends Worker {private int managepayment;public Manager(){}public Manager(String name,int workid,int payment,int managepayment){//带全部参数的构造 (父类+子类)super(name,workid,payment);this.managepayment=managepayment;}public int getManagepayment() {return managepayment;}public void setManagepayment(int managepayment) {this.managepayment = managepayment;}public void work(){System.out.println("管其他人");}}
public class Cook extends Worker {public Cook(){super();}public Cook(String name,int workid,int payment){super(name,workid,payment);}public void work(){System.out.println("炒菜");}}
import java.util.ArrayList;public class TestDemo {public static void main(String[] args) {Manager manager=new Manager("张三",001,8000,800);System.out.println(manager.getName()+","+manager.getWorkid()+","+manager.getPayment()+","+ manager.getManagepayment());//张三,1,8000,800manager.eat(); //吃米饭manager.work();//管其他人Cook cook=new Cook("李四",002,6000);System.out.println(cook.getName()+","+cook.getWorkid()+","+cook.getPayment()); //李四,2,6000cook.work(); // 炒菜cook.eat(); // 吃米饭}
}
二、创建带有继承结构的标准JavaBean类(2)
import java.util.ArrayList;public class TestDemo {public static void main(String[] args) {//公司中很多员工,按照工作内容不同分为教研部员工(Teacher)和行政部员工(AdminStaff)//1.教研部根据教学方式不同分为讲师(Lecturer)和助教(Tutor)//2.行政部根据负责事项不同,分为维护专员(Maintainer)和采购专员(Buyer)//3.公司每个员工都有编号,姓名和负责的工作//4.每个员工都有工作的功能,但是具体的工作内容不同Buyer buyer1=new Buyer("行政部001","张三");Maintainer maintainer1=new Maintainer("行政部002","李四");Lecturer lec1=new Lecturer("教研部001","王五");Tutor tutor1=new Tutor("教研部002","陈六");System.out.println(buyer1.getName()+","+buyer1.getNumber());System.out.println(maintainer1.getName()+","+maintainer1.getNumber());System.out.println(lec1.getName()+","+lec1.getNumber());System.out.println(tutor1.getName()+","+tutor1.getNumber());}
}
public class Employee {private String number;private String name;public Employee(){}public Employee(String number,String name){this.name=name;this.number=number;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNumber(){return number;}public void setNumber(String number) {this.number = number;}public void work(){System.out.println("工作");}
}
public class AdminStaff extends Employee{@Overridepublic void work() {System.out.println("行政工作");}public AdminStaff(){super();}public AdminStaff(String number,String name){super(number,name);}}public class Teacher extends Employee{@Overridepublic void work(){System.out.println("教研工作");}public Teacher(){super();}public Teacher(String number,String name){super(number,name);}
}public class Buyer extends AdminStaff{@Overridepublic void work(){System.out.println("采购工作");}public Buyer(){super();}public Buyer(String number,String name){super(number,name);}
}public class Maintainer extends AdminStaff{@Overridepublic void work() {System.out.println("维护工作");}public Maintainer(){}public Maintainer(String number,String name){super(number,name);}
}public class Tutor extends Teacher{@Overridepublic void work() {System.out.println("助教工作");}public Tutor(){}public Tutor(String number,String name){super(number,name);}
}public class Lecturer extends Teacher{@Overridepublic void work(){System.out.println("讲课工作");}public Lecturer(){}public Lecturer(String number,String name){super(number,name);}
}