On the inner classes in Java

 

Recently talking about inner classes in Java, the feeling of inner classes for beginners actually used much, so inner classes Simply put, in fact, the class within a class definition.

Divided according to the case definition: member inner classes, local inner classes, static inner classes, anonymous inner classes.

Members of the inner class is defined as a class member variable of the class.

Internal local class is defined as a local variable in the class which the class, or a method usually used in the code block.

Internal static class is a static variable defined in the class, which is a static class variable.

Anonymous inner classes, usually to create an interface object that implements the interface is at the same time creating an object, and the realization of this interface is only used once here, so no need to define the name, called anonymous inner classes.

Consider the following code:

public interface MyInter{

public void method();

}

 

public class Outter {

Public static //  int  i; // common static variables

// static inner classes

public static class Inner3{

}

// public String name; // ordinary member variables

// members of the inner class

public class Inner1{

}

 

public void test() {

// String n; // common local variables

// local inner class

class Inner2{

}

// anonymous inner classes, at runtime will generate a temporary name, is similar: Outter $ 1.java

MyInter in = new MyInter() {

@Override

public void method() {

}

};

}

}

 

The above code shows the case of four different internal classes.

Then look at the specific usage of these four scenarios.

 

Static inner classes:

public class Outter {

public int o1;

public static int os1;

public void om1() {

System.out.println(Inner3.ns3);

System.out.println(new Inner3().n3);

System.out.println("om1");

}

public static void oms1() {

System.out.println("oms1");

}

// public static int i;

// static inner classes

public static class Inner3{

// define basic members and methods, as well as static members and methods in a static inner classes

public int n3;

public static int ns3;

public void m3() {

System.out.println("m3");

}

 

public static void ms3() {

System.out.println(os1);

oms1();

System.out.println("ms3");

}

}

}

 

Static inner classes of use:

public static void main(String[] args) {

Outter.Inner3 i3 = new Outter.Inner3();

i3.n3 = 3;

Outter.Inner3.ns3 = 4;

Outter.Inner3.ms3();

i3.m3 ();

}

We can see static inner classes and static variables similar way, directly using the class name.

 

Members of the inner class:

// members of the inner class

public class Inner1{

public int n1;

public int o1;

public void m1(int o1) {

o1 = 1; // local variables

this.o1 = 2; // current attribute

Outter . This.o1 =. 4; // External Property

om1();

System.out.println("m1");

}

}

 

Members of the inner class use:

public static void main(String[] args) {

// transfer

Outter.Inner1 i1 = new Outter().new Inner1();

i1.n1 = 3;

i1.m1(3);

}

Because it is a member, so you need to create objects to use.

 

Local inner classes and anonymous inner classes: the equivalent of a local variable, and can only call methods create objects in it, you can call external variables and methods, external can not call it. Look at the following method:

public void test() {

// String n;

// local inner class

class Inner2{

public void m2() {

O1  = 3;

}

}

Inner2 i2 = new Inner2();

i2.m2();

 

// anonymous inner classes, at runtime will generate a temporary name, is similar: Outter $ 1.java

MyInter in = new MyInter() {

@Override

public void method() {

O1  = 3;

}

};

in.method();

}

So far, inner classes simply use it basically to the.

 

Guess you like

Origin www.cnblogs.com/qfchen/p/11165088.html