Java 局部类/匿名类[转]

[局部类]
对一个静态成员类,去掉其声明中的“
static”关键字,将其定义移入其外部类

的静态方法或静态初始化代码段中就成为了局部静态成员类。

       对一个成员类,将其定义移入其外部类的实例方法或实例初始化代码中就成为了局部成员类。

       局部静态成员类与静态成员类的基本特性相同。例如,都只能访问外部类的静态字段或方法,但不能访问外部类的实例字段和实例方法等。

       局部成员类与成员类的基本特性相同。例如,局部成员类实例必属于其外部类的一个实例,可通过OuterClass.this引用其外部类实例等。

另外,局部类也有其自己的特性,如以下代码所示:

public   class  Outer {
    
private   int  instanceField; 
    
private   static   int  staticField; 
    
    
// define a local member class in instance code block
    {
        
int  localVirable1  =   0 ;
        
final   int  localVirable2  =   1 ;
        
class  Inner1 {
            
public  Inner1() {
                
// can access its outer class' field and method directly
                instanceField  =   1 ;
                
// use OuterClass.this to get its corresponding outer class instance
                Outer. this .instanceField  =   1 ;
                
                
// can not access the not final local virable in its containing code block
                
// System.out.print(localVirable1);
                
                
// can access the final local virable in its containing code block
                System.out.print(localVirable2);
            }
        }        
        
        
// local class can not have privilege modifier 
         /* public class inner2 {            
        }
*/
    }
    
    
//  define a local static member class in static code block
     static  {
        
class  Inner2 {
            
public  Inner2() {
                staticField 
=   1 ;
                
// can not access instance field and method in a local static member class 
                
// intanceField = 2;
            }
        }
    }
    
    
public   void  intanceMethod() {
        
// define a local class in its out class' instance method
         class  Inner3 {
        }
        
        
// local class is visible only in its containning code block
        
// Outer.Inner2 inner2;
    }
    
    
private   static   void  staticMethod() {
        
// define a local static member class in its out class' static method
         class  Inner4 {    
            
public  Inner4() {
                staticField 
=   2 ;
            }
        }
        
        
// can not define a interface as a local class
         /* interface I {
        }
*/
    }
}

局部类特性
如示例代码所示,局部类能且只能访问其所属代码段中的声明为final 的局部 变量。为什么只能访问声明为final 的局部变量呢?我们知道,局部变量在其所属的代码段(譬如某个函数)执行完毕后就会被回收,而一个局部类的实例却可以在其类定义所属代码段执行完毕后依然存在,如果它可操控非final 的局部变量,用户就可以通过该实例修改已不存在的局部变量,无意义。

局部类约束
  • 如示例代码所示,内部类只在定义它的代码段中可见,不能在它所属代码段之外的代码中使用;因此也就没有public/private/default权限修饰符(无意义)
  • 不能以局部类形式定义一个接口。局部类只在其所属代码段中可见,定义这样的接口无意义
  • 局部类类名不能与其外部类类名重复

什么时候使用局部类
局部类大部分以匿名类的形式使用。

[匿名类]
没有类名的局部类就是匿名类。用一条语句完成匿名类的定义与实例创建。例如如下代码:

 

public   class  Outer {
    
public   void  instanceMethod() {
        
// define a nonymous class which implements Action interface and creat an instance of it
        Action action  =   new  Action() {
            
public   void  doAction() {
                System.out.println(
" a simple anonymous class demo " );
            }};
        action.doAction();
        
        
// define a nonoymous class which extends BaseClass and create an instance of it
         new  BaseClass( 5 ) {
            
public   void  printData(){
                System.out.println(
" data =  "   +  getData());
            }
        }.printData(); 
// "data = 5" will be outputed
    }
}

interface  Action {
    
void  doAction();
}

class  BaseClass {
    
private   int  data;
    
    
public  BaseClass ( int  data) {
        
this .data  =  data;
    }
    
    
public   int  getData() {
        
return  data;
    }
}

匿名类是一种特殊的局部类。局部类的特性与约束都适用与它。

转载于:https://www.cnblogs.com/simply-zhao/archive/2008/04/13/1151157.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值