简体   繁体   English

为什么Java持久性模型中的set和get方法应该具有相同的名称?

[英]why does set and get method in java persistence model should have the same name?

I wonder why methods of get and set in java persistence model should have the same name after the word get and set. 我想知道为什么Java持久性模型中的get和set方法在单词get and set之后应该具有相同的名称。 As like this: 像这样:

getCompanyName(){
}

setCompanyName(){
}

And I think this is not just in model, also in other configuration of get and set. 而且我认为这不仅在模型中,而且在get和set的其他配置中。 Can anyone explain why? 谁能解释为什么? I really want to know.. Thanks in advance :) 我真的很想知道..在此先感谢:)

Not necessarily. 不必要。

Docs just recommending that format: 文件只建议使用以下格式:

This class(bean) uses standard JavaBean naming conventions for property setter and setter methods, as well as private visibility for the fields. 此类(bean)对属性设置器和设置器方法使用标准JavaBean命名约定,并对字段使用私有可见性。 Although this is the recommended design, it is not required. 尽管这是推荐的设计,但不是必需的。 Hibernate can also access fields directly, the benefit of accessor methods is robustness for re factoring. Hibernate也可以直接访问字段,访问器方法的好处是重构的鲁棒性。

My possible reason's to follow that format: 我可能的原因是采用以下格式:

1)First of all Readability of your code. 1)首先,您的代码具有可读性

2)Secondly,no one provide a name getCompanyName() to get username :) 2)其次,没有人提供名称getCompanyName()来获取username :)

Few things to note: 几件事要注意:

  • A hibernate persistent entity is POJO. 休眠持久性实体是POJO。
  • Set/Get method of a POJO enables a framework (like hibernate) to introspect the property of the POJO provided it follows a Javabean Convention . POJO的Set / Get方法使框架(如休眠)可以自省 POJO的属性,前提是它遵循Javabean约定
  • In absence of any standard convention, it will be difficult to create generalized frameworks which can read the properties of a POJO. 在没有任何标准约定的情况下,将很难创建可以读取POJO属性的通用框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM