Java高级特性之new一个内部类

package three.day.newcharacter;


class Outer
{
public static final int height = 20;
public static int weight = 20;
private int num = 100;//内置类可以访问private的,public,protected肯定可以
class Inner //定义内置类
{
  class InnerInner
  {
    void output()
    {
     System.out.println(Outer.this.num);//无论内部累内置多少内部类,内置有多深始终可以直接使用Outer.this
     System.out.println(Inner.this.num);
    }
  }
  private int num = 90;
  void output()
  {
    int num = 80;
    InnerInner innerinner = new InnerInner();
    innerinner.output();
    System.out.println(num);//局部变量
    System.out.println(this.num);//内置类私有成员
    System.out.println(Outer.this.num);//引用外部类中的私有成员
  }
}
void output()
{
   Inner inner = new Inner();
   inner.output();//外部类成员函数调用内部类成员函数
}
Inner GetInner()
{
    return new Inner();
}
}


class InnerClass3

  public static void main(String[] args)
  {
     //final int a1 = 10;
     //System.out.println(a1);
    
     Outer outer = new Outer();
     outer.output();
     
      //怎么在main函数中直接使用Inner,注意Inner的可见性
     Outer.Inner inner1 = outer.GetInner();
     inner1.output();
     
     Outer.Inner inner2 = outer.new Inner();
     inner2.output();
     
     
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值