内部类InnerClass

本文详细解析了Java中内部类的访问规则及不同域内变量的访问格式,通过具体示例说明了内部类如何访问外部类的成员变量,并展示了如何创建内部类实例。

内部类:

1) 访问规则

内部类可以直接访问内部类中的成员,包括私有

外部类访问内部类的成员,必须建立内部类成员

2)  访问不同域内变量所用的格式

class Outer{

       private int x=3;

    class Inner{

           int x=4;

              void function(){

                     int x=6;

                     //x->6  this.x->4   outer.this.x->3

                     System.out.println("inner:"+Outer.this.x);

              }

       }

       void method(){

              Inner in=new Inner();

              in.function();

       }

}

 

public class InnerClass {

       public static void main(String[] args) {

              // TODO Auto-generated method stub

          /*//1.使用外部类

              Outer out=new Outer();

              out.method();*/

              //2.直接使用内部类

              Outer.Inner in=new Outer().new Inner();

              in.function();

转载于:https://www.cnblogs.com/ShiQiFang/p/6140569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值