为什么我无法访问此 kotlin 代码中抽象 class 的某些成员?
Why can't I access some members of an abstract class in this kotlin code?
在下面的代码中,我可以访问抽象 class 的所有重写成员,但不能访问它自己的成员。
interface Collection<T: Any> {
val count: Int
val isEmpty: Boolean
get() = count == 0
}
interface Heap<T: Any> : Collection<T> {
fun peek(): T?
}
abstract class AbstractHeap<T: Any>() : Heap<T> {
var elements: ArrayList<T> = ArrayList<T>()
override val count: Int
get() = elements.size
override fun peek(): T? = elements.firstOrNull()
...
}
class ComparableHeapImpl<T : Comparable<T>> :
AbstractHeap<T>() {
companion object {
fun <T : Comparable<T>> create(
elements: ArrayList<T>
): Heap<T> {
val heap = ComparableHeapImpl<T>()
heap.heapify(elements)
return heap
}
}
}
我无法访问 elements
,如下所示
fun main() {
val array = arrayListOf(1, 12, 3, 4, 1, 6, 8, 7)
val test = ComparableHeapImpl.create(array)
val a = test.elements[0] // This won't work.
}
但是我在这段代码中写道:
abstract class Test{
val i = 1
fun test(){ println("nothing")}
}
class Test1: Test(){
companion object {
val a = Test1()
fun create(): Test1 = a
}
}
fun main() {
val test = Test1.create()
test.test() // This works fine.
println(test.i) // And this works fine.
}
一切正常。我无法理解这一点。哦,我正在使用 android studio arctic firefox 补丁 2。
您的 test
变量在这里:
val test = ComparableHeapImpl.create(array)
不是 AbstractHeap<Int>
类型,它是 Heap<Int>
类型(根据 ComparableHeapImpl.create
的定义)。这就是您无法访问 elements
的原因 - 它不是 Heap
界面的一部分。
如果您想访问 elements
变量,您有几种选择:
- 使其成为
Heap
界面的一部分
- make
create
return AbstractHeap
(或ComparableHeapImpl
,对于这个工厂函数来说更符合逻辑)
- 将
test
转换为AbstractHeap
或ComparableHeapImpl
为什么要从外部访问此 属性? IMO,您应该提供所有对 Heap
接口中堆的使用有意义的 public methods/properties,但是这个最有可能应该是 private
(或者也许protected
).
在下面的代码中,我可以访问抽象 class 的所有重写成员,但不能访问它自己的成员。
interface Collection<T: Any> {
val count: Int
val isEmpty: Boolean
get() = count == 0
}
interface Heap<T: Any> : Collection<T> {
fun peek(): T?
}
abstract class AbstractHeap<T: Any>() : Heap<T> {
var elements: ArrayList<T> = ArrayList<T>()
override val count: Int
get() = elements.size
override fun peek(): T? = elements.firstOrNull()
...
}
class ComparableHeapImpl<T : Comparable<T>> :
AbstractHeap<T>() {
companion object {
fun <T : Comparable<T>> create(
elements: ArrayList<T>
): Heap<T> {
val heap = ComparableHeapImpl<T>()
heap.heapify(elements)
return heap
}
}
}
我无法访问 elements
,如下所示
fun main() {
val array = arrayListOf(1, 12, 3, 4, 1, 6, 8, 7)
val test = ComparableHeapImpl.create(array)
val a = test.elements[0] // This won't work.
}
但是我在这段代码中写道:
abstract class Test{
val i = 1
fun test(){ println("nothing")}
}
class Test1: Test(){
companion object {
val a = Test1()
fun create(): Test1 = a
}
}
fun main() {
val test = Test1.create()
test.test() // This works fine.
println(test.i) // And this works fine.
}
一切正常。我无法理解这一点。哦,我正在使用 android studio arctic firefox 补丁 2。
您的 test
变量在这里:
val test = ComparableHeapImpl.create(array)
不是 AbstractHeap<Int>
类型,它是 Heap<Int>
类型(根据 ComparableHeapImpl.create
的定义)。这就是您无法访问 elements
的原因 - 它不是 Heap
界面的一部分。
如果您想访问 elements
变量,您有几种选择:
- 使其成为
Heap
界面的一部分 - make
create
returnAbstractHeap
(或ComparableHeapImpl
,对于这个工厂函数来说更符合逻辑) - 将
test
转换为AbstractHeap
或ComparableHeapImpl
为什么要从外部访问此 属性? IMO,您应该提供所有对 Heap
接口中堆的使用有意义的 public methods/properties,但是这个最有可能应该是 private
(或者也许protected
).