Java學習(四)

來源:互聯網
上載者:User

標籤:public   儲存   編譯   就是   protect   temp   多繼承   color   自訂函數   

Java中類的操作與C++中基本相同,但捨棄了一些比較複雜但實際用處不多的操作,比如多繼承,在此用介面來表示。

 1 package helloWorld; 2  3 public class Class { 4     private int n; 5     public Class() 6     { 7         this(1);//相當於public Class(1)    {this.n=n;} 8     } 9     public Class(int n)10     {11         this.n=n;12     }13     14     void print()15     {16         System.out.println(n);17     }18 }19 20 class ClassTemp extends Class21 {22     int m;23     24     /*25      * 類的初始化26      */27     public ClassTemp(int n)28     {29         this.m=n;30     }31     32     /*33      * 添加自訂函數34      */35     public void run()36     {37         System.out.println("Hello World!");38     }39     40     /*41      * 覆蓋42      * @see helloWorld.Class#print()43      */44     @Override45     void print()46     {47         System.out.println(m);48     }49 }

類的繼承需要用extends來表示,函數的覆蓋需要在函數前用@Override來聲明(也可以不加)。子類繼承父類的所有欄位Field、非私人方法Method。但是不繼承構造方法。

在類中存在重載,即函數同名時可以通過變數名來區分不同的函數。以下就是兩個不同的函數。

1 void print(String str)2     {3         System.out.println(str);4     }5     6     void print(String str1,String str2)7     {8         System.out.println(str1+str2);9     }

在編譯器中,編譯器會自動產生如下名稱,用於預留空間給將要儲存的資料。在代碼中相同的函數名,實際上在編譯器中是不相同的。

$print_String$print_String_String

 

在Java中還有一個與this指標相類似的關鍵字——super。super可以用於指向父類的欄位、方法和建構函式,但不能指向子類。

super與this的異同:1.this指向本類中包含的關鍵字、 方法;而super只是指向父類的關鍵字、方法、建構函式。2.this指標很難區分子類與父類重名的情況,包括欄位重名、方法重名;而super可以讓兩重名欄位與方法相互賦值。

3.子類的構造方法通過super調用父類的構造方法來減少工作量。4.super訪問父類的關鍵字或方法時,其許可權要為protected或public,否則不能訪問。5.this和super都不能與static欄位共存。

此外,在使用this和super指標時,都需要將其放在方法中的第一行,否則會出錯。

以下是由上面更該的一些程式:

 1 package helloWorld; 2  3 public class Class { 4     private int n; 5     protected int p; 6     public Class() 7     { 8         this(1);//相當於public Class(1)    {this.n=n;} 9     }10     public Class(int n)11     {12         this.n=n;13     }14     15     void print()16     {17         System.out.println(n);18     }19 }20 21 class ClassTemp extends Class22 {23     int m;24     int n;25     26     /*27      * 類的初始化28      */29     public ClassTemp(int a,int b,int c )30     {31         super(a);//利用super調用父類Class的建構函式32         m=b;33         this.n=c;34         super.p=c;//訪問父類欄位需要使用protected或public35     }36     37     /*38      * 添加自訂函數39      */40     public void run()41     {42         super.print();//利用super調用父類的函數43         System.out.println("Hello World!");44     }45     46     /*47      * 重載48      * @see helloWorld.Class#print()49      */50     @Override51     void print()52     {53         System.out.println(m);54     }55         56 }

 

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.