我们目前正在对 OCA 证书进行相同的测试,今天我们遇到了一个小问题。 我会尽量保持简短:
Which are methods using JavaBeans naming conventions for accessors and mutators?
(Choose all that apply)
A. public boolean getCanSwim() { return canSwim;}
B. public boolean canSwim() { return numberWings;}
C. public int getNumWings() { return numberWings;}
D. public int numWings() { return numberWings;}
E. public void setCanSwim(boolean b) { canSwim = b;}
答案(由 OCA SE 8 指定):C 和 E
我们讨论的是C点:
public int getNumWings() { return numberWings;}
我同事的观点是,由于下面提到的规则,这是错误的。方法访问器一定是 getNumberWings 所以点 C 是错误的。我附上了 OCA 的规则表,它认为规则 5 是错误的。 PHOTO of Rules for JavaBeans naming convention on OCA SE 8 page 206
据我所知,方法的名称不必遵守属性。 您对此有何看法?
OCA Oracle Certified Associate Java SE 8 Programmer I 学习指南考试 1Z0-808
我也试图在以下网站上找到合适的答案:
来自 Oracle 的 JavaBeans 约定:http://download.oracle.com/otn-pub/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/beans.101.pdf?AuthParam=1484818426_7e07f5a35c14ebfbadb2c68798198d7e
JavaBeans 约定(简而言之 Java)
最佳答案
你的同事(我认为)认为
public int getNumWings() { return numberWings;}
违反了JavaBean,因为字段名和属性名不同。
规范不支持。 Java bean 约定(编码为 here )状态:
6.2.2. Properties
A bean defines a property
p
of typeT
if it has accessor methods that follow these patterns (ifT
is boolean, a special form of getter method is allowed):Getter
public T getP()
Boolean getter
public boolean isP()
Setter
public void setP(T)
Exceptions
Property accessor methods can throw any type of checked or unchecked exceptions
请注意,它没有说明保存属性值的(通常)private
字段的名称。事实上,该字段甚至可能不存在......如果可以用其他方式表示属性值。
关于java - JavaBeans 对 OCA 方法命名约定的误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738871/