开发者

What's the point of automatic properties that define both get and set in apex?

开发者 https://www.devze.com 2023-03-09 07:43 出处:网络
It makes sense to me why you might want to have a property like public string var {get;} to make it read-only, or

It makes sense to me why you might want to have a property like

public string var {get;}

to make it read-only, or

public string var {set;}

to make var write-only (although I'm not 开发者_StackOverflow中文版sure where that would be useful). But what does

public string var {get; set;}

get you that making var a bog-standard variable doesn't?


Nothing really. I prefer the explicit:

public String myVar {get; private set;}

to simply omitting the set. It's really just a shorthand convention of the classic:

public String myVar
{
   get
   {
      return myVar;
   }
   set(String s)
   {
      myVar = s;
   }
}

When in Rome...


It makes it visible to the VisualForce page.

public String var;

Won't let you get to the var variable from Visualforce.

I.e.

<apex:outputText value="{!var}">

will fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号