Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java类是否有默认值?_Java_Class_Properties_Default - Fatal编程技术网

Java类是否有默认值?

Java类是否有默认值?,java,class,properties,default,Java,Class,Properties,Default,可以将默认属性设置为Java类吗 这曾经是VB中的一个小技巧,所以我的代码看起来像这样 Team.Score; 而不是 Team.Score.getScore(); 它很难阻止我的表演,但由于这是我的第一个Java应用程序,我不得不怀疑。没有办法隐式调用getter或setter。一般来说,Java避免任何隐式行为(直到Java8),因此您必须说出您希望代码执行的操作。这样做的好处是,您可以更清楚地看到代码执行的细节。缺点是你不能隐藏代码正在做什么的细节,所以作者可以很容易地强调代码应该做什

可以将默认属性设置为Java类吗

这曾经是VB中的一个小技巧,所以我的代码看起来像这样

Team.Score;
而不是

Team.Score.getScore();

它很难阻止我的表演,但由于这是我的第一个Java应用程序,我不得不怀疑。

没有办法隐式调用getter或setter。一般来说,Java避免任何隐式行为(直到Java8),因此您必须说出您希望代码执行的操作。这样做的好处是,您可以更清楚地看到代码执行的细节。缺点是你不能隐藏代码正在做什么的细节,所以作者可以很容易地强调代码应该做什么

你的选择是

  MyObject(int value);        // only set it in the constructor
  myObject.m_value = x;       // not ideal again
  myObject.value(y);          // short setter style
  myObject.setValue(z);       // JavaBean setter
获得价值

  int x = myObject.my_value;  // get a field, not ideal as it break encapsulation
  int y = myObject.value();   // short getter style
  int z = myObject.getValue();// JavaBean getter style
对于setter,您的选项是

  MyObject(int value);        // only set it in the constructor
  myObject.m_value = x;       // not ideal again
  myObject.value(y);          // short setter style
  myObject.setValue(z);       // JavaBean setter

这在Java中是不可能的。Java中没有默认属性的概念。这将导致代码更短,但代码更不清晰。谢谢大家。我也这么想,但值得一试。