Java-InnerClass-04

如果不需要内部类对象与其外围类对象之间有联系,那么可以将内部类申明为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");
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值