package com.xp.pro;
public class Demo1 {
public static void main(String[] args) {
// 非静态内部类需要先定义外部类对象然后创建内部类对象
Out.Inner1 inner1 = new Out().new Inner1();
inner1.show();
// 静态内部类不需要创建外部类对象
Out.Inner2 inner2 = new Out.Inner2();
inner2.show();
}
}
//外部类的访问修饰符 只能是 public 或者 default 默认值
//而内部类可以看作外部类的成员 可以像其他成员一样加访问修饰符
class Out{
int x = 0;
static int y = 0;
class Inner1{
// 非静态内部类中不能定义静态变量,但是可以定义常量
// static int a = 0;
static final int a = 0;
int n = 0;
public void show(){
final int i = 0;
System.out.println("x = "+x);
System.out.println("y = "+y);
System.out.println("===========");
//局部内部类 --定义在方法体中的类 就像局部变量一样不能加访问修饰符
class Inner3{
public void doShow(){
System.out.println("x = "+x);
//访问局部内部类外部的局部变量要使用final修饰符修饰
System.out.println("i = "+i);
}
}
Inner3 inner3 = new Inner3();
inner3.doShow();
}
}
static class Inner2{
// 静态内部类中可以定义静态变量和常量
static int a = 0;
static final int b = 0;
int m = 0;
public void show(){
// 静态内部类不能访问外部类的非静态成员
// System.out.println("x = "+x);
System.out.println("y = "+y);
System.out.println("===========");
}
}
}