Java内部类使用总结

Java内部类其实在J2EE编程中使用较少,不过在窗口应用编程中特别常见,主要用来事件处理。其实,做非GUI编程,内部类完全可以不用。

内部类的声明、访问控制和外部类有所不同,要灵活使用内部类来编写程序,还是有相当难度的,Java发明了这种难懂的玩意儿,在其他语言中是没有的,但是在Java中,内部类也相当的重要,尤其做GUI开发时候,事件的响应处理全靠内部类了。

内部类所做的功能使用外部类也同样可以实现,只是有时候内部类做的更巧妙些。

内部类按照其所在位置不同,可分为以下几种:

1、普通内部类(最常见的内部类,内部类的定义与类成员平级,)
2、方法内部类
3、匿名类
4、静态内部类
5、接口内部类

 

1). 内部类声明与访问

1、内部类直接在类的内部进行声明。可以声明为private、protected、public或者默认访问权限,这个访问权限约定和外部类的成员变量完全一样。

2、内部类自动拥有对其外围类所有成员(方法、属性)的访问权。如果内部类和外部类成员的名字完全相同,在内部类方法中要访问外部类成员,则需要使用下面的方式      来访问:外部类名.this.外部成员名,例如Outer.this.i++;(见示例)

3、必须使用外部类对象来创建内部类对象,而不是直接去new一个。

格式为:外部对象名.new 内部类构造方法,比如要创建一个内部类iner对象,需要这么做:

Outer outer = new Outer();
Outer.Inner inner = new outer.Inner();

public classOuter {
    private int i = 10;
   privateinty = 8;
   Outer() {
    System.out.println("调用Outer构造方法:outer");
   }
   publicvoidsayMsg() {
    System.out.println("Outer class!");
   }
   classInner {
    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() {
        returni;
        }
   }
   publicvoidtest() {
    Inner in = new Inner();
        in.innerMsg();//外部类中要访问内部类方法,必须通过内部类对象来访问
   }
   publicintgetI() {
        returni;
   }
   publicintgetY() {
        returny;
   }
}
public classTest1 {
    public static void main(String[] args) {
        Outerouter = newOuter();//调用Outer构造方法:outer
        outer.test();//调用Inner构造方法:inner;>>>>>Inner class!;Outer class!
        System.out.println(outer.getI());//11
        System.out.println("-------1--------");
 
        Outer.Inner iner = outer.new Inner();//调用Inner构造方法:inner
        iner.innerMsg();//>>>>>Inner class!
        System.out.println(iner.getI());//1001
        System.out.println("-------2--------");
 
        System.out.println(outer.getI());//12
        System.out.println(outer.getY());//6
   }
}

2).内部类与接口

1、内部类可以实现接口。

2、内部类之间相互可见,但并非内部类之间方法都可见。

public interfaceFoo {
    void say();
}
public interfaceBar {
   voidreadme();
}
public classTest2 {
   publicstaticvoidmain(String[] args) {
    Outer outer = new Outer();
        Foo f = outer.genFoo();
        Bar b = outer.genBar();
        f.say();//say foo!
        b.readme();//say bar!
   }
}
class Outer {
   privateclassFooImplimplementsFoo {
    public void say() {
        System.out.println("say foo!");
        }
   }
   privateclassBarImplimplementsBar {
    public void readme() {
        System.out.println("say bar!");
        }
   }
   publicFoo genFoo() {
    return new FooImpl();
   }
   publicBar genBar() {
    return new BarImpl();
   }
}

3). 访问权限

外部类分两种:
一种嵌入了内部类声明代码外部类,称为直接外部类。
另一种是与内部类没有任何关系的外部类,称为外部类。

在同一个直接外部类中,内部类之间所有的方法都是相互可见的,包括在直接外部类的main()中可见。

在外部类中,要看到一个类的内部类成员,则至少要求这个内部类的class和成员权限大于或等于protected。

class Outer {
    protected class Foo {
        protected void say() {
            System.out.println("say foo!");
        }
        private void test() {
            System.out.println("----test------");
        }
   }
   protectedclassBar {
        protected void readme() {
            System.out.println("say bar!");
            new Foo().test();
        }
   }
   publicBar genBar() {
        return new Bar();
   }
}
public classTest1 {
    public static void main(String[] args) {
        Outero = newOuter();
        Outer.Bar b = o.genBar();
        b.readme();
   }
}

4). 方法内部类

方法内部类只在该方法内部可见,方法内部类可以定义在方法中的任何位置。

class Outer {
    public Foo genFoo() {
        //方法内的内部类
        class FooImplimplements Foo {
        public void say() {
             System.out.println("say foo!");
            }
        }
        return new FooImpl();
    }
    public Bar genBar() {
        Bar b = null;
        if (true) {
        //任意位置的内部类
            class BarImplimplements Bar {
              public void readme() {
                  System.out.println("say bar!");
                }
            }
            b = new BarImpl();
        }
        return b;
    }
}
public classTest1 {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Foo f = outer.genFoo();
        Bar b = outer.genBar();
        f.say(); //say foo!
        b.readme(); //say bar!
    }
}

5).匿名类

匿名类不给出类名,直接定义一个类,通常这个类实现了某种接口或者抽象。在一些多线程程序中比较常见。

示例1:

public classTest1 {
    public Foof =new Foo() {
        public void say() {
            System.out.println("O(∩_∩)O哈哈~!");
        }
   };
   publicFoo test() {
        return new Foo() {
            public void say() {
              System.out.println("say foo!");
            }
        };
   }
   publicstaticvoidmain(String[] args) {
        Test1 t = new Test1();
        t.f.say();
        t.test().say();
   }
}
interface Foo {
    void say();
}

示例2:

public classFk {
    private Stringx;
    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.toString());
   }
}

示例3:

interface Service {
   voidmethod1();
   voidmethod2();
}
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 =newServiceFactory() {
    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 =newServiceFactory() {
    public Service getService() {
        return new Implementation2();
        }
   };
}
public classFactories {
   public static void serviceConsumer(ServiceFactory fact) {
        Service s = fact.getService();
        s.method1();
        s.method2();
   }
   public static void main(String[] args) {
        serviceConsumer(Implementation1.factory);//Implementation1 method1;Implementation1 method2
        serviceConsumer(Implementation2.factory);//Implementation2 method1;Implementation2 method2
   }
}

6).静态内部类

静态内部类是static class型的内部类,这种内部类特点是:它不能访问外部类的非静态成员。要创建静态内部类对象时候,也不需要外部类对象了,直接可以:new 外部类名.内部类构造方法。

 public classOuter {
    public static int i =500;
   protected static class Inner {
        int i =100;
        Stringname;
       
        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.Innerin1 = newOuter.Inner("1111");
        in1.sayHello();
        System.out.println(Outer.i);
 
        Outer.Inner in2 = new Outer().genInner("2222");
        in2.sayHello();
        System.out.println(Outer.i);
   }
}


7).接口内部类

接口内部类自动都是publicstatic的,相当于为接口定义了一种变量类型,这在java的设计中就有使用,比如在HashMap中,就有:

static class Entry<K,V>implements Map.Entry<K,V>
 
public interface AInterface {
    void readme();
 
   class Inner1implements AInterface {
    public void readme() {
        System.out.println("我是一个接口内部类");
        }
   }
}
 
class Main {
    public static void main(String[] args) {
        AInterface.Inner1in1 = newAInterface.Inner1();
        in1.readme();
   }
}

8. 内部的类的嵌套

所谓内部类嵌套,就是内部类里面再定义内部类。其实这种用法还真没见过,试试写个简单例子看看吧:

public classOuter {
    private void f0() {
        System.out.println("f0");
   }
    classA {
        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) {
        Outero = newOuter();
        Outer.A a = o.new A();
        Outer.A.B b = a.new B();
        b.b();//f0;a;b
   }
}

9).内部类的继承

内部类的继承,可以继承内部类,也可以继承外部类。

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();
   }
}


转自:http://www.cnblogs.com/devinzhang/archive/2012/02/09/2344059.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值