Internal class (class inner member, the local inner classes (including anonymous inner classes))

It contains an internal thing to win a thing. A class which contains another class, called the class interior, comprising call outside its class.

For example: the relationship between the body and the heart; the relationship between the car and the engine.

Heart, only the engine and the car body to be useful. Inner classes, too.

 

classification:

  1. Internal class members;

  2. Local inner classes (including anonymous inner classes);

 

1. The members of the inner class

/ * 
Modifier class {class name external
modifiers internal class class name {
......
}
......
}
* /
public class Outer {
Private String name;

public class Inter {
public void InterMethod () {
System.out.println ( "internal method");
System.out.println ( "name:" + name); // class can access the internal members of the class properties
}
}

public void fun1 () {
Inter in new new Inter = ( );
in.InterMethod ();
}
}

After compilation, the class class file is saved on disk

 

 

 Use internal class

  Can be used directly by the external new class objects.

  Access in other classes:

    1. Indirect method: a method using an internal class external class, other classes used in the class will call this new method the external; 

/**
 * Other class uses an internal class
 */
public class OuterDemo1 {
    public static void main(String[] args) {
        Outer out = new Outer();
        out.fun1();
    }
}

    2. Direct way, directly create the inner classes

    Formula: External Internal class name name = new class name of the object class name external () .new internal class name ();.

public class OuterDemo2 {
    public static void main(String[] args) {
        Outer.Inter in = new Outer().new Inter();

        in.InterMethod();
    }
}

Question: outside the class, inner classes, inner classes method bodies appear the same name attribute, how to output the corresponding value of it?

solve:

{class OuterDemo3 public 

Private String name = "outer";

class Inter {
Private String name = "inside";
public void interMethod () {
String name = "method";
System.out.println (name); // Output: Method , the principle of proximity
System.out.println (this.name); // output: the
System.out.println (OuterDemo3.this.name); // output: outer
}
}
}

 

2. Local inner class

  In the process of defining a class body, out of this method, the class can not be used (it can not be used other types of local inner class {})

  popular:

  Permissions modifier usage rules:

  public  >  protected  >  (default)  >  private 

  1. External categories: Functional public / (defautl) modified

  2. The members of the inner class: publi / protected / (default) / private

  3. Local inner class: nothing to write

public class Test {

public void fun(){
final int num = 100;
class Fun{
private void fun2(){
System.out.println(num);
}
}
Fun fun = new Fun();
fun.fun2();
}
}

Question: Why access method where local variables, there must be final modification?

The reason (essentially a life cycle issues):

  1. Internal new class of object out of the heap memory;

  2. Local variables follow the method, the stack memory;

  3. The method runs out, and immediately the stack, local variables across disappeared;

  4. However, out of the new object will continue to exist in the heap, the garbage collector know;

  5. Therefore, to copy the memory to continue using constant pool to save.

 

3. Anonymous inner classes (important)

  As always, we want to use the interface method, you must first define the class that implements this interface -> rewrite all the abstract methods of the interface -> new implementation class to use.

  If the interface implementation class except only once, such a case can be omitted to achieve the defined class, but instead use anonymous inner classes []

interface

public interface MyInteface {
    void method();
}

Use anonymous inner classes []

/**
 * Format:
 * Interface Name Interface name = new object name () {
 * // overwrite all abstract methods
 *      };
 */
public class AnonymityTest2 {
    public static void main(String[] args) {
        MyInteface my = new MyInteface(){
            @Override
            public void method() {
                System.out.println ( "anonymous inner classes method");
            }
        };
        my.method();
    }
}

  Many people initially may have misunderstood: It is not [do] anonymous inner class? MyInteface my = new MyInteface ()} ...} This is not a name?

  Look for the "new new MyInteface () {... };" analysis:

    1) action on behalf of new objects that are created;

    2) Interface Name] [anonymous inner class to implement an interface;

    3) {...} this is anonymous inner classes [] content, which re-reads all the abstract methods interfaces

  It is bare, indeed no name no surname.

  The MyInteface my = new MyInteface ()} ...} is the object of my name, it is for you to call the object of an anonymous class method.

 

ps: anonymous inner classes, anonymous objects

  1, an anonymous inner classes [] said that in the creation of love that is the only time use only.

    If you want to create an object several times, but the same kind of content, then define the implementation class or alone more convenient.

public class AnonymityTest2 {
    public static void main(String[] args) {
        MyInteface my1 = new MyInteface(){
            @Override
            public void method() {
                System.out.println ( "anonymous inner classes method");
            }
        };
        my1.method();
        
        MyInteface my2 = new MyInteface(){
            @Override
            public void method() {
                System.out.println ( "anonymous inner classes method");
            }
        };
        my2.method();
    }
}

  2. [anonymous] represents an object, when calling the method can only be called only time.

    If you want the same object, repeatedly calling party, or give it a name from the subject.

        new MyInteface(){
            @Override
            public void method1() {
                System.out.println ( "anonymous inner classes Method 1");
            }

            @Override
            public void method2() {
                System.out.println ( "anonymous inner classes Method 2");
            }
        }.method1();
        
        new MyInteface(){
            @Override
            public void method1() {
                System.out.println ( "anonymous inner classes Method 1");
            }
            @Override
            public void method2() {
                System.out.println ( "anonymous inner classes Method 2");
            }
        }.method2();

  3. [anonymous inner classes are omitted] <implementation class / subclass>

    [Anonymous object is omitted] <object name>

    The two are not the same thing.

public class AnonymityTest {
    public static void main(String[] args) {
        fun1();
    }

    private static void fun1() {
        // For Thread, this is an anonymous object []
        // For Runnable, this is an anonymous inner classes []
        new Thread( new Runnable(){
            @Override
            public void run() {

            }
        }).start();
    }
}

  

Guess you like

Origin www.cnblogs.com/jr-xiaojian/p/12197986.html