JAVA中域、方法、類的可見度

來源:互聯網
上載者:User

標籤:log   私人方法   ber   gets   nbsp   def   理解   view   全域變數   

  1. 多態在域的問題上是特殊的。我理解不了中文版的書直接叫域,看了英文原版,原版寫的是fields,直接翻譯雖然沒錯,但是出問題的變數不是域。特地查了what is the meaning of field in java?很多人把它看成是由花括弧括起來的一個範圍。其實架構裡面也有一種東西——domain model,這個也叫域,領域模型。

     

    然後找到這篇文章

    What is a field in java?

     

    A field is an attribute. A field may be a class‘s variable, an object‘s variable, an object‘s method‘s variable, or a parameter of a function.

    [java] view plain copy 
    1. class bike{   
    2.   static int bikes;  
    3.   int gear;  
    4.   int cadence;  
    5.   
    6.   void create( int newGear, int newCadence ){  
    7.     bikes = bikes + 1;  
    8.     gear = newGear;  
    9.     cadence = newCadence;}  
    10.   int getSpeed(){  
    11.     int speed = gear*cadence*5*3.141;  
    12.     return speed;  
    13.   }  
    14. }  

    ‘bikes‘ is a class‘s variable (class variable) (static field).
    ‘gear‘ and ‘cadence‘ could be an object‘s variables (instance variables) (non-static fields).
    ‘speed‘ is an object‘s method‘s variable (local variable). 
    ‘newGear‘ and ‘newCadence‘ are parameters of a function (parameters).

     

     

    field,域是一種屬性,可以是一個類變數,一個物件變數,一個對象方法變數或者是一個函數的參數。(補充,class‘s variables,類的執行個體變數和靜態變數稱為class‘s variables,類屬變數,也稱類變數或資料域,其實翻譯成屬性也可以,類屬性,聽起來不會怪怪的,來自百度百科)。

     

     

    [java] view plain copy 
    1. class bike{   
    2.   static int bikes;  
    3.   int gear;  
    4.   int cadence;  
    5.   
    6.   void create( int newGear, int newCadence ){  
    7.     bikes = bikes + 1;  
    8.     gear = newGear;  
    9.     cadence = newCadence;}  
    10.   int getSpeed(){  
    11.     int speed = gear*cadence*5*3.141;  
    12.     return speed;  
    13.   }  
    14. }  

     


    bikes是一個類變數(靜態域)。

    gear 和 cadence 是物件變數(執行個體變數)(非靜態域)。

    (這裡有一點點小矛盾,其實這樣照百科這樣說,那麼bikes、gear和cadence都是類變數,bikes是類變數中的靜態變數,而gear和cadence是類變數中的執行個體變數。)

     

    speed是對象方法的變數(局部變數)。

    (看到沒有,local variable,java沒有出現gobal variable,全域變數,要說的話類變數的作用範圍和全域變數一樣,只不過不那樣叫)。

     

    newGear和newCadence是函數(方法)的參數(參數)。

     

  2. ** 
  3.  * 公用類,在所有包中可見 
  4.  * @author OOS 
  5.  * 
  6.  */  
  7. public class PublicClass {  
  8.     /** 
  9.      * 公用域,所有子類中可見 
  10.      */  
  11.     public int publicNum;  
  12.       
  13.     /** 
  14.      * 私人域,當前類可見 
  15.      */  
  16.     private int privateNum;  
  17.       
  18.     /** 
  19.      * 保護域,所有子類中可見 
  20.      */  
  21.     protected int protectedNum;  
  22.       
  23.     /** 
  24.      * 預設域,當前包的子類中可見 
  25.      */  
  26.     int defaultNum;  
  27.       
  28.     /** 
  29.      * 公用方法 
  30.      * 可使用:所有類 
  31.      * 可繼承:所有子類 
  32.      */  
  33.     public void publicMethod(){};  
  34.       
  35.     /** 
  36.      * 私人方法 
  37.      * 可使用:當前類 
  38.      * 可繼承:無 
  39.      */  
  40.     private void privateMethod(){};  
  41.       
  42.     /** 
  43.      * 保護方法 
  44.      * 可使用:當前包 
  45.      * 可繼承:所有子類 
  46.      */  
  47.     protected void protectedMethod(){};  
  48.       
  49.     /** 
  50.      * 預設方法 
  51.      * 可使用:當前包 
  52.      * 可繼承:當前包的子類 
  53.      */  
  54.     void defaultMethod(){};  
  55.       
  56.     /** 
  57.      * 私人類,僅在定義它的類中可見 
  58.      * @author OOS 
  59.      * 
  60.      */  
  61.     private class PrivateClass{  
  62.           
  63.     }  
  64.       
  65.     /** 
  66.      * 保護類,僅在當前包、當前類和所以子類中可見 
  67.      * @author OOS 
  68.      * 
  69.      */  
  70.     protected class ProtectedClass{  
  71.         protected ProtectedClass()  
  72.         {  
  73.               
  74.         }  
  75.     }  
  76.       
  77.     /** 
  78.      * 預設類,僅在當前包中可見 
  79.      * @author OOS 
  80.      * 
  81.      */  
  82.     class DefaultClass {  
  83.   
  84.     }  
  85. }  

 

 

與PublicClass同一個包中的測試類別CurrentPackageTest,如下:

 

[java] view plaincopy   
  1. public class CurrentPackageTest extends PublicClass{  
  2.     public static void main(String[] args)  
  3.     {  
  4.         PublicClass pc = new PublicClass();  
  5.         //當前包可以使用保護方法和預設方法  
  6.         pc.protectedMethod();  
  7.         pc.defaultMethod();  
  8.         //保護類在當前包可見  
  9.         PublicClass.ProtectedClass protectedClass = new PublicClass().new ProtectedClass();  
  10.         //預設類在當前包可見  
  11.         DefaultClass defaultClass = new PublicClass().new DefaultClass();  
  12.     }  
  13.       
  14.     public CurrentPackageTest()  
  15.     {  
  16.         System.out.println(super.publicNum);  
  17.         System.out.println(super.protectedNum);  
  18.         //PublicClass和當前類同屬於一個包,所以defaultNum可繼承  
  19.         System.out.println(super.defaultNum);  
  20.     }  
  21. }  


與PublicClass不同一個包中的測試類別OtherPackageTest,如下:

 

 

[java] view plaincopy   
    1. public class OtherPackageTest extends PublicClass{  
    2.     public static void main(String[] args)  
    3.     {  
    4.         PublicClass pc = new PublicClass();  
    5.         //PublicClass與當前類不屬於同一個包,僅能使用公用方法  
    6.         pc.publicMethod();  
    7.     }  
    8.       
    9.     public OtherPackageTest()  
    10.     {  
    11.         System.out.println(super.publicNum);  
    12.         System.out.println(super.protectedNum);  
    13.     }  
    14. }  

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.