Java内部类总结

Java内部类其实在J2EE编程中使用较少,不过在窗口应用编程中特别常见,主要用来事件的处理。其实,做非GUI编程,内部类完全可以不用。
 
内部类的声明、访问控制等于外部类有所不同,要灵活使用内部类来编写程序,还是有相当难度的,Java发明了这种难懂的玩意儿,在其他语言中是没有的,但是在Java中,内部类也相当的重要,尤其做GUI开发时候,事件的响应处理全靠内部类了。
 
内部类所做的功能使用外部类也同样可以实现,只是有时候内部类做的更巧妙些。
 
内部类按照其所在位置不同,可分为以下几种:
1、(普通的)内部类(最常见的内部类,内部类的定义与类成员平级,)
2、方法内部类
3、匿名类
4、静态内部类
5、接口内部类

这里指介绍普通内部类和匿名类
一、内部类声明与访问
 
1、内部类直接在类的内部进行声明。可以声明为private、protected、public或者默认访问权限,这个访问权限约定和属性的访问权限完全一样。跟外部类的访问权限不一样,外部类只能用默认和public访问权限
 
2、内部类自动拥有对其外围类所有成员(方法、属性)的访问权。如果内部类和外部类成员的名字完全相同,在内部类方法中要访问外部类成员,则需要使用下面的方式来访问: 外部类名.this.外部成员名,例如Outer.this.i++;  (看例子)
 
3、必须使用外部类对象来创建内部类对象,而不是直接去new一个。
格式为: 外部对象名.new 内部类构造方法
 
比如要创建一个内部类iner对象,需要这么做:
        Outer outer = new Outer();
        Outer.Inner iner = outer.new Inner();
package yuankai;

public class Outer {

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

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

运行结果:
调用Outer构造方法:outer
调用Inner构造方法:inner
>>>>>Inner class!
Outer class!
11
-------1--------
调用Inner构造方法:inner
>>>>>Inner class!
Outer class!
1001
-------2--------
12

二、匿名类
匿名类不给出类名,直接定义一个类,通常这个类实现了某种接口或者抽象或者继承了一个类。其实就是多态,父类引用指向子类对象。
在一些多线程程序中比较常见
匿名类最直接的特性是在构造方法后面加上大括号{}
public class TestInner2 {
	public static void main(String[] args) {
		Thread t = new Thread() {
			public void run() {
				//详细方法
			}
		};
		t.start();
	}
}

还有一个不得不提的经典实例,来自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); 
    } 
}

转载地址: http://lavasoft.blog.51cto.com/62575/179484/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值