[Java] 6.4. 多型

多型,繼承下物件的多種型別。

 

image/svg+xml繼承,讓程式設計師、測試⼈員或美⼯⼈員皆是 “IS-A” 員⼯ 轉型,並不會改變改寫的事實 6.4. 多型 - Polymorphism class Person { // 略過⼀些屬性與⽅法 } class Employee extends Person { // 略過⼀些屬性與⽅法 double getBonus (){ return ( 0.1 * salary); } } class Tester extends Employee { void test (){ System.out.println( " " + name + " 正在測試系統 " ); System.out.println( "Testing By" + name); System.out.println( " " + name + " 終於測試完了 " ); } } class Programmer extends Employee { void code (){ System.out.println( " " + name + " 正在寫程式 " ); System.out.println( "Coding By" + name); System.out.println( " " + name + " 終於寫完程式了 " ); } void debug (){ System.out.println( " " + name + " 正在除錯 " ); System.out.println( "Debugging By" + name); System.out.println( " " + name + " 終於抓出蟲了 " ); } } class Artist extends Employee { void draw (){ System.out.println( " " + name + " 正在畫圖畫 " ); System.out.println( "Drawing By" + name); System.out.println( " " + name + " 終於畫完了 " ); } } class Programmer extends Employee { // 略過⼀些程式 void show (){ System.out.println( " 我是程式設計師 " ); super .show(); } double getBonus (){ return salary * 3 ; } } class Tester extends Employee { // 略過⼀些程式 void show (){ System.out.println( " 我是測試⼈員 " ); super .show(); } double getBonus (){ return salary * 1.5 ; } } class Artist extends Employee { // 略過⼀些程式 void show (){ System.out.println( " 我是美⼯⼈員 " ); super .show(); } double getBonus (){ return salary * 2 ; } } class Main { public static void main( String [] args){ Programmer p = new Programmer (); Tester t = new Tester (); Artist a = new Artist (); p.salary = 30000 ; t.salary = 30000 ; a.salary = 30000 ; System.out.println( p.getBonus ()); System.out.println( t.getBonus ()); System.out.println( a.getBonus ()); } } 執⾏結果 90000 45000 60000 class Main { public static void main( String [] args){ Tester t = new Tester (); Artist a = new Artist (); Programmer p = new Programmer (); } } class Main { public static void main( String [] args){ Programmer p = new Programmer (); Tester t = new Tester (); Artist a = new Artist (); p.name = "Jack" ; p.name = "Eric" ; p.name = "Marry" ; Employee e1 = p ; Employee e2 = t ; Employee e3 = a ; } } class Main { public static void main( String [] args){ Programmer p = new Programmer (); Tester t = new Tester (); Artist a = new Artist (); p.name = "Jack" ; p.name = "Eric" ; p.name = "Marry" ; Employee e1 = p ; Employee e2 = t ; Employee e3 = a ; e1 . code (); e2 . test (); e3 . draw (); } } 編譯時發⽣錯誤 Main.java:15: error: cannot find symbol e1.code(); ^ symbol: method code() location: variable e1 of type Employee Main.java:16: error: cannot find symbol e2.test(); ^ symbol: method test() location: variable e2 of type Employee Main.java:17: error: cannot find symbol e3.draw(); ^ symbol: method draw() location: variable e3 of type Employee 3 errors class Main { public static void main( String [] args){ Programmer p = new Programmer (); p.id = "k123456789" ; p.name = "jack" ; p.gender = " 男⽣ " ; p.setAge( 25 ); p.employeeID = "048679" ; p.salary = 40000 ; Employee e = p ; Programmer p1 = (Programmer) e ; p1 . code (); p1 . debug (); } } class Main { public static void main( String [] args){ Programmer p = new Programmer (); p.id = "k123456789" ; p.name = "jack" ; p.gender = " 男⽣ " ; p.setAge( 25 ); p.employeeID = "048679" ; p.salary = 40000 ; Employee e = p ; e . show (); } } 執⾏結果 我是程式設計⼈員 我的⾝份證字號是 k1234567890 姓名是 jack 性別是男⽣ 年齡是 25 員⼯編號是 048679 薪資是 40000 class Boss { void payBonus ( Employee [] employees ){ for ( Employee e : employees ) System.out.println( 員⼯ + e.name + ", 得到獎⾦ " + e . getBonus ()); } } class Main { public static void main( String [] args){ Programmer p = new Programmer (); Tester t = new Tester (); Artist a = new Artist (); p.name = "Jack" ; p.salary = 40000 ; t.name = "Eric" ; t.salary = 40000 ; a.name = "Sala" ; a.salary = 40000 ; Employee [] allEmployees = { p , t , a }; Boss b = new Boss (); b . payBonus ( allEmployees ); } } 執⾏結果 120000    60000     80000     class Person { // 略過⼀些程式 String name ; // 略過⼀些程式 } class Employee extends Person { String employeeID; int salary; String name ; // 略過⼀些程式 } class Main { public static void main( String [] args){ Employee e = new Employee (); e . name = " jack " ; Person p = e ; p . name = " jack wallence " ; System.out.println( e . name ); System.out.println( p . name ); } } 執⾏結果 jack jack wallence J6_4_1 – Person.java 1. 程式設計師( Programmer )繼承⾃員⼯類別( Employee )。 J6_4_2 – Ar 4 st.java J6_4_1 – Employee.java J6_4_1 – Tester.java 1. 在此有個⼈員類別( Person )。 2. 這是員⼯類別( Employee )其繼承⾃⼈員類別。 3. 為員⼯加上獎⾦額度⽅法 getBonus() )。預設是薪⽔的 10% 4. 測試⼈員類別( Tester ),其擴充⾃員⼯類別。 5. 測試⽅法( test() )。 J6_4_1 – Programmer.java 6. 程式設計師類別( Programmer ),其是員⼯的⼀種。 7. 寫程式⽅法( code() )。 7. 除錯⽅法( debug() )。 J6_4_1 – Ar 4 st.java 9. 美⼯⼈員類別( Artist ),其也是擴充⾃員⼯類別。 10. 繪圖⽅法( draw() )。 J6_4_2 – Programmer.java 2. 為程式設計師所寫⾃我介紹⽅法 show() ),表明是程式設計師的⾝份。 3. 改寫獎⾦額度⽅法( getBonus() ), 3 倍薪資滿⾜程式設計師應得的獎⾦。 4. 測試⼈員( Tester )繼承⾃員⼯類別。 J6_4_2 – Tester.java 5. 為測試⼈員所寫⾃我介紹⽅法 show() ),表明為測試⼈員的⾝份。 6. 改寫獎⾦額度⽅法( getBonus() ), 1.5 倍作薪資作為測試⼈員應得的獎⾦。 7. 美⼯⼈員( Artist )繼承⾃員⼯類別。 8. 為美⼯⼈員所寫⾃我介紹⽅法 show() ),表明為美⼯⼈員的⾝份。 9. 改寫獎⾦額度⽅法( getBonus() ), 2 倍薪資作為美⼯⼈員應得的獎⾦。