Java inner classes (members of inner classes, local inner classes, static inner classes and anonymous inner classes)

1, members of the inner class: i.e., as a member of the class of the presence of external, outer class attribute, parallel method.
Note: Members of the class can not define the internal static variables, but you can access all members of the outer class.
Outer {class public
Private static int. 1 = I;
Private J = int 10;
Private int = 20 is K;
public static void outer_f1 () {
// do something More
}
public void out_f2 () {
// do something More
}
// members of the inner class
class inner {
// static int inner_i = 100; // not permitted in the internal static class variable
int j = 100; // inner class instance variables outside the class can coexist
int = inner_i. 1;
void inner_f1 () {
System.out.println (I); // if external variables and class variables within the class is not the same name, then the variable can directly access an external variable name class
System.out.println (j); // inside class access to internal class variable directly with their variable names
System.out.println (this.j); // can ". this variable name" to access the inner class variables within class with
// class with access to external internal name of the class instance variables available "external name class .this. variable name."
System.out.println (k); // if external variables and class variables within the class is not the same name, variable name can directly access the external variables of the class
outer_f1 ();
outer_f2 ();
}
}
// outer class the non-static method to access the internal member class
public void outer_f3 () {
inner inner inner new new = ();
inner.inner_f1 ();
}
static method // member inside the outer class-based access, the external access members inside outer class class as
public static void outer_f4 () {
// build Step1 external object
outer outer new new OUT = ();
// build Step2 *** The internal class object external object ***
inner inner inner out.new = ();
the method of access to the internal class // step3
inner.inner_f1 ();
}
public static void main (String [] args) {
outer_f4 ();
}
}
advantages of the members within the class:
⑴ internal class as a member of the outer class, can access external class members or private property. (Even if an external class declaration is PRIVATE, but for the class in the interior or inside thereof is visible.)
⑵ class definitions with the attribute inside the outer class inaccessible. This realization is smaller than the outer class private access to the outside of class.
Note: The internal class is a compile-time concept, once the compilation is successful, it will become a completely different categories. For a class called external outer and inner named therein defined within the class. Outer.class and outer $ inner.class categories after the compilation is completed.
2, a partial inner classes: i.e., classes defined in the internal process, similar to the local variables, without prior modifiers public or private local inner class, which defines the range of code blocks.
Note: the internal local static variables defined in the class are not available, access to local variables outer class (i.e., the variables in the method), but the variables must be final.
Outer {class public
Private int S = 100;
Private out_i = int. 1;
public void F (Final int K) {
Final S = 200 is int;
int. 1 = I;
Final J = int 10;
class Inner {// definition method internal
int s = 300; // variables can be defined with the external name of the class
// static int m = 20; // not a static variable defined
inner (int K) {
inner_f (K);
}
int inner_i = 100;
void inner_f (int k) {
System.out.println (out_i); // if there is no inner class name of the class and the external variables, within the class may directly access the instance variables of the class
System.out.println (k); // ***** can access the local variable outer class (i.e., the variables in the method), but the variables must be final *****
// System.out.println (I);
System.out.println (S); // If the internal there are class variables and external class with the same name, with direct access to the variable name is a variable inner class
System.out.println (this.s); // use ". this variable name" access is also internal class variable
System.out .println (Outer.this.s); // external. "external class name .this class variable name" to access an external class variable
}
}
new new Inner (K);
}
public static void main (String [] args) {
// access the local inner class must have external object
outer outer new new OUT = ();
out.f (. 3);
}
}
Note:
not directly generate a local inner class outside the class (class guaranteed inside the local is not visible outside of). To generate the required objects using local inner classes, objects, invoke methods, in the method before calling its local inner class. Through internal classes and interfaces a weak coupling force is reached, with a partial inner class to implement an interface, and the interface type in the return process, the local internal category is not visible, the visibility of the shielding implementation class.
3, static inner classes: static inner class defined in the class, the outer any method by static definition.
Note: Static internal class can be defined in a static or non-static member
public class Outer {
Private static int. 1 = I;
Private J = int 10;
public static void outer_f1 () {
}
public void outer_f2 () {
}
// static internal class can be public, protected, private modified
// class can be defined in the internal static static or non-static member
static class inner {
static int inner_i = 100;
int = 200 is inner_j;
static void inner_f1 () {
System.out.println ( "Outer.i" + i); // internal static type can only access a static class member external
outer_f1 (); // including static variables and methods
}
void inner_f2 () {
// System.out.println ( "outer .i "+ j); static inner classes can not access the external non-static member class //
// outer_f2 (); // comprises a non-static and non-static method variable
}
}
public void outer_f3 () {
// internal and external access class static class members: internal static member
System.out.println (Inner.inner_i);
Inner.inner_f1 ();
// access non-static class member outside inner classes: the class to instantiate internal
Inner Inner Inner new new = ();
inner.inner_f2 ();
}
static void main public (String [] args) {
new new outer () outer_f3 ();.
}
}
Note: ******* generation (new) does not require an external static class member inner class: this class is a static internal and the difference between members of the inner class. Static objects can be generated directly inside classes:
Outer.Inner in Outer.Inner new new = ();
without generating by generating external object. This would actually static inner classes to become a top-level class. Internal static type can not be defined by private. *******
Examples:
For the two classes with the same method:
class People
{
RUN ();
}
class Machine {
RUN ();
}
In this case there is a robot class:
class Robot Implement the extends People Machine.
This when the run () can not be implemented directly.
Note: When the interface class (interface or interface) method naming conflicts occur, must use the internal classes to implement. Can not fully use multiple inheritance interface with the interface with the inner class can achieve true multiple inheritance.
4, anonymous inner classes
anonymous inner classes is a special local inner class that implements the interface is anonymous class.
IA is defined as the interface.
IA I = new IA () { };
characteristics anonymous inner classes:
1, for a class or other classes to inherit implement the interface, does not require additional process, just in advance of or covered with inherited methods.
2, just to get an object instance, you do not need to know the actual type.
3, the class name does not make sense, that is, do not need to use.
Outer {class public
Private static int. 1 = I;
Private J = int 10;
public static void outer_f1 () {
}
public void outer_f2 () {
}
// static inner class may be public, protected, private modified
// class static inner you can define a static or non-static member
static class inner {
static int inner_i = 100;
int = 200 is inner_j;
static void inner_f1 () {
System.out.println ( "Outer.i" + I); // static inner classes You can only access static members outside of class
outer_f1 (); // including static variables and methods
}
void inner_f2 () {
@ System.out.println ( "Outer.i" + J); // static inner classes can not access non-static member outside the class
// outer_f2 (); // comprises a non-static and non-static method variable
}
}
public void outer_f3 () {
static class member // external access to internal classes: internal static member
System.out.println (Inner.inner_i);
inner .inner_f1 ();
// access non-static class member outside inner classes: the class to instantiate internal
inner inner inner new new = ();
inner.inner_f2 ();
}
public static void main (String [] args) {
new new . Outer () outer_f3 ();
}
}
Note: An anonymous inner class must be in the back of the new, with its implicit implement an interface or a class, not the class name, according to the multi-state, we use the parent class name. Because he is a local inner class, then all inner classes are partial restrictions on its entry into force. Anonymous inner classes is not the only class constructor. Most anonymous inner class is an interface back to the call. Anonymous inner classes at compile time named Out $ 1.class automatically by the system. If compile-time type of an object is the interface, then the class type of its operation for the realization of this interface. Because no constructor anonymous inner classes, so their use is very limited range. Using local inner class when multiple objects, using local inner class relatively more. Anonymous inner classes can not define a constructor method. If compile-time type of an object is the interface, then the class type of its operation for the realization of this interface.
________________________________________________________________________________
inner classes Summary:
1. First, the inner class as a special member of the outer class to look at, so it has a closed class members rating: private, protected, default (friendly), public
it has members of the class modifiers: static, Final, abstract
2. non-static inner classes nested inner class, inner classes have an implicit this pointer outside of class, so it can access all the resources outside the class (including, of course private)
members outside of class to access internal class, We must first get inside the object classes, and depending on the package level internal class members.
Nonstatic inner classes can not contain any static members.
3. The static inner classes: static inner class, this pointer no longer contains an outer class, the class and initializes the external load.
Internal static type can include static or non-static member.
Static inner classes can only be accessed outside the class static members.
External static inner class to access the members of the class, follow the general class regulations. For static members. To access a member of the class name, for a non-static members, only
with the object. Access member
4. A method for an internal class or classes can only access internal block or blocks method final variables .
There are two static class member, non-static, inside the same class there are two
examples of non-static internal class, create or must be created (OuterClassInstanceName.new innerClassName (ConstructorParameter by way of example in the method outside the outer class class) ), and direct access to information outside the class, external object referenced by OuterClassName.this may be
instances static internal class can be created directly, without reference to an external class instance.
Whether static inner classes or non-static has a reference to the outer class
non-static inner classes have static members are not allowed
inside class method only allows final list of parameters final local variables and methods of access methods, so that the method inner classes and inner classes makes no difference. But the internal class method can not be accessed outside of the method, the method can not have static internal class
method if anonymous inner class inherits from the interface must implement the specified interface, and no parameters
anonymous inner classes if inherited from class, parameters must be a parent parameters passed to the constructor of the class

If the violation, please contact deleted from the above: https: //zhidao.baidu.com/question/717236740296607765.html 

 

Guess you like

Origin www.cnblogs.com/soulmatesjc/p/11208750.html