天天看点

Java内部类 静态内部类和非静态内部类

创 建非静态内部类对象

Face f = new Face();

Face.Nose n = f.new Nose();

n.brith();      

通过简单的代码来实现内部类如何调用

创建非静态内部类对象

Face f = new Face();

Face.Nose n = f.new Nose();//非静态内部类

n.brith();                    

创建静态内部类对象

Face.Ear e = new Face.Ear();  //静态内部类

e.listen();

class Face{

String type="大脸";

static String color = "红润";

非静态内部类

class Nose{

String type="大鼻子";

void brith(){

System.out.println(color);  //非静态内部类可以调用静态的变量

System.out.println(Face.this.type);//

System.out.println("我会呼吸!");

}

}

静态内部类

static class Ear{

void listen(){

System.out.println(color); / /静态内部类可以访问静态变量,不能访问非静态变量

System.out.println("i can listen!");

}

}

}