有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中“包含类的子类”的含义是什么?

我在阅读a paper时遇到了表达式“包含类的子类”。这在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.


共 (2) 个答案

  1. # 1 楼答案

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

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

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

    在上面的示例中,Inner继承自Outer,它充当其包含类

  2. # 2 楼答案

    这个例子有一个包含类的子类:

    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".
        }
    }
    

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