【问题标题】:How to migrate Java child class to Kotlin when the parent class implements Parcelable父类实现Parcelable时如何将Java子类迁移到Kotlin
【发布时间】:2021-05-10 13:22:12
【问题描述】:

我决定从 Java 迁移到 Kotlin。我的 java 应用程序中有以下数据对象结构。

public class User implements Parcelable{
private String uuid;
private String name; 
// ..etc constructors getters setters and parcelable  implementation

}

public class Student extends User{

    private String grade; 
    // ..etc constructors getters setters and parcelable  implementation

    }

如何在 kotlin 中实现这一点? 谢谢

【问题讨论】:

  • Kotlin 教程有一个关于类的部分。 kotlinlang.org/docs/…
  • 我知道如何在 Kotlin 中创建类,我只想知道如何像我所做的那样进行继承
  • 您的问题可能与继承本身无关,而是与Parceleable 实现有关。请查看我的编辑,看看它是否能让您的问题变得更好。
  • @cutiko 感谢您编辑问题现在我认为每个人都更清楚了

标签: java android kotlin parcelable


【解决方案1】:

你可能想要这样:

@Parcelize
data class User(
 val uuid:String,
 val name:String):Parcelable

请自己研究如何进行继承。(从上面的代码sn-p应该很清楚)

【讨论】:

  • 当我写 @Parcelize 数据类 Student(var Grade:String):User() 它不会工作
  • 您必须将值传递给 User() 的构造函数,例如:@Parcelize data class Student(....):User(uuid="something",name="something") ,可打包
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
相关资源
最近更新 更多