Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 派生类中未初始化超类属性_Inheritance_Kotlin - Fatal编程技术网

Inheritance 派生类中未初始化超类属性

Inheritance 派生类中未初始化超类属性,inheritance,kotlin,Inheritance,Kotlin,我有以下Kotlin代码: import java.util.* import kotlin.collections.HashSet open class Graph(open val n: Int) { val graph = List<MutableSet<Int>>(n) {HashSet<Int>()} open fun addEdge(u: Int, v: Int) { graph[u].add(v)

我有以下Kotlin代码:

import java.util.*
import kotlin.collections.HashSet

open class Graph(open val n: Int) {
    val graph = List<MutableSet<Int>>(n) {HashSet<Int>()}

    open fun addEdge(u: Int, v: Int) {
        graph[u].add(v)
        graph[v].add(u)
    }

    val numEdges: Int
        get() {
            return graph.asSequence()
                    .map { it.size }
                    .reduce { x, y -> x + y }
        }   

    fun edgeSet() : HashSet<Pair<Int,Int>> {
        val result = HashSet<Pair<Int,Int>>()
        for (i in graph.indices) {
            for(j in graph[i]) {
                if(i<j) result.add(i to j)
                else result.add(j to i)
            }
        }
        return result;
    }

    override fun toString(): String {
        return "Graph(n=$n, graph=$graph)"
    }
}

class DGraph(override val n: Int) : Graph(n) {
    override fun addEdge(u: Int, v: Int) {
        graph[u].add(v)
    }
}

我发现在DGraph实例中graph属性没有初始化,我得到和IndexOutOfBoundsException。为什么会发生这种情况;DR从
DGraph
构造函数参数中删除
覆盖val

您的代码秘密地等同于以下Java伪代码:

class Graph {
    private final int n;
    private final List<MutableSet<Int>> graph;

    Graph(int n) {
        this.n = n;
        this.graph = someStuffThatCreatesAList(getN());
    }

    int getN() { return n; }

    // ...
}

class DGraph extends Graph {
    private final int n;

    DGraph(int n) {
        super(n);
        this.n = n;
    }

    int getN() { return n; }

    // ...
}
类图{
私人终审法院;
私有最终列表图;
图(int n){
这个,n=n;
this.graph=someStufghtCreateSalist(getN());
}
int getN(){return n;}
// ...
}
类DGraph扩展了图{
私人终审法院;
DGraph(int n){
超级(n);
这个,n=n;
}
int getN(){return n;}
// ...
}
因此,在
构造函数期间,将调用重写的
getN
。但是,子类成员尚未初始化,因此它返回默认值(
0


通过删除
override val
,可以消除对
getN

的覆盖。如果
graph
属性未初始化,则
graph.graph.size
将失败。@OliverCharlesworth,是的,我知道。但是为什么图形属性没有初始化。谢谢。现在可以了。我确实尝试过删除覆盖,但却得到了一个错误。我从未想过删除val,因为我也需要n作为DGraph中的一个属性。现在我更明白了。
class Graph {
    private final int n;
    private final List<MutableSet<Int>> graph;

    Graph(int n) {
        this.n = n;
        this.graph = someStuffThatCreatesAList(getN());
    }

    int getN() { return n; }

    // ...
}

class DGraph extends Graph {
    private final int n;

    DGraph(int n) {
        super(n);
        this.n = n;
    }

    int getN() { return n; }

    // ...
}