码迷,mamicode.com
首页 > 其他好文 > 详细

内部类InnerClass

时间:2016-12-07 13:20:17      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:private   str   strong   rate   访问   static   outer   bsp   格式   

内部类:

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();

内部类InnerClass

标签:private   str   strong   rate   访问   static   outer   bsp   格式   

原文地址:http://www.cnblogs.com/ShiQiFang/p/6140569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!