為正確理解static在應用於內部類時的含義
(
(
但在存在一些限制
倘若為了創建內部類的對象而不需要創建外部類的一個對象
//: Parcel
// Static inner classes
package c
abstract class Contents {
abstract public int value();
}
interface Destination {
String readLabel();
}
public class Parcel
private static class PContents
extends Contents {
private int i =
public int value() { return i; }
}
protected static class PDestination
implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
public static Destination dest(String s) {
return new PDestination(s);
}
public static Contents cont() {
return new PContents();
}
public static void main(String[] args) {
Contents c = cont();
Destination d = dest(
}
} ///:~
在main()中
通常
//: IInterface
// Static inner classes inside interfaces
interface IInterface {
static class Inner {
int i
public Inner() {}
void f() {}
}
} ///:~
在本書早些時候
//: TestBed
// Putting test code in a static inner class
class TestBed {
TestBed() {}
void f() { System
public static class Tester {
public static void main(String[] args) {
TestBed t = new TestBed();
t
}
}
} ///:~
這樣便生成一個獨立的
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27032.html