kotlin中继承父属性使用构造方法
1. 定义父类,给它两个属性:
abstract class AbstractResponseMessage {
private var success = true
private var reason: String? = null
constructor() {}
constructor(success: Boolean, reason: String?) {
this.success = success
this.reason = reason
}
}
2. 子类继承该父类,其中 success 和reason 为父类属性:
class ChatResponseMessage : AbstractResponseMessage {
var from: String = ""
var content: String = ""
constructor(from: String, content: String)
constructor(success: Boolean, reason: String, from: String, content: String) : super(success, reason){
this.from = from
this.content = content
}
}
|