每次看到java 都會想到老闆在講那句 “你沒有多型的觀念” “你沒有多型的觀念” “你沒有多型的觀念” $$^%$#%#$.
雖然不是講給我聽得拉 不過我覺得講給我聽也不為過
先來一段程式碼 然後 這篇就結束了 多型我不會…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 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()); } } |