Java-InnerClass-04

來源:互聯網
上載者:User

如果不需要內部類對象與其外圍類對象之間有聯絡,那麼可以將內部類申明為static.稱為嵌套類.嵌套類相當於外部定義的類.這種類很少使用.
普通的內部類對象隱式地儲存了一個引用,指向建立它的外圍類對象.然而,當內部類是static時就不是這樣了.嵌套類意味著:
1.要建立嵌套類的對象,並不需要其外圍類的對象;2.不能從嵌套類的對象中訪問非靜態外圍類對象;
3.普通內部類的欄位和方法,只能放在類的外部層次上,所以普通內部類不能有static成員,也不能包含嵌套類.但是嵌套類可以包含所有這些東西.

為什麼普通內部類不能擁有static成員?
I hate static!
You've probably heard about static inner classes. Well, they don't deserve to be called inner classes! 
A static inner class (an inner class marked as static) looks like this:
  class Outer{
 static class Inner{}
` } 
I don't like them because they don't give me that special object-to-object bond. In fact, static inner classes aren't even supposed to be called inner classes at all. Technically, they are "top-level nested classes".
A static nested class can be instantiated, but the object created doesn't share any special relationship with an outer object.
The static nested class is tied only to the outer class, not an instance of the outer class.
  Outer.Inner i = new Outer.Inner(); 
That's why you can make an instance of the static nested class without having an instance of the outer class, just the way you can call static methods of a class without having any instances of that class. A top-level nested class is little more than another way to control namespace.
Anyway, a static inner class is not really an inner class
 
只能猜,而猜的又一言難盡,就算猜中了亦只是思維遊戲.思維遊戲自己做才有趣味,才有用.所以如果閣下不想動腦筋(實在也未必值得),記住它就成了.

不能在內部類中聲明靜態變數是對java的內部類的一種保護

什麼是靜態方法和變數?:在運行時它們被儲存在靜態區,其它的對象都可以訪問靜態區.內部類的本意是資訊隱藏,所以不可以.

記住有一點,凡是靜態,都不是內部的,這樣你就明白了.雖然你的top level的程式可以包含所謂的靜態內部類,靜態變數,靜態塊,但是其實都是類執行個體外部的東西,他們不屬於任何一個類的執行個體.
在一個普通的內部類中,通過一個特殊的this引用可以連結到其外圍類對象.嵌套類就沒有這個特殊的this引用,這使得它類似於一個static方法.

/**//*Destination.java:*/
public interface Destination...{
    String readLabel();
}
/**//*Contents.java:*/
public interface Contents...{
    int readValue();
}

class StaticParcel...{
    private static class StaticContents implements Contents...{
        private int value;
        private StaticContents(int value)...{this.value = value;}
        public int readValue()...{return value;}
        /**//*nested class中的成員沒有必要全是static的*/
    }
    private static class StaticDestination implements Destination...{
        private String label;
        private StaticDestination(String toWhere)...{label = toWhere;}
        public String readLabel()...{return label;}
        private static int distance;
        public static int readDistance()...{return distance;}
    }
    public static Contents getContents(int value)...{return new StaticContents(value);}
    public static Destination getDestination(String label)...{return new StaticDestination(label);}
}
class TestStaticParcel...{
    public static void main(String[] args) ...{
        Contents con = StaticParcel.getContents(11);
        Destination des = StaticParcel.getDestination("ChangSha");
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.