java嵌套类和内部类、匿名类
1、嵌套类和内部类定义
可以在一个类的内部定义另一个类,这种类称为嵌套类(nested classes),它有两种类型:静态嵌套类和非静态嵌套类。静态嵌套类使用很少,最重要的是非静态嵌套类,也即是被称作为内部类(inner)。其中inner类又可分为三种:1)在一个类(外部类)中直接定义的内部类;
2)在一个方法(外部类的方法)中定义的内部类;
3)匿名内部类。
2、静态嵌套类
<pre name="code" class="java">public class StaticInnerClassTest {运行结果:
private static String msg="Hello";
private String city="Beijing";
static class Person {
private String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void displayMsg(){
//只能访问外部类的静态变量
System.out.println("Msg="+msg);
//System.out.println("City=" + city);
}
}
public void printInfo(){
Person person = new Person("jack",23);
System.out.println("#################");
person.displayMsg();
System.out.println("#################");
System.out.println("name ="+person.name);
System.out.println("age ="+person.age);
}
public static void main(String[] args) {
StaticInnerClassTest test=new StaticInnerClassTest();
test.printInfo();
}
}
#################
Msg=Hello
#################
name =jack
age =23
特点:1、静态方法不能直接访问非静态成员(静态方法不能直接访问非静态成员)。 2、外部类访问内部类的的成员有些特别,不能直接访问,但可以通过内部类直接访问,这是因为静态嵌套内的所有成员和方法默认为 静态变量和静态方法。 3、内部静态类Person只在类StaticInnerClassTest内访问。
3、内部类
public class InnerClassTest {运行结果: ################## Inner display
private String outerMsg= "outer Hello";
class Inner {
public String innerPublicMsg = "inner public msg:Hello";
private String innerPrivateMsg = "inner private msg:Hello";
public void display() {
System.out.println("\n################## Inner display");
System.out.println("Inner display(Outer):" + outerMsg);
}
}
void printInfo() {
Inner inner = new Inner();
inner.display();
System.out.println("\n################## Outer printInfo");
System.out.println("Inner(Public):" + inner.innerPublicMsg);//可以访问
System.out.println("Inner(Private):" + inner.innerPrivateMsg);//可以访问
// System.out.println("Inner(Private):" + innerPrivateMsg);//不可以访问
InnerTwo innerTwo = new InnerTwo();
innerTwo.show();
}
class InnerTwo {
Inner inner = new Inner();
public void show() {
inner.display();//可以访问
System.out.println("\n################## InnerTwo show");
System.out.println("Inner(Public):" + inner.innerPublicMsg);//可以访问
System.out.println("Inner(Private):" + inner.innerPrivateMsg);//可以访问
// System.out.println("Inner(Private):" + innerPrivateMsg);//不可以访问
}
}
public static void main(String args[]) {
InnerClassTest outer = new InnerClassTest();
outer.printInfo();
}
}
Inner display(Outer):outer Hello
################## Outer printInfo
Inner(Public):inner public msg:Hello
Inner(Private):inner private msg:Hello
################## Inner display
Inner display(Outer):outer Hello
################## InnerTwo show
Inner(Public):inner public msg:Hello
Inner(Private):inner private msg:Hello
特点:1、可以访问外部类的所有成员。
2、外部类访问内部类的的成员通过内部类直接访问。
3、内部只在外部类和外部类的其它内部类内访问。
4、匿名内部类
abstract class Person {运行结果: ################# 常规
public abstract void showCareer();
}
public class Teacher extends Person{
@Override
public void showCareer() {
System.out.println("Career is Teacher");
}
}
public interface Tree {
void showTreeName();
}
public class AnonymousInnerClass {
public static void main(String[] args) {
System.out.println("################# 常规");
normal();
System.out.println("################# 覆盖父类方法");
extendsInner();
System.out.println("################# 抽象类");
abstractInner();
System.out.println("################# 接口");
interfaceInner();
}
//覆盖父类方法
private static void extendsInner() {
Person person = new Teacher() {
@Override
public void showCareer() {
System.out.println("Career is Worker");
}
};
person.showCareer();
}
//正常
public static void normal() {
Person person = new Teacher();
person.showCareer();
}
//匿名内部类 抽象类 实现
public static void abstractInner() {
Person person = new Person() {
@Override
public void showCareer() {
System.out.println("Career is lawyer");
}
};
person.showCareer();
}
//匿名内部类 接口 实现
public static void interfaceInner() {
Tree tree = new Tree() {
public void showTreeName() {
System.out.println("This is Apple Tree");
}
};
tree.showTreeName();
}
}
Career is Teacher
################# 覆盖父类方法
Career is Worker
################# 抽象类
Career is lawyer
################# 接口
This is Apple Tree