The inner nested class is widely used class java in order to avoid difficult to understand, with a few small examples herein, description of its use.
And the internal structure of nested classes below the class
Static nested classes
Static nested classes, declared in a nested class outside the class, because it is static, it is not initialized, you can pass the class name, called directly.
1 class out1{
2 private static String name = "xingoo";
3 static class info{
4 void print(){
5 System.out.println("name:"+name);
6 }
7 }
8 }
Use Case
package testClass;
class out1{
private static String name = "xingoo";
static class info{
void print(){
System.out.println("name:"+name);
}
}
}
class out2{
private static String name = "xingoo test 2";
static class info{
String name = "inner infomation!";
void printInner(){
System.out.println("name:"+name);
}
void printOuter(){
System.out.println("name:"+out2.name);
}
}
}
/**
* 静态嵌套类
* @author xingoo
*
*/
public class test1 {
public static void main(String[] args){
//创建对象时,以xxx.yyy这种格式创建
out1.info oi = new out1.info();
oi.print();
out2.info oi2 = new out2.info();
oi2.printInner();
oi2.printOuter();
}
}
Members of the inner class
Members of the inner class, that class as a member of another class, so only a reference to another class to create the class.
1 class outer{
2 private String name = "xingoo";
3 class inner{
4 void print(){
5 System.out.println(name);
6 }
7 }
8 }
Usually implement an interface for concealment, as shown in the following, first it is the traditional implementation, an implementation of the second hidden interface implementation.
1 interface Printer{
2 void print(String name);
3 }
4 class outerImpl1 implements Printer{
5 @Override
6 public void print(String name) {
7 System.out.println(name);
8 }
9 }
10 class outerImpl2{
11 private class inner implements Printer{
12 @Override
13 public void print(String name) {
14 System.out.println(name);
15 }
16 }
17 public Printer getPrinter(){
18 return new inner();
19 }
20 }
Use Case
package testClass;
/**
* 简单测试成员内部类
* @author xingoo
*
*/
class outer{
private String name = "xingoo";
class inner{
void print(){
System.out.println(name);
}
}
}
interface Printer{
void print(String name);
}
class outerImpl1 implements Printer{
@Override
public void print(String name) {
System.out.println(name);
}
}
class outerImpl2{
private class inner implements Printer{
@Override
public void print(String name) {
System.out.println(name);
}
}
public Printer getPrinter(){
return new inner();
}
}
/**
* 成员内部类
* @author xingoo
*
*/
public class test2 {
public static void main(String[] args){
//创建一个外部类的对象,通过调用这个对象的new方法创建其内部类的对象
outer o = new outer();
outer.inner i = o.new inner();
i.print();
//内部类实现接口隐藏
Printer out1 = new outerImpl1();
out1.print("xingoo test1");
outerImpl1 out1trans = (outerImpl1)out1;//支持向下转换
Printer out2 = (new outerImpl2()).getPrinter();
out2.print("xingoo test2");
//无法向下转换,因为inner是private类型的,这样就实现了接口的隐藏
}
}
Partial inner class
The effect of local class declaration within a block of code, and therefore if a declaration of the class is only used by a program, no longer less than elsewhere, can be achieved in this way. ·
1 interface Logger{
2 public void log(String message);
3 }
4 public class test3 {
5
6 String startTime = (new Date()).toString();
7 /**
8 * 局部内部类,从定义上来讲,不属于任何其他的类,因为是声明在这个类内部的。与匿名内部类不同的就是它有名字。
9 * @return
10 */
11 public Logger getLogger(){
12 //由于LoggerImpl只在 getLogger内部使用,因此把它定义在函数内部,相对来说更安全一些
13 class LoggerImpl implements Logger{
14 @Override
15 public void log(String message) {
16 System.out.println(startTime + ":" + message);
17 }
18 }
19 return new LoggerImpl();
20 }
21
22 public static void main(String[] args){
23 test3 test = new test3();
24 Logger logger = test.getLogger();
25 logger.log("hello xingoo!");
26 }
27 }
Anonymous inner classes
When you create an object using new, created its implementation class directly behind.
1 abstract class Printable{
2 public void print(String name){
3
4 }
5 }
6 /**
7 * 就是跟在new创建对象之后,直接定义它的类实现
8 * @author Administrator
9 */
10 public class test4 {
11 public static void main(String[] args){
12 Printable printer = new Printable(){
13 public void print(String name){
14 System.out.println(name);
15 }
16 };//注意因为这其实是一个new语法,因此要加上分号;
17 printer.print("hello!xingoo!");
18 }
19 }
Reference: "JAVA 7 Programming"
Reproduced in: https: //my.oschina.net/u/204616/blog/545127