InnerClass、StaticInnerClass、PartInnerClass、AnonymousInnerClass

package com.kilophone.classcontent;

/**
 * @author Administrator 内部类
 * 
 */
class OuterClass {
	private int i = 5;

	public void fun1() {
		InnerClass ic = new InnerClass();
		ic.fun1();
		System.out.println();
	}

	class InnerClass {
		public void fun1() {
			System.out.println(i);
		}
	}
}

public class TestInnerClass {
	public static void main(String[] args) {
		OuterClass oc = new OuterClass();
		OuterClass.InnerClass ocic = oc.new InnerClass();
		ocic.fun1();

		// 通过匿名外部类创建内部类对象
		new OuterClass().new InnerClass().fun1();
	}
}
package com.kilophone.classcontent;

/**
 * @author Administrator 静态内部类
 */
class OuterClass2 {

	static class InnerClass2 {
		int i = 5;
	}
}

public class TestStaticInnerClass {
	public static void main(String[] args) {
		OuterClass2.InnerClass2 oi = new OuterClass2.InnerClass2();
		System.out.println(oi.i);
	}
}

package com.kilophone.classcontent;

/**
 * @author Administrator 局部内部类
 * 
 */
class OuterClass3 {
	public void fun1() {
		class InnerClass3 {
			public void fun1() {
				System.out.println("InnerClass3-fun1");
			}
		}

		InnerClass3 ic3 = new InnerClass3();
		ic3.fun1();
	}
}

public class TestPartInnerClass {
	public static void main(String[] args) {
		OuterClass3 oc3 = new OuterClass3();
		oc3.fun1();
	}
}

package com.kilophone.classcontent;

/**
 * @author Administrator 匿名内部类 1、(一般情况下)匿名内部类是在抽象类和接口的基础上发展起来的,也可以是普通的类
 *         2、它可以是继承某个类,或是实现某个接口
 */
abstract class A {
	String name;
	int age;

	public A(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public abstract void test();
}

interface B {
	public void test();
}

class C {
	public void test() {
		System.out.println("Father C...");
	}
}

public class TestAnonymousInnerClass {
	public static void main(String[] args) {
		A a = new A("Jarry", 12) {
			@Override
			public void test() {
				System.out.println("抽象类实现的匿名内部类...");
			}
		};
		a.test();

		B b = new B() {
			@Override
			public void test() {
				System.out.println("接口实现的匿名内部类...");
			}
		};
		b.test();

		C c = new C() {
			public void test() {
				System.out.println("Child C...");
			}
		};
		c.test();
	}
}
// 抽象类实现的匿名内部类...
// 接口实现的匿名内部类...
// Child C...


你可能感兴趣的:(InnerClass、StaticInnerClass、PartInnerClass、AnonymousInnerClass)