小编Jax*_*xon的帖子

kotlin 中类的私有可见性修饰符的范围

当我尝试在同一项目的practice1.kt 和practice2.kt 文件中创建私有Person 类时,出现重新声明错误。例如:

练习1.kt

private class Person constructor(var name: String, var age: Int){
var profession: String = "Not mentioned"

init{
    println("$name's details are being held in this class object.")
}

constructor(name:String,age:Int, profession: String): this(name,age){
    this.profession = profession
}

fun printPersonDetails(){
    println("$name whose profession is $profession, is $age years old.")

}}

fun main(args: Array<String>){
val smith = Person("Smith",20)
smith.printPersonDetails()}
Run Code Online (Sandbox Code Playgroud)

练习2.kt

private class Person(val name:String, age:Int){

var age: Int = age
set(new_data){
    println("Setting age to $new_data")
    field  = new_data
}}
Run Code Online (Sandbox Code Playgroud)

我在 …

kotlin

3
推荐指数
1
解决办法
587
查看次数

标签 统计

kotlin ×1