返回顶部

收藏

java inner class示例

更多

如下示例代码,演示如何使用java的内置类。

package cn.outofmemory.snippets.core;

public class InnerClassRef {

    public static void main(String[] args) {
        InnerClassRef inner1 = new InnerClassRef();
        inner1.boat("Athens");
        InnerClassRef inner2 = new InnerClassRef();

        // Defining references to inner classes:
        InnerClassRef.C c = inner2.cont();
        InnerClassRef.D d = inner2.to("Thessaloniki");
    }

    class C {

        private int i = 11;

        public int value() {
            return i;
        }
    }

    class D {

        private String str;

        D(String whereTo) {
            str = whereTo;
        }

        String readLabel() {
            return str;
        }
    }

    public D to(String s) {
        return new D(s);
    }

    public C cont() {
        return new C();
    }

    public void boat(String dest) {
        C c = cont();
        D d = to(dest);
        System.out.println(d.readLabel());
    }
}

输出:

Athens

标签:java,OOD,内嵌类

收藏

0人收藏

支持

0

反对

0

发表评论