ale*_*sov 59 getter-setter kotlin
例如,在Java中,我可以自己编写getter(由IDE生成)或者在lombok中使用@Getter之类的注释 - 这非常简单.
然而,Kotlin 默认拥有getter和setter.但我无法理解如何使用它们.
我想说,就像Java一样:
private val isEmpty: String
get() = this.toString() //making this thing public rises an error: Getter visibility must be the same as property visibility.
Run Code Online (Sandbox Code Playgroud)
那么吸气剂如何工作?
Cor*_*ave 103
在Kotlin中自动生成getter和setter.如果你写:
val isEmpty: Boolean
Run Code Online (Sandbox Code Playgroud)
它等于以下Java代码:
private final Boolean isEmpty;
public Boolean isEmpty() {
return isEmpty;
}
Run Code Online (Sandbox Code Playgroud)
在您的情况下,私有访问修饰符是冗余的 - 默认情况下isEmpty是私有的,只能由getter访问.当您尝试获取对象的isEmpty属性时,可以实际调用get方法.为了更好地理解Kotlin中的getter/setter:下面的两个代码示例是相同的:
var someProperty: String = "defaultValue"
Run Code Online (Sandbox Code Playgroud)
和
var someProperty: String = "defaultValue"
get() = field
set(value) { field = value }
Run Code Online (Sandbox Code Playgroud)
另外我想指出this一个getter不是你的属性 - 它是类实例.如果要在getter或setter中访问字段的值,可以使用保留字field:
val isEmpty: Boolean
get() = field
Run Code Online (Sandbox Code Playgroud)
如果您只想在公共访问中使用get方法 - 您可以编写以下代码:
var isEmpty: Boolean
private set
Run Code Online (Sandbox Code Playgroud)
由于set访问器附近的私有修饰符,您只能在对象内的方法中设置此值.
hot*_*key 27
有关属性访问器可见性修饰符的规则如下:
吸气剂知名度var和val属性应该是完全一样的财产的知名度,所以你只能明确地复制性能改进剂,但它是多余的:
protected val x: Int
protected get() = 0 // No need in `protected` here.
Run Code Online (Sandbox Code Playgroud)var属性的Setter可见性应 与属性可见性相同或更低:
protected var x: Int
get() = 0
private set(x: Int) { } // Only `private` and `protected` are allowed.
Run Code Online (Sandbox Code Playgroud)在科特林,属性总是通过getter和setter访问,从而有在作出财产无需private与public存取像Java中- 它支持字段(如果存在的话)已经是私有的.因此,属性访问器上的可见性修饰符仅用于使setter可见性不那么宽松:
对于具有支持字段和默认访问者的属性:
var x = 0 // `public` by default
private set
Run Code Online (Sandbox Code Playgroud)对于没有后备字段的属性:
var x: Int // `public` by default
get() = 0
protected set(value: Int) { }
Run Code Online (Sandbox Code Playgroud)如果您有 Var,那么您可以:
var property: String = "defVal"
get() = field
set(value) { field = value }
Run Code Online (Sandbox Code Playgroud)
但对于 Val 来说,一旦分配就无法设置它,因此不会有 setter 块:
val property: String = "defVal"
get() = field
Run Code Online (Sandbox Code Playgroud)
或者如果你不想要设置者那么:
val property: String = "defVal"
private set
Run Code Online (Sandbox Code Playgroud)
默认情况下,kotlin中的Getter是public,但是您可以将setter设置为private,并使用类内的一个方法设置值。像这样。
/**
* Created by leo on 17/06/17.*/
package foo
class Person() {
var name: String = "defaultValue"
private set
fun foo(bar: String) {
name = bar // name can be set here
}
}
fun main(args: Array<String>) {
var p = Person()
println("Name of the person is ${p.name}")
p.foo("Jhon Doe")
println("Name of the person is ${p.name}")
}
Run Code Online (Sandbox Code Playgroud)
您可以查看本教程以获取更多信息:
特性
在 Kotlin 世界中,类不能有字段,只有属性。与 val 相比,var 关键字告诉我们该属性是可变的。让我们看一个例子:
Run Code Online (Sandbox Code Playgroud)class Contact(var number: String) { var firstName: String? = null var lastName: String? = null private val hasPrefix : Boolean get() = number.startsWith("+") }代码不多,但幕后发生了很多事情。我们将一步一步地完成它。首先,我们创建了一个公共 final 类 Contact。
这是我们必须面对的主要规则:如果没有另外指定,默认情况下类是 public 和 final 的(顺便说一下,类方法也是如此)。如果你想从类继承,用 open 关键字标记它。
1)实施例的默认setter和getter用于属性 firstName在科特林
class Person {
var firstName: String = ""
get() = field // field here ~ `this.firstName` in Java or normally `_firstName` is C#
set(value) {
field = value
}
}
Run Code Online (Sandbox Code Playgroud)
使用
val p = Person()
p.firstName = "A" // access setter
println(p.firstName) // access getter (output:A)
Run Code Online (Sandbox Code Playgroud)
如果上面的setter或与您getter的完全相同,则可以将其删除,因为不必要
2)示例自定义设置器和获取器。
const val PREFIX = "[ABC]"
class Person {
// set: if value set to first name have length < 1 => throw error else add prefix "ABC" to the name
// get: if name is not empty -> trim for remove whitespace and add '.' else return default name
var lastName: String = ""
get() {
if (!field.isEmpty()) {
return field.trim() + "."
}
return field
}
set(value) {
if (value.length > 1) {
field = PREFIX + value
} else {
throw IllegalArgumentException("Last name too short")
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用
val p = Person()
p.lastName = "DE " // input with many white space
println(p.lastName) // output:[ABC]DE.
p.lastName = "D" // IllegalArgumentException since name length < 1
Run Code Online (Sandbox Code Playgroud)
更多内容
我开始从Java学习Kotlin,因此感到困惑field,property因为在Java中没有property。
某些搜索后,我看到了field和property在科特林样子C#(字段和属性之间的区别是什么?)
下面是一些相关的帖子里面谈到field与propertyJava和科特林。
java是否具有类似于C#属性的东西?
https://blog.kotlin-academy.com/kotlin-programmer-dictionary-field-vs-property-30ab7ef70531
如果我错了,请纠正我。希望对你有帮助
| 归档时间: |
|
| 查看次数: |
53033 次 |
| 最近记录: |