开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜