1. 程式人生 > 實用技巧 >24-面向物件的特徵三:多型性

24-面向物件的特徵三:多型性

1.多型性的理解

  可以理解為一個事物的多種形態。


2.何為多型性:

  物件的多型性:父類的引用指向子類的物件(或子類的物件賦給父類的引用)


舉例:

Person p = new Man();
Object obj = new Date();

3.多型性的使用:虛擬方法呼叫
  > 有了物件的多型性以後,我們在編譯期,只能呼叫父類中宣告的方法,但在執行期,我們實際執行的是子類重寫父類的方法。


  > 總結:編譯,看左邊;執行,看右邊。


4.多型性的使用前提:
  ① 類的繼承關係   ② 方法的重寫

5.多型性的應用舉例:

 1 //舉例一:
 2 public void func(Animal animal){//
Animal animal = new Dog(); 3 animal.eat(); 4 animal.shout(); 5 } 6 //舉例二: 7 public void method(Object obj){ 8 9 } 10 //舉例三: 11 class Driver{ 12 13 public void doData(Connection conn){//conn = new 14 MySQlConnection(); / conn = new OracleConnection(); 15 //規範的步驟去操作資料
16 // conn.method1(); 17 // conn.method2(); 18 // conn.method3(); 19 } 20 }           21

6.多型性使用的注意點:
  物件的多型性,只適用於方法,不適用於屬性(編譯和執行都看左邊)

************************************************************
7.關於向上轉型與向下轉型:
  7.1 向上轉型:多型
  7.2 向下轉型:
    7.2.1 為什麼使用向下轉型:
        有了物件的多型性以後,記憶體中實際上是載入了子類特有的屬性和方法的,但是由於變數宣告為父類型別,導致編譯時,只能呼叫父類中宣告的屬性和方法。

        子類特有的屬性和方法不能呼叫。如何才能呼叫子類特的屬性和方法?使用向下轉型。


    7.2.2 如何實現向下轉型:
      使用強制型別轉換符:()


    7.2.3 使用時的注意點:
      ① 使用強轉時,可能出現ClassCastException的異常。


      ② 為了避免在向下轉型時出現ClassCastException的異常,我們在向下轉型之前,先進行instanceof的判斷,一旦返回true,就進行向下轉型。如果返回false,不進行向下轉型。

    7.2.4 instanceof的使用:
      ① a instanceof A:判斷物件a是否是類A的例項。如果是,返回true;如果不是,返回false。
      ② 如果 a instanceof A返回true,則 a instanceof B也返回true.其中,類B是類A的父類。
      ③ 要求a所屬的類與類A必須是子類和父類的關係,否則編譯錯誤。


    7.2.5 圖示:

8. 面試題:

  8.1 談談你對多型性的理解?
    ① 實現程式碼的通用性。
    ② Object類中定義的public boolean equals(Object obj){ }
      JDBC:使用java程式操作(獲取資料庫連線、CRUD)資料庫(MySQL、Oracle、DB2、SQL Server)
    ③ 抽象類、介面的使用肯定體現了多型性。(抽象類、介面不能例項化)


    8.2 多型是編譯時行為還是執行時行為?

 1 //證明如下:
 2 class Animal {
 3     protected void eat() {
 4         System.out.println("animal eat food");
 5     }
 6 }
 7 
 8 class Cat extends Animal {
 9     protected void eat() {
10         System.out.println("cat eat fish");
11     }
12 }
13 
14 class Dog extends Animal {
15     public void eat() {
16         System.out.println("Dog eat bone");
17     }
18 }
19 
20 class Sheep extends Animal {
21     public void eat() {
22         System.out.println("Sheep eat grass");
23     }
24 }
25 
26 public class InterviewTest {
27     public static Animal getInstance(int key) {
28         switch (key) {
29             case 0:
30                 return new Cat ();
31             case 1:
32                 return new Dog ();
33             default:
34                 return new Sheep ();
35     }
36 }
37 
38 public static void main(String[] args) {
39     int key = new Random().nextInt(3);
40     System.out.println(key);
41     Animal animal = getInstance(key); 
42     animal.eat(); 
43     }
44 }
45 
46 /******************************************************/
47 //考查多型的筆試題目
48 public class InterviewTest1 {
49 
50     public static void main(String[] args) {
51         Base1 base = new Sub1();
52         base.add(1, 2, 3); //sub_3
53 
54         Sub1 s = (Sub1)base;
55         s.add(1,2,3);//sub_2
56 
57         Sub1 sub1 = new Sub1();
58         sub1.add(1,2,3,4); //sub_3
59 
60         sub1.add(1,new int[]{1,2,3});//sub_3
61     }
62 }
63 
64 class Base1 {
65     public void add(int a, int... arr) {
66         System.out.println("base");
67     }
68 }
69 
70 class Sub1 extends Base1 {
71     // public void add(int a, int[] arr) {
72         // System.out.println("sub_1");
73     // }
74         public void add(int a, int... arr) {
75             System.out.println("sub_3");
76         }
77 
78         public void add(int a, int b, int c) {
79             System.out.println("sub_2");
80         }
81 }