大部分时候,类被定义成一个独立的成簇单元。在某些情况下,也会把一个类放在另一个类的内部定义,这个定义在其他内部的类被称为内部类(有的地方也叫嵌套类),包含内部类的类也被称为外部类(有的地方也叫宿主类);
内部类主要作用如下:
1. 内部类提供更好的封装,可以把内部类隐藏在外部类之内,不允许同一包中的其他类访问该类。
2. 内部类成员可以直接访问外部类的私有数据,应为内部类被当成其外部类的成员,同一个类的成员之间可以互相访问。
3. 匿名内部类适合用于创建那些仅需要一次使用的类。
内部类大致分为:非静态内部类,静态内部类,局部内部类和匿名内部类;
**
1.非静态内部类
**
(1).可直接访问外部类的所有数据;
(2)访问内部类,必须创建对象
①外部类访问,直接创建对象
②其他类访问,外部类名.内部类名 对象名 = 外部类对象.内部类对象;
(3)当变量名与外部类重名时,用引用区分
(4)私有内部类,在外部类创建get()方法创建内部类对象
(5)非静态内部类不能有静态成员
例1: 调用内部类,访问外部类成员
public class Test {
public static void main(String[] args) {
//外部类名.内部类名 对象名 = 外部类对象.内部类对象
A.B ab = new A().new B();
ab.print();
}
}
class A{
private int n =10; //外部类成员变量
int m = 10;
private void method() {
System.out.println("method");
}
class B {
//static int a=10; //非静态内部类不能有静态成员
public void print() { //内部类随意访问外部类成员
System.out.println("输出结果:");
System.out.println(n);
System.out.println(m);
method();
}
}
}
输出结果:
10
10
method
例2:私有内部类
class A{
private class B {
public void print() {
System.out.println("输出结果:");
System.out.println("私有内部类");
}
}
public void getB() { //创建get()方法
B b = new B(); //方法中生成内部类对象
b.print();
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
a.getB();
}
}
输出结果:
私有内部类
例3:变量名重名,引用区分
class A{
int m = 30;
class B {
int m = 20;
public void print() {
int m = 10;
System.out.println("输出结果:");
System.out.println(m); //局部变量
System.out.println(this.m); //内部类成员变量
System.out.println(A.this.m); //外部类成员变量
}
}
}
public class Test {
public static void main(String[] args) {
A.B ab = new A().new B();
ab.print();
}
}
输出结果:
10
20
30
2.静态内部类
(1)访问内部类
①外部类访问,直接(类名.)调用
②其他类访问,外部类名.内部类名 对象名 = 外部类名.内部类对象;
(2)静态内部类不可以直接访问外部类的非静态数据
class A{
int m = 30;
static class B {
public void print() {
System.out.println("输出结果:");
//System.out.println(m); //无法访问
System.out.println("静态内部类");
}
}
}
public class Test {
public static void main(String[] args) {
A.B ab = new A.B(); //A.B ab = A.new B();
ab.print(); //两者等效,前者更符合书写习惯;
}
}
输出结果:
静态内部类
3.局部内部类
定义在方法体中,相当于局部变量
局部内部类访问所在方法的变量必须用final修饰
为什么?
答:当调用这个方法时,不修饰则随方法共存亡;如果用fianl修饰,会在类加载的时候进入常量池,即使方法贪占消失,常量池仍可使用。
class A{
int m = 30;
public void method() {
final int n = 10;
class B {
public void print(){
System.out.println("输出结果:");
System.out.println(n);
System.out.println(m);
}
}
B b = new B();
b.print();
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
a.method();
}
}
输出结果:
10
30
4.匿名内部类(局部内部类的一种)
前提:存在一个类或者接口
本质:匿名内部类当做参数传递(本质把匿名内部类作为一个对象)
作用:适合创建那种只需要一次使用的类
例1:
interface C {
public void print();
}
class A{
public void method(){
new C() { //实现该接口,创建接口的子类对象
public void print() {
System.out.println("输出结果:");
System.out.println("匿名内部类1");
}
}.print(); //编译看父类,运行看子类
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
a.method();
}
}
输出结果:
匿名内部类1
**例2:
理解更清晰,多态,父类引用指向子类对象**
interface C {
public void print();
}
class A{
public C method(){
C c = new C() {
public void print() {
System.out.println("输出结果:");
System.out.println("匿名内部类2");
}
};
return c;
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
a.method().print();
}
}
输出结果:
匿名内部类2