原创|其它|编辑:郝浩|2010-02-23 10:44:00.000|阅读 782 次
概述:本文将为大家介绍Java内部类,内部类主要用于GUI设计方面,平时大家可能接触不多,但了解内部类还是很有的。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
四、方法内部类
方法内部类只在该方法内部可见,方法内部类可以定义在方法中的任何位置。
运行结果:
say foo!
say bar!
Process finished with exit code 0
五、匿名类
匿名类不给出类名,直接定义一个类,通常这个类实现了某种接口或者抽象。匿名类的访问权限更没有讨论价值了,看个例子就行了。
在一些多线程程序中比较常见,有点变态,呵呵。
运行结果:
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,有改动:
这个应用给了我们很多思考,我就不说了,不同人看了会有不同的感受。
内部类的巧妙使用会让你的代码很牛,如果要形容下,那就是:没看懂的时候感觉神出鬼没,看懂后感觉鬼斧神工。不过这些代码多了,别人想看懂都难,想看懂你思路就难上加难了。呵呵!
六、静态内部类
静态内部类是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
七、接口内部类
接口内部类自动都是public static的,相当于为接口定义了一种变量类型,这在java的设计中就有使用,比如在HashMap中,就有:
static class Entry implements Map.Entry
下面我给个例子,
八、内部的类的嵌套
所谓内部类嵌套,就是内部类里面再定义内部类。其实这种用法还真没见过,试试写个简单例子看看吧:
运行结果:
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
总结:
内部类是Java中最复杂深奥的概念之一,而且内部类在访问控制,修饰符,继承,实现,抽象,序列化等等很多方面都是一个很让人迷惑的问题,在实际中,这些问题也许永远没机会没时间搞清,但是一般说来,懂得以上的内部类的知识就足够用了。
内部类的设计也许是弥补Java语言本身的先天不足吧,作为语言来说,这个特性太变态了点,难道就没别的法了?
以上的总结完全是建立在实践基础上的,所列举的例子也许偏颇,不能全面反映问题的本质,希望有兴趣的博友多多发表自己的看法与观点。
/** * 嵌套内部类 * * @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(); } } |
/** * 接口内部类 * * @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(); } } |
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 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(); } |
/** * 内部类实现接口 * * @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; } } |
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:网络转载