用一个程序来说明Java内部类的使用要点

 //package MyTest.Unit1; --> This is must be fit to actual directory

public class Test1 {
  static int l = 4;
  public static void main(String[] args) {
    Test1 a = new Test1();
    a.amethod();
    Test2 b = Test2.dump(2);
    b.out();
    Test2 c = Test2.dump2(2);
    b.out();
    Test2 d = new Test2(0);
    d = d.dump(2);
    int i = 0x7FFFFFF8;
    System.out.println("/nThe opposite value of 0x7FFFFFF8 is: " + (~i) + "/n");
    Test2.inner2 in = d.new inner2(4);
    //Test2.inner3 in2 = Test2.new inner3(5); //Error!!
    //Test2.inner3 in2 = d.new inner3(5);  // Error!
  }
 
  public void dump() {
  }
 
  public void amethod() {
    class CInMet{
      public void mymethod() {
        dump();
        System.out.println("l Value: " + l + "/n");  // --> This statement shows that: Inner Class can access Outer Class's static member(No matter this member is public or private).
      }
    }
  }
}

class Test2 {
  private int i = 0;
  private int j = 0;
  public int m = 0;
  private static int t = 0;
  public Test2(int i)
  {
    this.i = i;
  }
  public void out() {
    System.out.println("Test2.i: " + i + "/n" + "Test2.j: " + j + "/n" + "Test2.t: " + t + "/n");
  }
  public static Test2 dump(final int i) {
   return new Test2(i) {
     private int k = i;
     public void change() {
       //i = 10;  --> Here "i" is the parameter "final int i", not "private int i" of Test2!
       super.i = 10;
       t = 10;
     }
   };
  }
  public static Test2 dump2(final int i) {
    class inner extends Test2 {
      private int k = i;
      public void change() {
        //j = 10; --> "j" is a private member in Test2
        super.j = 10;
        //m = 10; --> "m" is non-static member, it can not be referenced in a static function
        super.m = 10;
        t = 10;
      }
      public inner(int i) {
        super(i);
        change();
      }
    }
    return new inner(i);
  }
  public Test2 dump3(final int i) {
    class inner extends Test2 {  // In function, can not declare inner class as private or static
      private int k = i;
      public void change() {
        //j = 10; --> "j" is a private member in Test2
        super.j = 10;
        //m = 10; --> "m" is non-static member, it can not be referenced in a static function
        super.m = 10;
        t = 10;
      }
      public inner(int i) {
        super(i);
        change();
      }
    }
    return new inner(i);
  }
 
  private class inner extends Test2 {
    private int k = i;   // --> inner class(not be declared in a function) can access outer class's member(private, public or static, ...).
    public void change() {
      j = 10;
      m = 10;
      t = 10; // --> inner class can access outer class's member(private, public or static, ...).
    }
    public inner(int i) {
      super(i);
      change();
    }
  } 
 
  class inner2 extends Test2 {
    private int k = i;   // --> inner class(not be declared in a function) can access outer class's member(private, public or static, ...).
    public void change() {
      j = 10;
      m = 10;
      t = 10; // --> inner class can access outer class's member(private, public or static, ...).
    }
    public inner2(int i) {
      super(i);
      change();
    }
  }
 
  static class inner3 extends Test2 {
    //private int k = i;   // --> i can not be referenced in static context
    public void change() {
      //j = 10;  // --> j can not be referenced in static context
      m = 10;
      t = 10; // --> inner class can access outer class's member(private, public or static, ...).
    }
    public inner3(int i) {
      super(i);
      change();
    }
  }
 
  public void amethod() {
    inner iClass = new inner(1);
    iClass.change();
  }  
}

你可能感兴趣的:(java,c,String,function,Class,Access)