草庐IT

Getter-Setter

全部标签

constructor - 在 kotlin 中,如何将主构造函数中的属性 setter 设为私有(private)?

在kotlin中,如何将主构造函数中的属性setter设为私有(private)?classCity(valid:String,varname:String,vardescription:String=""){funupdate(name:String,description:String?=""){this.name=namethis.description=description?:this.description}}我希望属性name的setter是私有(private)的,而它的getter是公开的,我该怎么做? 最佳答案

c++ - getter 函数是否需要互斥体?

我有一个从多个线程访问的类。它的getter和setter函数都由锁保护。真的需要getter函数的锁吗?如果是,为什么?classfoo{public:voidsetCount(intcount){boost::lock_guardlg(mutex_);count_=count;}intcount(){boost::lock_guardlg(mutex_);//mutexneeded?returncount_;}private:boost::mutexmutex_;intcount_;}; 最佳答案 唯一可以绕过锁的方法是让自己相

java - Kotlin:属性 setter 的文档

我正在编写一个Kotlin库。在其中一门课中,我有以下内容:classSessionWrapper{/***Thetimeinmillisecondsafterwhichthesessionwillexpire.*/varexpiryTime=DEFAULT_EXPIRY_TIMEget(){mainThreadCheck()returnfield}set(value){mainThreadCheck()field=valueupdateExpiry(value)然而,updateExpiry(long)的行为应该对SessionWrapper的客户端是透明的,如果他们修改expiry

java - Kotlin:属性 setter 的文档

我正在编写一个Kotlin库。在其中一门课中,我有以下内容:classSessionWrapper{/***Thetimeinmillisecondsafterwhichthesessionwillexpire.*/varexpiryTime=DEFAULT_EXPIRY_TIMEget(){mainThreadCheck()returnfield}set(value){mainThreadCheck()field=valueupdateExpiry(value)然而,updateExpiry(long)的行为应该对SessionWrapper的客户端是透明的,如果他们修改expiry

c# - 等待来自 setter 属性的异步函数

我需要等待来自属性setter方法的异步函数。publicStringtestFunc(){get{}set{//AwaitCalltotheasyncfunc}}我知道我们不应该创建异步属性,那么什么是实现此操作的最佳方法。 最佳答案 您不能制作异步属性,您也不应该想要-属性意味着快速、非阻塞操作。如果您需要执行长时间运行的事件,正如您想要启动异步操作并等待它所暗示的那样,根本不要将其设为属性。移除setter并创建一个方法。 关于c#-等待来自setter属性的异步函数,我们在Sta

Kotlin 只读属性,带和不带 getter

这些是等价的吗?valfoo=someFooReturningFunction()valfooget()=someFooReturningFunction()按照我理解文档的方式,它们是,但在我自己的测试中它们不是。使用get()someFooReturningFunction()会在每次访问属性时进行评估,而不是仅评估一次。 最佳答案 它们不等价。自定义getter确实会在每个属性访问时评估,类似于普通函数,而没有自定义访问器的val属性仅在初始化时评估一次(实际上存储在finalJVM平台上的字段)。这里至少还有一些不同之处:控

Kotlin 只读属性,带和不带 getter

这些是等价的吗?valfoo=someFooReturningFunction()valfooget()=someFooReturningFunction()按照我理解文档的方式,它们是,但在我自己的测试中它们不是。使用get()someFooReturningFunction()会在每次访问属性时进行评估,而不是仅评估一次。 最佳答案 它们不等价。自定义getter确实会在每个属性访问时评估,类似于普通函数,而没有自定义访问器的val属性仅在初始化时评估一次(实际上存储在finalJVM平台上的字段)。这里至少还有一些不同之处:控

properties - 在 Kotlin 中创建接口(interface)时,属性是否具有 get/set 是否重要?

在Kotlin接口(interface)中,是否使用空的get/set语句声明属性是否重要?比如……interfaceExampleInterface{//These...vala:Stringgetvarb:Stringgetset//...comparedtothese...valc:Stringvard:String}我很难注意到差异。在实现接口(interface)时,我是否对属性使用getter/setter或直接设置值似乎并不重要。当通过java访问这些时,val都有getter,var都有getter和setter。publicvoidjavaMethod(Example

properties - 在 Kotlin 中创建接口(interface)时,属性是否具有 get/set 是否重要?

在Kotlin接口(interface)中,是否使用空的get/set语句声明属性是否重要?比如……interfaceExampleInterface{//These...vala:Stringgetvarb:Stringgetset//...comparedtothese...valc:Stringvard:String}我很难注意到差异。在实现接口(interface)时,我是否对属性使用getter/setter或直接设置值似乎并不重要。当通过java访问这些时,val都有getter,var都有getter和setter。publicvoidjavaMethod(Example

kotlin - 当我们有属性 setter 时, `Delegates.observable` 的用例是什么?

当我们只能使用属性setter时,Delegates.observable的用例是什么?varfoobyDelegates.observable("hell0"){prop,old,new->//reacttochangesinfoo}varbar="hello"set(value){field=value//reacttochangesinbar//wecanalsodovalidation,setsomethinglike`value*2`tofield,etc.} 最佳答案 如果您希望多个属性以相同的方式对修改使用react,