Dowemo

The internal class isn't a great understanding, but it's actually a class that contains another class.
As a person is composed of the brain, limb, organ, and other body results, the inner class is equivalent to one of the organs, such as heart: It also has its own properties and behavior ( blood, bounce )
Obviously, it isn't possible to use attributes or methods to represent a heart, requiring a class.
And the heart is in the body, as if it's inside the outside.

Example 1: basic structure of an internal class


//外部类


class Out {


 private int age = 12;



 //内部类


 class In {


 public void print() {


 System.out.println(age);


 }


 }


}



public class Demo {


 public static void main(String[] args) {


 Out.In in = new Out().new In();


 in.print();


 //或者采用下种方式访问


 /*


 Out out = new Out();


 Out.In in = out.new In();


 in.print();


 */


 }


}






Run results: 12
From the above example, it isn't hard to look at the internal class, but the internal class is, but why also use the inner class.
Because an internal class can use a member variable of an external class, including private, I & tead of generating an external class object, this is the only advantage of internal classes.
As the heart can directly access the body 's blood, not by doctor.

After compiling the program, two. Class files are generated, respectively out, class, and out. In. classIt's Medium Represents the out. In in the program above.
A out. In in = new out ( ). New in ( ) can be used to generate an object of an internal class, which has two small pieces of knowledge.
A. The beginning of the is to indicate which external class object to generate in order to be generated.
2. Objects that must have an external class to produce an object of an internal class because the internal class is intended to access member variables in an external class.

Example 2: variable access in internal class


class Out {


 private int age = 12;



 class In {


 private int age = 13;


 public void print() {


 int age = 14;


 System.out.println("局部变量:" + age);


 System.out.println("内部类变量:" + this.age);


 System.out.println("外部类变量:" + Out.this.age);


 }


 }


}



public class Demo {


 public static void main(String[] args) {


 Out.In in = new Out().new In();


 in.print();


 }


}






Run results:
Local variable: 14
Internal class variable: 13
External class variable: 12
From I & tance 1, an internal class directly accesses member variables of external classes without having to have the same member variables and local variables, without specifying out. This. Property names.
Otherwise, local variables in the internal class override member variables of external classes
Access to the internal class itself can be used by the this. Attribute name, which accesses member variables of external classes need to use out. This. Attribute names.

Example 3: static internal class


class Out {


 private static int age = 12;



 static class In {


 public void print() {


 System.out.println(age);


 }


 }


}



public class Demo {


 public static void main(String[] args) {


 Out.In in = new Out.In();


 in.print();


 }


}






Run results: 12
As you can see, the internal class can only access static member variables of external classes, with limitations if they're statically enforced internally.
Second, because the internal class is static, out. In can be seen as a whole, and can be directly new to the object of the inner class ( from the class name access static, which doesn't produce an external class object ).

Instance 4: private internal class


class Out {


 private int age = 12;



 private class In {


 public void print() {


 System.out.println(age);


 }


 }


 public void outPrint() {


 new In().print();


 }


}



public class Demo {


 public static void main(String[] args) {


 //此方法无效


 /*


 Out.In in = new Out().new In();


 in.print();


 */


 Out out = new Out();


 out.outPrint();


 }


}






Run results: 12
If an internal class only wants to be manipulated by methods in an external class, you can use private to declare an inner class
In the code above, we've to generate objects of the in class in the out class, without using out. In in = new out ( ), new in ( ).
That's, the internal class at this point is controlled only by external classes
As is, my heart can only be controlled by my body, and others cannot access it directly.

Example 5: method internal class


class Out {


 private int age = 12;



 public void Print(final int x) {


 class In {


 public void inPrint() {


 System.out.println(x);


 System.out.println(age);


 }


 }


 new In().inPrint();


 }


}



public class Demo {


 public static void main(String[] args) {


 Out out = new Out();


 out.Print(3);


 }


}






Run results:
3
12
In the code above, we move the inner class to the method of the external class, and then generate an internal class object in the method of the outer class to call the inner class method
If you pass the parameter in the method of the class at this point, the method parameter of the external class must use the final definition
For the final end there's no special meaning, just a representation.
Anonymous internal classes are also internal classes without names.
Because there's no name, anonymous internal classes can only be used once, which is often used to simplify code writing
But there's a prerequisite to using anonymous internal classes: Must inherit a pare not class or implement an interface

Example 6. 1: an abstract method isn't implemented using anonymous inner classes.


abstract class Person {


 public abstract void eat();


}



class Child extends Person {


 public void eat() {


 System.out.println("eat something");


 }


}



public class Demo {


 public static void main(String[] args) {


 Person p = new Child();


 p.eat();


 }


}






Run results: eat something
As you can see, we inherit the person class with child, then implement an instance of the child, and turn it up into a reference to the person class
However, if the child class here's only used once, it's difficult to write it as a separate class.
At this time, an anonymous internal class is introduced.

Example 6. 2: basic implementation of anonymous internal classes.


abstract class Person {


 public abstract void eat();


}



public class Demo {


 public static void main(String[] args) {


 Person p = new Person() {


 public void eat() {


 System.out.println("eat something");


 }


 };


 p.eat();


 }


}






Run results: eat something
As you can see, we implement the method from the abstract class person directly in braces
This allows you to omit the writing of a class
Also, anonymous internal classes can also be used on interfaces

Example 6. 3: use anonymous internal classes on interfaces.


interface Person {


 public void eat();


}



public class Demo {


 public static void main(String[] args) {


 Person p = new Person() {


 public void eat() {


 System.out.println("eat something");


 }


 };


 p.eat();


 }


}






Run results: eat something
As you can see from the above example, as long as a class is abstract or an interface, the methods in the subclass can use an anonymous inner class




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs