-1

当我阅读一篇论文时,我遇到了“包含类的子类”这个表达。containing class这在 Java中是什么意思?这是论文的摘录。

Primarily, this entailed three things: (i) studying the implementation of the entity, as well as its usage, to reason about the intent behind the functionality; (ii) performing static dependency analysis on the entity, and any other types, methods, or fields referenced by it, including constants; and (iii) examining the inheritance hierarchy and subclasses of the containing class. This approach took considerable time and effort to apply.

4

2 回答 2

3

此示例具有包含类的子类:

class Parent {
    class Child {
    }
}

class ParentSubclass extends Parent {
    void whatever() {
        new Child(); // Creates an instance of Parent.Child
    }
}

ParentSubclass是的包含类的类。请注意,外部(或其子类)将不起作用,因为您需要有一个包含(“外部”)类来实例化非“内部”类。ChildParentnew Child()static

当您现在向 中添加一个方法doSomething、在其中Parent调用它Child但在ParentSubclass.

class Parent {
    void doSomething() {
        System.out.println("Not doing anything");
    }

    class Child {
        void whatever() {
            doSomething(); // actually: Parent.this.doSomething()
        }
    }
}

class ParentSubclass extends Parent {
    void doSomething() {
        System.out.println("I'm just slacking.");
    }

    void whatever() {
        Child a = new Child(); // Creates an instance of Parent.Child
        a.whatever(); // will print "I'm just slacking".
    }
}

像这样的情况使静态代码分析成为一个相当困难的问题。

于 2012-12-20T18:10:42.350 回答
2

由于我无法访问该论文,因此这是我最好的猜测:在 Java 中,类之间可以通过多种方式相互关联:除了相互继承之外,类还可以嵌套在另一个内部。

下面是一个继承自它所嵌套的类的类的示例:

public class Outer {
    public void doSomething() {
        // ...does something
    }
    private static class Inner extends Outer {
        public void doSomething() {
            // ...does something else
        }
    }
}

在上面的示例中,Inner继承自Outer用作其包含类的 。

于 2012-12-20T18:12:53.557 回答