java中的内部类

 

内部类

 

不一定在类的内部,方法的内部也是可以的;应用的场景不是很多;只需能看懂别人的代码;和Swing应用;

 

四种内部类

1、

静态内部类(static inner class):只能访问外部类的静态成员变量与静态方法,生成静态内部类对象的方式为:

OuterClass.InnerClass inner = new OuterClass.InnerClass();

 

The simplest form of inner class

uCan’t have the same name as the enclosing class

uCompiled into a completely separate .class file from the outer class

uCan access only static members and methods of the enclosing class, including private static members

uCreate an instance of a static inner class out of enclosing class:

 

上面是四个要求

是由Static关键字修饰的

只能访问外部类的静态变量和静态方法;

 

2、成员内部类

 

Defined in an enclosing class without using the static modifier

uLike instance variables

uCan access all members of the enclosing class

uCreate an instance within the enclosing classthis.newInnerclass();

uCreate an instance out of the enclosing class(new Outerclass()).new Innerclass();

uAccess members of the enclosing class within inner classesOuterclass

 

类比非静态元素;有Static关键字就是

 

成员内部类:

 

3、局部内部类

uDefined within the scope of a method, even smaller blocks within methods

uThe least used form of inner class

uLike local variables, can’t be declared public, protected, private and static

uCan only access final local variables

 

不能被外部的使用;

4、匿名内部类

Local inner classes which don’t have class names

uNo key word class

uNo key word extends and implements

uNo constructors

uImplicitly(隐士) extend a superclass or implement an interface

 

匿名内部类(Anonymous Inner Class):匿名内部类会隐式地继承一个父类或实现一个接口

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值