内部类按照其所在位置不同,可分为以下几种:
1、(普通的)内部类(最常见的内部类,内部类的定义与类成员平级,)
2、方法内部类
3、匿名类
4、静态内部类
5、接口内部类
2、方法内部类
3、匿名类
4、静态内部类
5、接口内部类
一、内部类声明与访问
1、内部类直接在类的内部进行声明。可以声明为private、protected、public或者默认访问权限,这个访问权限约定和外部类完全一样。
2、内部类自动拥有对其外围类所有成员(方法、属性)的访问权。如果内部类和外部类成员的名字完全相同,在内部类方法中要访问外部类成员,则需要使用下面的方式来访问:外部类名.this.外部成员名,例如Outer.this.i++; (看例子)
3、必须使用外部类对象来创建内部类对象,而不是直接去new一个。
格式为:外部对象名.new 内部类构造方法
比如要创建一个内部类iner对象,需要这么做:
Outer ot= new Outer();
Outer.Inner inn = ot.new Inner();
Outer.Inner inn = ot.new Inner();
class Outer {
private int i=10;
private int y=8;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
Outer() {
System.out.println("调用Outer构造方法:outer");
}
public void sayMsg() {
System.out.println("Outer class!");
}
class Inner {
int i = 1000;
Inner() {
System.out.println("调用Inner构造方法:inner");
}
void innerMsg() {
System.out.println("Inner class!");
sayMsg();
//访问内部类自己的成员i,也可以写成 this.i++
this.i++;
//访问外部类的成员 i和y
Outer.this.i+=2;
y--;
}
int getI() {
return i;
}
}
public void test() {
Inner in = new Inner();
in.innerMsg();
}
}
public class lzwCode {
public static void main(String [] args) {
Outer ot = new Outer();
ot.test();
System.out.println(ot.getI());
System.out.println("============1==============");
Outer.Inner inn = ot.new Inner();
inn.innerMsg();
System.out.println(inn.getI());
System.out.println("============2==============");
System.out.println(ot.getI());
}
}
控制台结果:
二、内部类与接口
1、内部类可以实现接口。
2、内部类之间相互可见,但并非内部类之间方法都可见。
interface Foo {
void say();
}
interface Bar {
void readMe();
}
class Outer {
private class FooImpl implements Foo {
public void say() {
System.out.println("Say Foo!");
}
}
private class BarImpl implements Bar {
public void readMe() {
System.out.println("read Bar!");
}
}
public Foo getFoo() {
return new FooImpl();
}
public Bar getBar() {
return new BarImpl();
}
}
public class lzwCode {
public static void main(String [] args) {
Outer outer = new Outer();
Foo f = outer.getFoo();
Bar b = outer.getBar();
f.say();
b.readMe();
}
}
控制台结果:
三、访问权限
外部类分两种:
一种嵌入了内部类声明代码外部类,称为直接外部类。
另一种是与内部类没有任何关系的外部类,称为外部类。
一种嵌入了内部类声明代码外部类,称为直接外部类。
另一种是与内部类没有任何关系的外部类,称为外部类。
在同一个直接外部类中,内部类之间所有的方法都是相互可见的,包含在直接外部类的main()中可见。
在外部类中,要看到一个类的内部类成员,则至少要求这个内部类的class和成员权限大于或等于protected。
class Outer {
protected class Foo {
protected void say() {
System.out.println("Say Foo!");
}
private void test() {
System.out.println("Say test!");
}
}
protected class Bar {//注意访问权限
protected void readMe() { //注意访问权限
System.out.println("Say Bar!");
new Foo().test();
new Foo().say();
}
}
public Foo getFoo() {
return new Foo();
}
public Bar getBar() {
return new Bar();
}
}
public class lzwCode {
public static void main(String [] args) {
Outer outer = new Outer();
Outer.Bar bar = outer.getBar();
bar.readMe();
}
}
控制台结果:
四、方法内部类
方法内部类只在该方法内部可见,方法内部类可以定义在方法中的任何位置。
interface Foo {
void say();
}
interface Bar {
void readMe();
}
class Outer {
public Foo getFoo() {
//方法内的内部类
class FooImpl implements Foo {
public void say() {
System.out.println("Say foo!");
}
}
return new FooImpl();
}
public Bar getBar() {
Bar b = null;
if (true) {
//任意位置的内部类
class BarImpl implements Bar {
public void readMe() {
System.out.println("Say Bar!");
}
}
b = new BarImpl();
}
return b;
}
}
public class lzwCode {
public static void main(String [] args) {
Outer outer = new Outer();
Foo f = outer.getFoo();
Bar b = outer.getBar();
f.say();
b.readMe();
}
}
控制台结果: