2、方法內部類
3、匿名類
4、靜態內部類
5、接口內部類
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());
}
}
調用Inner構造方法:inner
>>>>>Inner class!
Outer class!
11
-------1--------
調用Inner構造方法:inner
>>>>>Inner class!
Outer class!
1001
-------2--------
12
Process finished with exit code 0
void say();
}
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 bar!
Process finished with exit code 0
一種嵌入了內部類聲明代碼外部類,稱爲直接外部類。
另一種是與內部類沒有任何關係的外部類,稱爲外部類。
在外部類中,要看到一個類的內部類成員,則至少要求這個內部類的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();
}
}
* 內部類實現接口
*
* @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;
}
}
say bar!
Process finished with exit code 0
* 匿名類.
*
* @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();
}
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);
}
}
Process finished with exit code 0
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 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);
}
}
501
Hello 2222
502
Process finished with exit code 0
* 接口內部類
*
* @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();
}
}
* 嵌套內部類
*
* @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();
}
}
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();
}
}
Inner2 doing ...
HeHe!
Process finished with exit code 0