Java

[Java] 4.多型

每次看到java 都會想到老闆在講那句 “你沒有多型的觀念” “你沒有多型的觀念” “你沒有多型的觀念” $$^%$#%#$.

雖然不是講給我聽得拉 不過我覺得講給我聽也不為過

先來一段程式碼 然後 這篇就結束了  多型我不會…


public class FullTimeEmployee extends Employee {
    private double monthlySalary;  //月薪
    
    public void display() {
       super.display();
       System.out.println("月薪=" +  monthlySalary);
    }

    public FullTimeEmployee (int empno , String ename , double monthlySalary ) {
          super(empno, ename);
          this.monthlySalary = monthlySalary ;
    }
    
    //add
    public double getSalary() { return monthlySalary; }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class PartTimeEmployee extends Employee {
     private double hourPay; //時薪
     private int workHour;   //工作時數 

     public void display() {
       super.display();
       System.out.println("hour pay=" + hourPay);
       System.out.println("work hour=" + workHour);
     }


     public PartTimeEmployee(int empno , String ename , double hourPay, int workHour) {
          super(empno, ename);
          this.hourPay = hourPay;
          this.workHour = workHour;
     }

     //add
     public double getSalary() {
          return hourPay * workHour;
     }
 }
 -----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Manager extends FullTimeEmployee {
    private double bonus;  //獎金; 額外津貼; 特別補助
    
    public void display() {
       super.display();
       System.out.println("額外津貼=" +  bonus);
    }

    public Manager (int empno , String ename , double monthlySalary, double bonus) {
          super(empno, ename, monthlySalary);
          this.bonus = bonus ;
    }
    
    //add
    public double getSalary() {
    	 double monthlySalary = super.getSalary();
       return monthlySalary + bonus;
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Employee {
    private int empno;
    private String ename;
    
    public void setEmpno(int empno) {
    	this.empno = empno;
    }
    public int getEmpno() {
    	return empno; 
    } 
    
    public void setEname(String ename) {
    	this.ename = ename;
    }
    public String getEname() {
    	return ename; 
    }
    
    
    public Employee (int empno, String ename) {
           this.empno = empno;
           this.ename = ename;
    }
     
    public Employee (int empno) {
       this(empno, "-");
    }
    
    public Employee (String ename) {
       this(0, ename);
    }
 
    public Employee () {
    	 this(0,"-");   //或  empno = 0; ename = "-";
    }
    
    
    public void display() {
         System.out.println("empno=" + empno);
         System.out.println("ename=" + ename); 
    }
    
    //add
    public double getSalary() { return 0; }
}
-------------------------------------------------------------------------------------
public class TestPolymorphism2 { 
  public static void main(String[] args) {  
    
//     Employee[] e = new Employee[3];  //宣告員工陣列,準備置入員工3人
//     e[0] = new FullTimeEmployee(7002 ,"peter", 40000.0 );
//     e[1] = new Manager(7003 ,"merry", 50000.0 , 10000.0);
//     e[2] = new PartTimeEmployee(7004 , "John" , 1000.0, 8);
//     for (int i = 0; i < e.length; i++)
//           System.out.println(e[i].getSalary());
 
     
//    instanceof測試 
//    for (int i = 0; i < e.length; i++)
//    if(e[i] instanceof FullTimeEmployee)         //Employee , FullTimeEmployee , Manager , PartTimeEmployee
//		   System.out.println("yes");
//	  else 
//		   System.out.println("no");

  

//     另外第二種寫法
//     Employee[] e = new Employee[3];
//     Employee e0 = new FullTimeEmployee(7002 ,"peter", 40000.0 );
//     Employee e1 = new Manager(7003 ,"merry", 50000.0 , 10000.0);
//     Employee e2 = new PartTimeEmployee(7004 , "John" , 1000.0, 8);
//     e[0] = e0;
//     e[1] = e1;
//     e[2] = e2;
//     for (int i = 0; i < e.length; i++)
//           System.out.println(e[i].getSalary());
         


//     再另第三種寫法
  
     FullTimeEmployee e0 = new FullTimeEmployee(7002 ,"peter", 40000.0 );
     Manager e1 = new Manager(7003 ,"merry", 50000.0 , 10000.0);
     PartTimeEmployee e2 = new PartTimeEmployee(7004 , "John" , 1000.0, 8);
     System.out.println(e0.getSalary());
     System.out.println(e1.getSalary());
     System.out.println(e2.getSalary());
     

  
  }
}
Be the First to comment.

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

(若看不到驗證碼,請重新整理網頁。)