class Outer2{
static class Inner2{
private String InnerStr="Inner attrib";
public void InnerFun(){
System.out.println( "static nest class have a none" +" static member =>innerFun" );
}
}
public static void main(String[] arg){
Outer2.Inner2 inner=new Outer2.Inner2();
System.out.println(inner.InnerStr);
inner.InnerFun();
//System.out.println(Outer2.Inner2.InnerStr);//錯誤
//Outer2.Inner2.InnerFun();//錯誤
}
}
1 則留言:
public class MyOuter2{
public static class MyStaticInner{
public static void foo(){
System.out.println("MyOuter2.MyStaticInner si = new MyOuter2.MyStaticInner();");
System.out.println(
"先叫外部類別再叫內部類別,然後 new 一遍,就可以建Static Inner Class");
}
}
public class MyNonStaticInner{
public void foo(){
System.out.println(
"MyOuter2.MyNonStaticInner nsi = new MyOuter2().new MyNonStaticInner();");
System.out.println("要先建外部類別再建內部類別,所以要 new 兩遍!");
}
}
}
class BuildInnerClass{
public static void main(String[] args){
MyOuter2.MyStaticInner si = new MyOuter2.MyStaticInner();
MyOuter2.MyNonStaticInner nsi = new MyOuter2().new MyNonStaticInner();
si.foo();
System.out.println();
nsi.foo();
}
}
===================================
有看到一個題目像這樣在比較new的方式,還蠻有趣的!有助於更加了解兩者差異!
張貼留言