内部类:
1,概述
内部类是指在一个外部类的内部再定义一个类。
内部类作为外部类的一个成员,并且依附于外部类而存在的。
内部类可为静态,可用PROTECTED和PRIVATE修饰。(而外部类不可以:外部类只能
使用PUBLIC和DEFAULT)。
2,分类
成员内部类、
局部内部类、
静态内部类、
匿名内部类(图形是要用到,必须掌握)。
3,使用方法
A,成员内部类:
作为外部类的一个成员存在,与外部类的属性、方法并列。
成员内部类的优点:
⑴内部类作为外部类的成员,可以访问外部类的私有成员或属性。(即使将外部类声
明为PRIVATE,但是对于处于其内部的内部类还是可见的。)
⑵用内部类定义在外部类中不可访问的属性。这样就在外部类中实现了比外部类的
private还要小的访问权限。
注意:内部类是一个编译时的概念,一旦编译成功,就会成为完全不同的两类。
对于一个名为outer的外部类和其内部定义的名为inner的内部类。编译完成后出现
outer.class和outer$inner.class两类。
注意:当Outer是一个private类时,外部类对于其外部访问是私有的,所以就无法
建立外部类对象,进而也无法建立内部类对象。
B,局部内部类
在方法中定义的内部类称为局部内部类。
与局部变量类似,在局部内部类前不加修饰符public和private,其范围为定义它的
代码块。
注意:
在类外不可直接生成局部内部类(保证局部内部类对外是不可见的)。
要想使用局部内部类时需要生成对象,对象调用方法,在方法中才能调用其局部内部
类。
通过内部类和接口达到一个强制的弱耦合,用局部内部类来实现接口,并在方法中返
回接口类型,使局部内部类不可见,屏蔽实现类的可见性。
C,静态内部类:(注意:前三种内部类与变量类似,所以可以对照参考变量)静态内部类定义在类中,任何方法外,用static定义。
生成(new)一个静态内部类不需要外部类成员:这是静态内部类和成员内部类的区
别。静态内部类的对象可以直接生成:
Outer.Inner in=new Outer.Inner();
而不需要通过生成外部类对象来生成。这样实际上使静态内部类成为了一个顶级类。
静态内部类不可用private来进行定义。例子:
对于两个类,拥有相同的方法:
注意:当类与接口(或者是接口与接口)发生方法命名冲突的时候,此时必须使用内
部类来实现。
用接口不能完全地实现多继承,用接口配合内部类才能实现真正的多继承。
D,匿名内部类
匿名内部类是一种特殊的局部内部类,它是通过匿名类实现接口。
IA被定义为接口。
IA I=new IA(){};
匿名内部类的特点:
1,一个类用于继承其他类或是实现接口,并不需要增加额外的方法,只是对继承方法的事先或是覆盖。
2,只是为了获得一个对象实例,不许要知道其实际类型。
3,类名没有意义,也就是不需要使用到。
注:一个匿名内部类一定是在new的后面,用其隐含实现一个接口或实现一个类,没
有类名,根据多态,我们使用其父类名。
因其为局部内部类,那么局部内部类的所有限制都对其生效。
匿名内部类是唯一一种无构造方法类。
大部分匿名内部类是用于接口回调用的。
匿名内部类在编译的时候由系统自动起名Out$1.class。
如果一个对象编译时的类型是接口,那么其运行的类型为实现这个接口的类。
因匿名内部类无构造方法,所以其使用范围非常的有限。
当需要多个对象时使用局部内部类,因此局部内部类的应用相对比较多。匿名内部类
中不能定义构造方法。如果一个对象编译时的类型是接口,那么其运行的类型为实现
这个接口的类。
因匿名内部类无构造方法,所以其使用范围非常的有限。
举例说明:
Outer.Inner iner = outer.new Inner();
/**
* 内部类创建与初始化
*
* @author leizhimin 2009-7-17 13:51:52
*/
public class Outer {
private int i = 10;
private int y = 8;
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++;
y--;
}
int getI() {
return i;
}
}
public void test() {
Inner in = new Inner();
in.innerMsg();
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
class Test1 {
public static void main(String[] args) {
Outer outer = new Outer();
outer.test();
System.out.println(outer.getI());
System.out.println("-------1--------");
Outer.Inner iner = outer.new Inner();
iner.innerMsg();
System.out.println(iner.getI());
System.out.println("-------2--------");
System.out.println(outer.getI());
}
}
运行结果:
调用Outer构造方法:outer
调用Inner构造方法:inner
>>>>>Inner class!
Outer class!
11
-------1--------
调用Inner构造方法:inner
>>>>>Inner class!
Outer class!
1001
-------2--------
12
Process finished with exit code 0
public interface Foo{
void say();
}
public interface Bar {
void readme();
}
/**
* 内部类实现接口
*
* @author leizhimin 2009-7-17 14:57:50
*/
public class Test2 {
public static void main(String[] args) {
Outer outer = new Outer();
Foo f = outer.genFoo();
Bar b = outer.genBar();
f.say();
b.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("say bar!");
}
}
public Foo genFoo() {
return new FooImpl();
}
public Bar genBar() {
return new BarImpl();
}
}
输入结果:
say foo!
say bar!
Process finished with exit code 0
一种嵌入了内部类声明代码外部类,称为直接外部类。
另一种是与内部类没有任何关系的外部类,称为外部类。
在外部类中,要看到一个类的内部类成员,则至少要求这个内部类的class和成员权限大于或等于protected。
/**
* 内部类实现接口
*
* @author leizhimin 2009-7-17 14:57:50
*/
public class Test2 {
public static void main(String[] args) {
Outer o = new Outer();
Outer.Bar b = o.genBar();
b.readme();
}
}
class Outer {
protected class Foo {
protected void say() {
System.out.println("say foo!");
}
private void test() {
System.out.println("----test------");
}
}
protected class Bar {
protected void readme() {
System.out.println("say bar!");
new Foo().test();
}
}
public Foo genFoo() {
return new Foo();
}
public Bar genBar() {
return new Bar();
}
}
/**
* 内部类实现接口
*
* @author leizhimin 2009-7-17 14:57:50
*/
public class Test2 {
public static void main(String[] args) {
Outer outer = new Outer();
Foo f = outer.genFoo();
Bar b = outer.genBar();
f.say();
b.readme();
}
}
class Outer {
public Foo genFoo() {
//方法内的内部类
class FooImpl implements Foo {
public void say() {
System.out.println("say foo!");
}
}
return new FooImpl();
}
public Bar genBar() {
Bar b = null;
if (true) {
//任意位置的内部类
class BarImpl implements Bar {
public void readme() {
System.out.println("say bar!");
}
}
b = new BarImpl();
}
return b;
}
}
运行结果:
say foo!
say bar!
Process finished with exit code 0
/**
* 匿名类.
*
* @author leizhimin 2009-7-17 15:56:17
*/
public class Test3 {
public Foo f = new Foo() {
public void say() {
System.out.println("O(∩_∩)O哈哈~!");
}
};
public Foo test() {
return new Foo() {
public void say() {
System.out.println("say foo!");
}
};
}
public static void main(String[] args) {
Test3 t = new Test3();
t.f.say();
t.test().say();
}
}
interface Foo {
void say();
}
运行结果:
O(∩_∩)O哈哈~!
say foo!
Process finished with exit code 0
/**
* 普通类的匿名初始化
*
* @author leizhimin 2009-7-17 16:13:31
*/
public class Fk {
private String x;
public Fk(String x) {
this.x = x;
}
@Override
public String toString() {
return "Fk{" +
"x='" + x + '\'' +
'}';
}
}
class Test4 {
public Fk hehe() {
//把后面的一对大括号去掉呢,呵呵
return new Fk("fk") {
};
}
public static void main(String[] args) {
Test4 t = new Test4();
Fk f = t.hehe();
System.out.println(f);
}
}
运行结果:
Fk{x='fk'}
Process finished with exit code 0
还有一个不得不提的经典实例,来自thining in java,有改动:
interface Service {
void method1();
void method2();
}
interface ServiceFactory {
Service getService();
}
class Implementation1 implements Service {
private Implementation1() {}
public void method1() {System.out.println("Implementation1 method1");}
public void method2() {System.out.println("Implementation1 method2");}
public static ServiceFactory factory = new ServiceFactory() {
public Service getService() {
return new Implementation1();
}
};
}
class Implementation2 implements Service {
private Implementation2() {}
public void method1() {System.out.println("Implementation2 method1");}
public void method2() {System.out.println("Implementation2 method2");}
public static ServiceFactory factory = new ServiceFactory() {
public Service getService() {
return new Implementation2();
}
};
}
public class Factories {
public static void serviceConsumer(ServiceFactory fact) {
Service s = fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args) {
serviceConsumer(Implementation1.factory);
serviceConsumer(Implementation2.factory);
}
}
/**
* 静态内部类
*
* @author leizhimin 2009-7-17 16:53:05
*/
public class Outer {
public static int i =500;
protected static class Inner {
int i =100;
String name;
Inner(String name) {
this.name = name;
}
void sayHello() {
System.out.println("Hello " + name);
Outer.i++;
}
}
public Inner genInner(String name) {
return new Inner(name);
}
}
class Test {
public static void main(String[] args) {
Outer.Inner in1 = new Outer.Inner("1111");
in1.sayHello();
System.out.println(Outer.i);
Outer.Inner in2 = new Outer().genInner("2222");
in2.sayHello();
System.out.println(Outer.i);
}
}
运行结果:
Hello 1111
501
Hello 2222
502
Process finished with exit code 0
/**
* 接口内部类
*
* @author leizhimin 2009-7-17 17:20:28
*/
public interface AInterface {
void readme();
class Inner1 implements AInterface {
public void readme() {
System.out.println("我是一个接口内部类");
}
}
}
class Main {
public static void main(String[] args) {
AInterface.Inner1 in1 = new AInterface.Inner1();
in1.readme();
}
}
/**
* 嵌套内部类
*
* @author leizhimin 2009-7-17 17:33:48
*/
public class Outer {
private void f0() {
System.out.println("f0");
}
class A {
private void a() {
f0();
System.out.println("a");
}
class B {
protected void b() {
a();
System.out.println("b");
}
}
}
}
class Test{
public static void main(String[] args) {
Outer o = new Outer();
Outer.A a = o.new A();
Outer.A.B b = a.new B();
b.b();
}
}
运行结果:
f0
a
b
Process finished with exit code 0
/**
* 内部类的继承,可以继承内部类,也可以继承外部类
*
* @author leizhimin 2009-7-22 13:50:01
*/
public class Outer {
class Inner {
void doSomething() {
System.out.println("Inner doing ...");
}
}
class Inner2 extends Inner {
void doSomething() {
System.out.println("Inner2 doing ...");
}
void readme() {
System.out.println("HeHe!");
}
}
}
class Test {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner in = outer.new Inner();
Outer.Inner2 in2 = outer.new Inner2();
in.doSomething();
in2.doSomething();
in2.readme();
}
}
运行结果:
Inner doing ...
Inner2 doing ...
HeHe!
Process finished with exit code 0