简体   繁体   English

如何在不继承抽象类的情况下在 Kotlin 中创建抽象类的数组

[英]How to create an array of Abstract class in Kotlin Without inheriting the abstract class

I have 2 classes, class A which is an abstract class, and a class B like我有 2 个类,A 类是抽象类,B 类像

abstract class A(name: String){
    //some code
}

class B {
    //some code
}

Now if I want to add and array of type class A in Class B it is very simple in java like现在,如果我想在 B 类中添加 A 类类型的数组,它在 Java 中非常简单,例如

class B {
    A[][] arr = new A[2][2];
}

but I am trying the same in Kotlin like但我在 Kotlin 中尝试同样的方法

class B { 
    var arr = Array(2) {Array(2) {A}}
}

then it is giving error as "Classifier 'A' does not have a companion object and thus must be initialized here" but then I tried putting some variables in the companion object in the abstract class, and now it does not give any error.然后它给出错误,因为“分类器'A'没有伴随对象,因此必须在此处初始化”但后来我尝试在抽象类的伴随对象中放置一些变量,现在它没有给出任何错误。 but the array is made of type 'A.companion' and not of type 'A'.但数组是由“A.companion”类型而不是“A”类型组成的。

Kindly help me if you know anything about this.如果您对此有所了解,请帮助我。

Your Java code creates an array of 2 arrays, each of which can hold 2 objects of type A or null and initially contains null.您的 Java 代码创建了一个由 2 个数组组成的数组,每个数组可以包含 2 个类型为A或 null 的对象,并且最初包含 null。 (In Java, every reference value is nullable, so this is standard practice.) (在 Java 中,每个引用值都可以为空,所以这是标准做法。)

The Kotlin equivalent of that is: Kotlin 的等价物是:

Array(2){ Array(2){ null as A? }}

Like the Java code, each element there is nullable, and is initialised to null.与 Java 代码一样,那里的每个元素都是可空的,并被初始化为空。

(The error you're getting is because you're trying to give the Array constructor a type, but it's expecting a value — so it's trying to interpret A as a value, and guessing you mean the companion object of A . Don't worry if you don't know what that is!) (你得到的错误是因为你试图给 Array 构造函数一个类型,但它期待一个值——所以它试图将A解释为一个值,并猜测你的意思是Acompanion object 。不要如果您不知道那是什么,请担心!)

However, Kotlin supports non-nullable values too, which are often preferable.但是,Kotlin 也支持不可为空的值,这通常是更可取的。 So if you know what values you want the array to hold, it's more common to create and initialise it in one go, so that the type can be non-nullable.因此,如果您知道想要数组保存哪些值,更常见的是一次性创建和初始化它,这样类型就可以是不可为空的。 For example:例如:

arrayOf(arrayOf(A("1"), A("2")),
        arrayOf(A("3"), A("4")))

Notice that in Java, the array created by new A[2][2] contains only null elements.请注意,在 Java 中,由new A[2][2]创建的数组仅包含null元素。 There are no instances of A s in there.那里没有A的实例。 In Kotlin, this would be represented as a Array<Array<A?>> , rather than Array<Array<A>> , because the A s are null initially.在 Kotlin 中,这将表示为Array<Array<A?>> ,而不是Array<Array<A>> ,因为A最初为 null。

In terms of one dimensional arrays, you essentially have an array of 2 elements, and each of those is an array of 2 elements containing nulls only.就一维数组而言,您本质上有一个由 2 个元素组成的数组,每个元素都是一个仅包含空值的 2 个元素的数组。 Hence, you can write:因此,你可以写:

val aArray = Array<Array<A?>>(2) { arrayOfNulls(2) }

or a little bit shorter:或者更短一点:

val aArray = Array(2) { arrayOfNulls<A>(2) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM