四、方法内部类
方法内部类只在该方法内部可见,方法内部类可以定义在方法中的任何位置。
/**
* 内部类实现接口
*
* @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);
}
}
这个应用给了我们很多思考,我就不说了,不同人看了会有不同的感受。
内部类的巧妙使用会让你的代码很牛,如果要形容下,那就是:没看懂的时候感觉神出鬼没,看懂后感觉鬼斧神工。不过这些代码多了,别人想看懂都难,想看懂你思路就难上加难了。呵呵!
六、静态内部类
静态内部类是static class型的内部类,这种内部类特点是:它不能访问外部类的非静态成员。要创建静态内部类对象时候,也不需要外部类对象了,直接可以:
new 外部类名.内部类构造方法
来创建,给个例子:
/**
* 静态内部类
*
* @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
|
|