class DuoTaiTest {
public static void main(String[] args) {
// Employee e = new Coder("大牛",3000,"838838",180.8);
// Coder c = (Coder)e;
// System.out.println(e.getName()+"....."+e.getSalary()+"...."+e.getId()+"...."+c.getHeight());
// c.Coding();//强转后调用子类的特有功能
// e.study();
// e.eat();//c.eat();//这里是既可以用父类应用调用静态的eat()方法,也可以用强转后的c调用eat()方法
//
//
// Bonus b = new Manager();
// b.getBonus();
// Manager m = (Manager)b;
// m.study();
method(new Coder());//多态,创建子类对象,传到Employe 中
method(new Manager());
}
public static void method(Employee e){
if(e instanceof Coder){
Coder c = (Coder)e;
c.Coding();
}else if(e instanceof Manager){
Manager m = (Manager)e;
m.getBonus();
}else{e.study();}
}
}
//定义员工类
abstract class Employee {
private String name;
private double salary;
private String id;
public Employee(){}//无参构造
public Employee(String name,double salary,String id ){//有参构造
this.name = name;
this.salary = salary;
this.id = id;
}
public void setName(String name){//设置姓名
this.name = name;
}
public String getName(){
return name;
}
public void setSalary(double salary){//设置薪水
this.salary = salary;
}
public double getSalary(){
return salary;
}
public void setId(String id){//设置工号
this.id = id;
}
public String getId(){
return id;
}
public abstract void study();
public static void eat(){
System.out.println("大家一起吃饭");
}
}
//定义程序员类
class Coder extends Employee{
private double height;//设置子类特有属性身高
public Coder(){} //无参构造
public Coder(String name,double salary,String id,double height ){//子类的有参构造
super(name,salary,id);
this.height = height;
}
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height;
}
public void study(){
System.out.println("程序员学习代码");
}
public void Coding(){
System.out.println("程序员苦逼敲代码");
}
}
//定义经理类继承员工实现薪水接口
class Manager extends Employee implements Bonus{
public Manager(){}
public Manager(String name,double salary,String id ){
super(name,salary,id);
}
//复写接口getBonus()方法
public void getBonus(){
System.out.println("项目经理happy拿奖金");
}
public void study(){
System.out.println("经理学习管理知识");
}
//复写接口//复写接口throwBonus()方法
public void throwBonus(){
System.out.println("项目经理把奖金给别人了");
}
}
//定义接口
interface Bonus{
String bonus = "38388";
public abstract void getBonus();
public abstract void throwBonus();
} |
|