【java的多態性】

來源:互聯網
上載者:User

標籤:public   來源程式   多態性   java   軟體   

java的多態性是物件導向的三大特性之一.(封裝,繼承,多態),多態是指程式中定義的引用變數所指向的具體類型和通過該引用變數發出的方法調用在編程時並不確定,而是在程式運行期間才確定,即一個引用變數倒底會指向哪個類的執行個體對象,該引用變數發出的方法調用到底是哪個類中實現的方法,必須在由程式運行期間才能決定。因為在程式運行時才確定具體的類,這樣,不用修改來源程式代碼,就可以讓引用變數綁定到各種不同的類實現上,從而導致該引用調用的具體方法隨之改變,即不修改程式碼就可以改變程式運行時所綁定的具體代碼, 讓程式可以選擇多個運行狀態,這就是多態性。多態性增強了軟體的靈活性和擴充性。



java多態分為:


1.方法的重載與重寫。


       重載:同類中傳回值和函數名相同,而參數列表不同的兩個方法。


       重寫:是子類的方法覆蓋父類的方法,要求方法名和參數都相同


2.對象的多態性

在這裡我們主要介紹對象的多態性


我自己的理解是小轉大自動轉換,大轉小需要強制轉換。父類大於子類。

(1)父類引用指向子類執行個體

  1. public class Mystring {  

  2.       

  3.     public static void main(String[] args) {  

  4.           

  5.         //父類大,子類小,小轉大自動轉換,只能調用父類也有的方法  

  6.          person temp=new student();//父類的引用指向子類對象  

  7.          temp.say();  

  8.            

  9.          person temp1=new teacher();  

  10.          temp1.say();  

  11.     }  

  12.   

  13. }  

  14. //抽象類別  

  15. abstract class person  

  16. {  

  17.     public String name;   

  18.      

  19.       

  20.     public String getName() {  

  21.         return name;  

  22.     }  

  23.   

  24.   

  25.     public void setName(String name) {  

  26.         this.name = name;  

  27.     }  

  28.   

  29.     public abstract void say();//抽象方法  

  30. }  

  31. //學生類  

  32. class student extends person//繼承person類  

  33. {  

  34.     //實現抽象方法  

  35.     public void say()  

  36.     {  

  37.         System.out.println("我是學生。。。。");  

  38.       

  39.     }  

  40. }  

  41. //老師類  

  42. class teacher extends person//繼承person類  

  43. {  

  44.   

  45.     public void say()  

  46.     {  

  47.         System.out.println("我是老師。。。。");  

  48.       

  49.     }  

  50.        //擴充方法  

  51.     public void sayHello()  

  52.     {  

  53.         System.out.println("Hello,我是老師。。。。");  

  54.     }  

  55. }  

複製代碼

650) this.width=650;" id="aimg_288" src="http://techfoxbbs.com/data/attachment/forum/201505/21/110413br5n9255r2fnk889.png" class="zoom" width="274" alt="110413br5n9255r2fnk889.png" /> 
(2)子類對象指向父類引用
  1. public class Mystring {  

  2.       

  3.     public static void main(String[] args) {  

  4.           

  5.         //父類大,子類小,自動轉換,只能調用父類也有的方法  

  6.          person temp=new student();//父類的引用指向子類對象  

  7.          temp.say();  

  8.            

  9.          person temp1=new teacher();  

  10.          temp1.say();  

  11.         //------------調用teacher的擴充方法sayHello()  

  12.         //父類person沒有sayHello方法,不能直接temp1.sayHello()  

  13.            

  14.          //子類小於父類,需要強制轉換  

  15.          teacher temp3=(teacher)temp1;  

  16.          temp3.sayHello();  

  17.            

  18.     }  

  19.   

  20. }  

  21. //省略類的定義  

複製代碼

執行結果: 650) this.width=650;" id="aimg_289" src="http://techfoxbbs.com/data/attachment/forum/201505/21/110443w77q858wvwrr4067.png" class="zoom" width="313" alt="110443w77q858wvwrr4067.png" />

我覺得記住小轉大自動轉換,大轉小需要強制轉換。父類大於子類就很好理解


既然父類可以自動轉化為子類,那我們可以利用這特性解決一個經常遇到問題

  1. public class Mystring {   

  2.     public static void main(String[] args) {      

  3.          student temp=new student();  

  4.          say(temp);        

  5.          teacher temp1=new teacher();  

  6.          say(temp1);               

  7.     }  

  8.     public static void say(student temp)  

  9.     {  

  10.         temp.say();  

  11.     }  

  12.       

  13.     public static void say(teacher temp1)  

  14.     {  

  15.         temp1.say();  

  16.     }  

  17.     //省略類的定義  

  18. }  

複製代碼

我們發現say()由於需要的參數不同,所以重載了兩份。這樣寫,要是類很多,那不要重載很多份。這裡我們使用對象的多態就能很好的解決。把代碼改為:
  1. public class Mystring {  

  2.       

  3.     public static void main(String[] args) {  

  4.           

  5.          person temp=new student();  

  6.          say(temp);  

  7.            

  8.          person temp1=new teacher();  

  9.          say(temp1);  

  10.       

  11.            

  12.     }  

  13.     public static void say(person temp)  

  14.     {  

  15.         temp.say();  

  16.     }         

  17.     //省略類的定義  

  18. }  

複製代碼

當然這樣寫的話,調用的方法只能是父類也有的方法,這裡能性是因為父類也有say()方法。為瞭解決這一問題,我們引入關鍵字instanceof。

instanceof的基本文法為:對象 instanceof 類型-----------返回boolean值

用來判斷一個對象是否屬於某和類的執行個體,是就返回true,否則返回false


下面我們在say()方法裡調用teacher的擴充方法sayHello()。student和person類都沒用該方法,上面代碼肯定報錯。這是我們可以將代碼改為:

  1. public class Mystring {  

  2.       

  3.     public static void main(String[] args) {  

  4.           

  5.          person temp=new student();  

  6.          say(temp);  

  7.            

  8.          person temp1=new teacher();  

  9.          say(temp1);  

  10.       

  11.            

  12.     }  

  13.     public static void say(person temp)  

  14.     {  

  15.         //如果是teacher對象就強制轉換為teacher(大轉小)  

  16.         if(temp instanceof teacher)  

  17.         {  

  18.             teacher ok=(teacher)temp;  

  19.             ok.sayHello();  

  20.         }  

  21.       

  22.     }  

  23.       

  24.       

  25.     //省略類的定義  

  26. }  

複製代碼

父類大轉小自動轉換,讓我想起了所有類的父類Object類,它當然是最大的啦。所有我們代碼當然還可以改為:
  1. public class Mystring {  

  2.       

  3.     public static void main(String[] args) {  

  4.           

  5.          person temp=new student();  

  6.          say(temp);  

  7.            

  8.          person temp1=new teacher();  

  9.          say(temp1);  

  10.       

  11.            

  12.     }  

  13.          //使用Object類  

  14.     public static void say(Object temp)  

  15.     {  

  16.         //如果是teacher對象就強制轉換為teacher(大轉小)  

  17.         if(temp instanceof teacher)  

  18.         {  

  19.             teacher ok=(teacher)temp;  

  20.             ok.sayHello();  

  21.         }  

  22.       

  23.     }  

  24.       

  25.       

  26.     //省略類的定義  

  27. }  

  28. //抽象類別  

複製代碼

這篇部落格是對我學習java多態的一個總結,也拿出來和大家分享,有什麼錯誤還望指正。


【java的多態性】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.