'subclasses of the containing class'在Java中的含义?



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

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.

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

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

ParentSubclass包含Child子类。请注意,在Parent(或其子类)之外new 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".
}
}

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

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

下面是一个从嵌套在其中的类继承的类的示例:

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

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

最新更新