对于Java内部类,大家实际上了解不多。在这里我们以实际代码的形式,为大家详细介绍Java内部类在GUI设计的作用。
Java 内部类其实在J2EE编程中使用较少, 不过在窗口应用编程中特别常见,主要用来事件的处理。其实,做非GUI编程,内部类完全可以不用。
内部类的声明、访问控制等于外部类有所不同,要灵活使用内部类来编写程序,还是有相当难度的,Java发明了这种难懂的玩意儿,在其他语言中是没有 的,但是在Java中,内部类也相当的重要,尤其做GUI开发时候,事件的响应处理全靠内部类了。
内部类所做的功能使用外部类也同样可以实现,只是有时候内部类做的更巧妙些。
内部类按照其所在位置不同,可分为以下几种:
1、(普通的)内部类(最常见的内部类,内部类的定义与类成员平级,)
2、方法内部类
3、匿名类
4、静态内部类
5、接口内部类
一、内部类声明与访问
1、内部类直接在类的内部进行声明。可以声明为private、protected、public或者默认访问权限,这个访问权限约定和外部类完全 一样。
2、内部类自动拥有对其外围类所有成员(方法、属性)的访问权。如果内部类和外部类成员的名字完全相同,在内部类方法中要访问外部类成员,则需要使 用下面的方式来访问:外部类名.this.外部成员名,例如Outer.this.i++; (看例子)
3、必须使用外部类对象来创建内部类对象,而不是直接去new一个。
格式为:外部对象名.new 内部类构造方法
比如要创建一个内部类iner对象,需要这么做:
- Outer outer = new Outer();
- 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
二、内部类与接口
1、内部类可以实现接口。
2、内部类之间相互可见,但并非内部类之间方法都可见。
- 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
三、访问权限
外部类分两种:
一种嵌入了内部类声明代码外部类,称为直接外部类。 另一种是与内部类没有任何关系的外部类,称为外部类。
在同一个直接外部类中,内部类之间所有的方法都是相互可见的,包含在直接外部类的main()中可见。
在外部类中,要看到一个类的内部类成员,则至少要求这个内部类的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();
- }
- }
本问转自 http://developer.51cto.com/art/201002/183375.htm
感觉写的不错,收藏一下,希望对需要的朋友有帮助