1 访问规则:
非静态内部类中不能含有静态成员
静态成员不能访问非静态成员(该条规则不仅仅适用于内部类)
静态内部类是属于外部类的类相关,不属于任何实例。
代码说明:
public class TextMain04 {
/**
* 在非静态方法中测试与内部类的访问规则
*/
public void text01() {
/**
* 非静态内部类
*/
new InnerClass().text01();
/**
* 静态内部类
*/
// 静态内部类可直接通过类名调用静态方法,
new TextMain04.StaticClass().text02();
// 也可采用如下的方法
new StaticClass().text01();
new StaticClass().text02();
}
/**
* 在静态方法中测试与内部类的访问规则
*/
public static void text02() {
/**
* 非静态内部类
*/
// 静态方法中不能调用非静态内部类中的方法
//new InnerClass().text01();
/**
* 静态内部类
*/
// 静态内部类可直接通过类名调用静态方法,
StaticClass.text02();
// 也可采用如下的方法
new StaticClass().text01();
new StaticClass().text02();
}
/**
* 非静态内部类
*
* @author Tang
*
*/
public class InnerClass {
public void text01() {
}
// 非静态内部类中不能存在静态方法
// public static void text02(){}
}
/**
* 静态内部类
*
* @author Tang
*
*/
public static class StaticClass {
public void text01() {
}
public static void text02() {
}
}
}