Java nested classes 嵌套类

quoted from :http://tutorials.jenkov.com/java/nested-classes.html;

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html


nested classes:

1.static nested classes:

 public class Outer{
      public class Nested{
      }
   }

   declare inner class:

   Outer.Nested instance = new Outer.Nested();

2.Non-static nested classes(Inner Classes):

 public class Outer{
      private String text = "I am a string!";      
      public class Inner{
         public void printText() {
               System.out.println(text);
        }
      }
   }

   ① declaration;② call the printText() method;

   Outer outer = new Outer();

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

   inner.printText();

special inner class: local classes;  anonymous classes

3.shadowing:

public class ShadowTest {

    public int x = 0;

    class FirstLevel {

        public int x = 1;

        void methodInFirstLevel(int x) {
            System.out.println("x = " + x);
            System.out.println("this.x = " + this.x);
            System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
        }
    }

    public static void main(String... args) {
        ShadowTest st = new ShadowTest();
        ShadowTest.FirstLevel fl = st.new FirstLevel();
        fl.methodInFirstLevel(23);
    }
}

The following is the output of this example:

x = 23
this.x = 1
ShadowTest.this.x = 0



转载于:https://my.oschina.net/u/251879/blog/219807

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值