Kotlin中的可包裹派生类 [英] Parcelable Derived Class in Kotlin

查看:167
本文介绍了Kotlin中的可包裹派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https://github.com上的Android Parcelable插件/nekocode/android-parcelable-intellij-plugin-kotlin

我在具有此定义的类上尝试过

I tried it on a class with this definition

class ChessClock : TextView  {
    lateinit var player:String
    constructor(context: Context) : super(context)
    constructor(context:Context, p:String, angle:Float) : this(context) {
       player = p
       rotation = angle
   }
   <snip>
}

,定义更改为

class ChessClock() : TextView, Parcelable {
   lateinit var player:String 
   constructor(context: Context) : super(context)
   constructor(context:Context, p:String, angle:Float) : this(context){
       player = p
       rotation = angle
   }
   <snip -- various stuff added here>
}

突出显示了两个语法错误.

Two syntax errors were highlighted.

在行

class ChessClock() : TextView, Parcelable

TextView带下划线,并带有注释此类型具有构造函数,必须在此处初始化".

TextView is underlined, with the comment "This type has a constructor, and must be initialized here."

在行

constructor(context: Context) : super(context)

带下划线的是

super,并带有注释期望进行主要构造函数调用".

super is underlined, with the comment "Primary constructor call expected."

我只用了Kotlin几个星期,但我不明白这里发生了什么.首先,我知道(或者至少我想我知道)kotlin没有实现多重继承,所以我不理解什么.

I've only been using kotlin for a few weeks, and I don't understand what's going on here. First of all, I know (or at least I think I know) that kotlin doesn't implement multiple inheritance, so I don't understand what

ChessClock()类:TextView,可包裹

class ChessClock() : TextView, Parcelable

表示.这真的是合法的科特林吗?如何在Kotlin中使派生类为Parcelable?

means. Is this really legitimate kotlin? How can one make a derived class Parcelable in kotlin?

推荐答案

  1. TextView是一个类,因此您的主要构造函数应 调用其构造函数之一
  2. 您应该从其他构造函数调用您的主要构造函数
  1. TextView is a class and therefore your primary constructor should invoke one of its constructors
  2. you should invoke your primary constructor from other constructors

示例:

class ChessClock(context: Context) : TextView(context), Parcelable

//in this case you don't need other constructors but in-case you do, this is how you should write it:
constructor(context: Context, dummy: Int): this(context)

这篇关于Kotlin中的可包裹派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆