我不知道如何做到这一点,以便在创建对象时,参数的值“通过设置器”,我得到的最接近的是复制代码,在创建对象时使用一次,然后在二传手
class User(var name: String, password: String, age: Int) {
// IDK how use a custom setter in the primary constructor
var password: String = if (password.length > 6) password else throw IllegalArgumentException("Password is too short")
set(value) {
if(value.length > 6)
field = value
else
throw IllegalArgumentException("Password is too short")
}
var age: Int = if (age > 18) age else throw IllegalArgumentException("Age must be 18+")
set(value) {
if(value > 18 )
field = value
else
throw IllegalArgumentException("Age must be …Run Code Online (Sandbox Code Playgroud) 我正在尝试在标题下方打印一行。这个想法是该行与标题的长度相同。我尝试了多种方法,我认为这是最接近的,但它给我的结果不正确。
fun main() {
val chapters = arrayOf("Basic syntax", "Idioms", "Kotlin by example", "Coding conventions")
for (numCharpter in chapters.indices){
// Print the name of the chapter
val title = "Charpter ${numCharpter + 1}: ${chapters[numCharpter]}"
val arrayDash = Array(title.length) {'='}
val stringDash = arrayDash.toString()
println("$title\n$stringDash\n")
// the rest of the code
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的输出是:
Charpter 1: Basic syntax
========================
Charpter 2: Idioms
==================
Charpter 3: Kotlin by example
=============================
Charpter 4: Coding conventions
==============================
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
Charpter 1: Basic syntax
[Ljava.lang.Character;@24d46ca6
Charpter …Run Code Online (Sandbox Code Playgroud)