Defining a class inside another class is an inner class.
The basic inner class.
public class Innerbasic {
private int id;
protected String name;
static int age;
Abstract class Inner {
public String inner;
void Test () {
This.inner = name;
}
}
Private class PInner {
}
Final class Inners {
String iname = name;
int id = new Innerbasic (). ID;
Innerbasic Getout () {
return innerbasic.this; Returns a reference to an external class object
}
}
Static Class Innert {
int IDT = age;
String s = new Innerbasic (). Name;
Object Test () {
Class Testt {
void xxx () {
SYSTEM.OUT.PRINTLN ("xxx");
}
}
return new Testt ();
}
}
public static void Test1 () {
Innert inner = new Innert (); Static
Inners inners = new Innerbasic (). New Inners ();
}
public void Test () {
Innert Innert = new Innert (); Static
Innerbasic.inners inner = new Innerbasic.inners ();
}
}
Local inner class
Package com.javase.innerclass;
public class Methodinnerclass {
Public IService GetService (final String s) {
Final int count = 100; Can use count cannot be modified, final/here looks like a closure, but completely different. This method ends, and the count scope ends.
Final person person = new person ();
Class Innerservice implements IService {
String name;
Innerservice (String name) {
THIS.name = name;
}
@Override
public void Test () {
THIS.name = s;
System.out.println ("Method inner test" + count + name +person.province);
}
}
return new Innerservice (s);
}
}
Anonymous inner class//For anonymous classes, the actual effect of instance initialization is the constructor. Instance initialization methods cannot be overloaded, so there can be only one constructor.
Anonymous inner classes are somewhat restricted compared to formal inheritance, because anonymous inner classes can extend classes or implement interfaces, but not both.
public class Anonymityclass {
public void print () {
System.out.println ("print");
}
IService Test () {
New Anonymityclass () {
}.print ();
comparator<person> Comparator = new Comparator<person> () {
@Override
public int Compare (person O1, person O2) {
Return (O1.id > O2.id 1:-1);
}
};
list<person> list = new arraylist<person> ();
Collections.sort (list, comparator);
return new IService () {
@Override
public void Test () {
}
};
}
}
Java Internal classes