怎么用单位电脑做网站服务器,php做用户登录网站,做一个网页需要多少钱,邢台生活网尽管每次需要时都可以手动实现一些常见类型的属性#xff0c;但将它们实现一次、添加到库中以便以后重用会更为方便
懒加载属性#xff1a;仅在首次访问时计算值。可观察属性#xff1a;监听器会收到有关此属性更改的通知。将属性存储在映射中而不是为每个属性单独创建字段…尽管每次需要时都可以手动实现一些常见类型的属性但将它们实现一次、添加到库中以便以后重用会更为方便
懒加载属性仅在首次访问时计算值。可观察属性监听器会收到有关此属性更改的通知。将属性存储在映射中而不是为每个属性单独创建字段。
为了涵盖这些以及其他情况kotlin支持委托属性delegated properties
语法如下val/var property name: Type by expression。在 by 之后的表达式是一个委托因为与属性对应的 get()和 set()将被委托给其 getValue() 和 setValue() 方法。属性委托不必实现一个接口但它们必须提供一个 getValue() 函数对于 var 还需要提供 setValue()
class Delegate {operator fun getValue(thisRef: Any?, property: KProperty*): String {return $thisRef, thank you for delegating ${property.name} to me!}operator fun setValue(thisRef: Any?, property: KProperty*, value: String) {println($value has been assigned to ${property.name} in $thisRef.)}
}class Example {var p: String by Delegate() // 委托
}fun main() {Example().p 王老吉 // 王老吉 has been assigned to p in com.example.Example6acdbdf5.println(Example().p) // com.example.Example7a1ebcd8, thank you for delegating p to me!
}当你从委托给Delegate的实例中p中读取属性时将调用Delegate的getValue()函数。第一个参数是你从中读取p的对象而第二个参数保存了p属性本身的描述
你可以在函数或代码块内声明一个委托属性它不必是类的成员
标准委托
kotlin为重用代理定义了模板方法
懒加载属性
lazy() 是一个接受 lambda并返回 LazyT 实例的函数可用作实现懒加载属性的委托。第一调用 get() 会执行lambda表达式并记住结果之后的调用直接返回结果
val lazyValue: String by lazy {println(computed!)Hello
}fun main() {println(lazyValue)println(lazyValue)
// computed!
// Hello
// Hello}默认情况下属性懒懒加载是同步的在一个线程中进行其他线程看到相同的结果。如果允许多个线程同时初始化使用LazyThreadSafetyMode.PUBLICATION参数。
val lazyValue: String by lazy (LazyThreadSafetyMode.PUBLICATION){println(computed!)Hello
}如果确信属性初始化和使用是在同一个线程中进行的可以使用LazyThreadSafetyMode.NONE参数但是不保证线程安全
可观察属性
Delegates.observable() 接受两个参数初始值和用于处理修改的处理程序。
每次你对属性进行赋值时在赋值完成后处理程序都会被调用。它有三个参数被赋值的属性、旧值和新值
class User {var name: String by Delegates.observable(no name) {prop, old, new - println($old - $new)}
}fun main() {val user User()user.name first // no name - firstuser.name second // first - second
}如果你想拦截赋值并否决它们使用 vetoable() 而不是 observable()。传递给 vetoable() 的处理程序将在分配新属性值之前被调用
class User {var name: String by Delegates.vetoable(no name) {prop, old, new - println($old - $new)true}
}fun main() {val user User()user.name first // no name - firstuser.name second // first - second
}委托给另一个属性
一个属性可以将其 getter 和 setter 委托给另一个属性。这样的委托对于顶层和类属性成员和扩展都是可用的
委托属性可
顶层属性同一类的成员或扩展属性另一个类的成员或扩展属性
var topLevelInt: Int 0
class ClassWithDelegate(val anotherClassInt: Int)class MyClass(var memberInt: Int, val anotherClassInstance: ClassWithDelegate) {var delegatedToMember: Int by this::memberIntvar delegatedToTopLevel: Int by ::topLevelIntval delegatedToAnotherClass: Int by anotherClassInstance::anotherClassInt
}
var MyClass.extDelegated: Int by ::topLevelInt这可能在某些情况下很有用例如当你想以向后兼容的方式重命名一个属性时引入一个新属性用 Deprecated 注解标记旧属性并委托其实现
class MyClass {var newName: Int 0Deprecated(Use newName instead, ReplaceWith(newName))var oldName: Int by this::newName
}
fun main() {val myClass MyClass()// 提示: oldName: Int is deprecated. Use newName insteadmyClass.oldName 42println(myClass.newName) // 42
}在Map中存储属性
一个常见的用例是将属性的值存储在Map中。这在解析JSON或执行其他动态任务的应用程序中经常发生。在这种情况下您可以使用Map实例本身作为委托属性的委托
class User(val map: MapString, Any?) {val name: String by mapval age: Int by map
}fun main() {// 构造方法接收一个Mapval user User(mapOf(name to John Doe,age to 25))
}可变的Map
class MutableUser(val map: MutableMapString, Any?) {var name: String by mapvar age: Int by map
}局部委托变量
可以将局部变量声明为委托属性。例如可以使局部变量成为延迟加载的
fun example(computeFoo: () - Foo) {val memoizedFoo by lazy(computeFoo)if (someCondition memoizedFoo.isValid()) {memoizedFoo.doSomething()}
}The memoizedFoo variable will be computed on first access only. If someCondition fails, the variable won’t be computed at all.
属性委托的要求
对于只读属性val委托应提供一个带有以下参数的getValue()
thisRef必须与属性所有者的类型相同或者是其超类型对于扩展属性它应该是被扩展的类型。property必须是KProperty*类型或其超类型
getValue()必须返回与属性相同的类型或其子类型
import kotlin.reflect.KPropertyclass Resourceclass Owner {val valResource: Resource by ResourceDelegate()
}class ResourceDelegate {operator fun getValue(thisRef: Owner, property: KProperty*): Resource {return Resource()}
}对于可变属性var委托还必须提供一个带有以下参数的setValue()
thisRef必须与属性所有者的类型相同或者是其超类型对于扩展属性它应该是被扩展的类型property必须是KProperty*类型或其超类型value必须是与属性相同的类型或其超类型
import kotlin.reflect.KPropertyclass Resourceclass Owner {var varResource: Resource by ResourceDelegate()
}class ResourceDelegate(private var resource: Resource Resource()) {operator fun getValue(thisRef: Owner, property: KProperty*): Resource {return resource}operator fun setValue(thisRef: Owner, property: KProperty*, value: Any?) {if (value is Resource) {resource value}}
}getValue()/setValue()函数可以作为委托类的成员函数或扩展函数提供当委托类没有这两个函数时可以定义扩展函数。这两个函数都需要用operator关键字标记。
可以通过使用kotlin标准库中的ReadOnlyProperty和ReadWriteProperty接口以匿名对象的方式创建委托而无需创建新的类。它们提供了所需的方法getValue()在ReadOnlyProperty中声明ReadWriteProperty继承了它并添加了setValue()。这意味着您可以在期望ReadOnlyProperty的任何地方传递一个ReadWriteProperty。
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KPropertyclass Resourcefun resourceDelegate(resource: Resource Resource()): ReadWritePropertyAny?, Resource object : ReadWritePropertyAny?, Resource {var curValue resourceoverride fun getValue(thisRef: Any?, property: KProperty*): Resource curValueoverride fun setValue(thisRef: Any?, property: KProperty*, value: Resource) {curValue value}}val readOnlyResource: Resource by resourceDelegate()
var readWriteResource: Resource by resourceDelegate()fun main() {readWriteResource Resource()println(readOnlyResource)
}委托属性转换规则
在底层kotlin编译器为某些类型的委托属性生成辅助属性然后将委托交给这些辅助属性 出于优化目的编译器在一些情况下不会生成辅助属性。通过委托到另一个属性的示例学习有关优化的信息。 例如对于属性prop它生成隐藏的属性prop$delegate而访问器的代码简单地委托给这个附加属性
class C {var prop: Type by MyDelegate()
}// 上边的编码被编译为一下内容
class C {private val prop$delegate MyDelegate()var prop: Typeget() prop$delegate.getValue(this, this::prop)set(value: Type) prop$delegate.setValue(this, this::prop, value)
}编译器在参数中提供了关于prop的所有必要信息第一个参数 this 指的是外部类 C 的一个实例而 this::prop 是一个 KProperty 类型的反射对象描述了 prop 本身
优化委托属性的情况
如果委托是以下情况之一将省略 $delegate 字段
引用属性
class CType {private var impl: Type ...var prop: Type by ::impl
}命名对象
object NamedObject {operator fun getValue(thisRef: Any?, property: KProperty*): String ...
}val s: String by NamedObject同一模块中使用backing field 和默认getter()的final val属性
val impl: ReadOnlyPropertyAny?, String ...class A {val s: String by impl
}常量表达式、枚举项、this、null。this示例
class A {operator fun getValue(thisRef: Any?, property: KProperty*) ...val s by this
}委托给另一个属性时的转换规则
当委托给另一个属性时编译器生成对所引用属性的直接访问。这意味着编译器不会生成字段 prop$delegate。这种优化有助于节省内存
class CType {private var impl: Type ...var prop: Type by ::impl
}prop 变量的属性访问器直接调用 impl 变量跳过了委托属性的 getValue 和 setValue 运算符因此不需要 KProperty 引用对象。
对于上述代码编译器生成了以下代码
class CType {private var impl: Type ...var prop: Typeget() implset(value) {impl value}fun getProp$delegate(): Type impl // This method is needed only for reflection
}提供委托
通过定义 provideDelegate运算符您可以扩展创建属性实现委托的对象的逻辑。如果在 by 右侧使用的对象定义了 provideDelegate 作为成员或扩展函数那么将调用该函数来创建属性委托实例
provideDelegate 的一种可能用例是在属性初始化时检查其一致性
例如要在绑定之前检查属性名称您可以编写如下代码
class ResourceDelegateT : ReadOnlyPropertyMyUI, T {override fun getValue(thisRef: MyUI, property: KProperty*): T { ... }
}class ResourceLoaderT(id: ResourceIDT) {operator fun provideDelegate(thisRef: MyUI,prop: KProperty*): ReadOnlyPropertyMyUI, T {checkProperty(thisRef, prop.name)// create delegatereturn ResourceDelegate()}private fun checkProperty(thisRef: MyUI, name: String) { ... }
}class MyUI {fun T bindResource(id: ResourceIDT): ResourceLoaderT { ... }val image by bindResource(ResourceID.image_id)val text by bindResource(ResourceID.text_id)
}provideDelegate 的参数与 getValue 相同
thisRef 必须与属性所有者的类型相同或者是其超类型对于扩展属性它应该是被扩展的类型property必须是 KProperty*类型或其超类型。
provideDelegate 方法在创建 MyUI 实例期间为每个属性调用并立即执行必要的验证。
如果没有拦截属性与其委托之间的绑定的能力要实现相同的功能您将不得不显式传递属性名称这不是很方便
// Checking the property name without provideDelegate functionality
class MyUI {val image by bindResource(ResourceID.image_id, image)val text by bindResource(ResourceID.text_id, text)
}fun T MyUI.bindResource(id: ResourceIDT,propertyName: String
): ReadOnlyPropertyMyUI, T {checkProperty(this, propertyName)// create delegate
}在生成的代码中provideDelegate 方法被调用来初始化辅助的 prop$delegate 属性。比较具有属性声明 val prop: Type by MyDelegate() 的生成代码与上面生成的代码当不存在 provideDelegate 方法时
class C {var prop: Type by MyDelegate()
}// this code is generated by the compiler
// when the provideDelegate function is available:
class C {// calling provideDelegate to create the additional delegate propertyprivate val prop$delegate MyDelegate().provideDelegate(this, this::prop)var prop: Typeget() prop$delegate.getValue(this, this::prop)set(value: Type) prop$delegate.setValue(this, this::prop, value)
}请注意provideDelegate 方法仅影响辅助属性的创建不影响为 getter 或 setter 生成的代码
使用标准库中的 PropertyDelegateProvider 接口可以创建委托提供程序而无需创建新的类
val provider PropertyDelegateProvider { thisRef: Any?, property -ReadOnlyPropertyAny?, Int {_, property - 42 }
}
val delegate: Int by provider