现在的位置: 首页 > 综合 > 正文

Java Inner Class, Static Inner Class, Inner Interface

2012年02月01日 ⁄ 综合 ⁄ 共 1292字 ⁄ 字号 评论关闭
class A{
    
public static class B{
    }

    
    
public class C{        
    }

    
    
public interface IF{
    }

    
    
public IF create(){
        
/**
         *  local inner class
         
*/

        
class F implements IF{
            
        }

        
return new F();
    }

    
    
/**
     * 和C#类似,好熟悉的匿名委托阿
     * 
@return
     
*/

    
public IF createAnother(){
        
return new IF(){
        }
;
    }

    
}


class X implements A.IF{
    
}


interface IShare{
    
class A{
    }


    A getData();
}




public class ClientConsole{
    
public static void main(String[] args){
        A.B b 
= new A.B();
        System.out.println(b.getClass().getName());
        
        A.C c 
= (new A()).new C();
        System.out.println(c.getClass().getName());
        
        A.IF x 
= new X();
        System.out.println(x.getClass().getName());
        System.out.println(x 
instanceof A.IF);
        
        A.IF f1 
= (new A()).create();
        
// local inner class 仅仅显示为外部可见类型
        System.out.println(f1.getClass().getName());
        System.out.println(f1 
instanceof A.IF);    

        A.IF f2 
= (new A()).create();
        System.out.println(f2.getClass().getName());
        System.out.println(f2 
instanceof A.IF);    
        
        System.out.println(IShare.A.
class.getName());
    }

}

 

 

 输出:

A$B
A$C
X
true
A$1F
true
A$1F
true
IShare$A

 

抱歉!评论已关闭.