Skip to content
标签
欢迎扫码关注公众号

JavaBean getter/setter 命名规范


原文:JavaBean getter/setter 命名规范 [特例]


一般 JavaBean 属性以小写字母开头,驼峰命名格式,相应的 getter/setter 方法是 get/set 接上首字母大写的属性名。

例如:属性名为 userName,其对应的 getter/setter 方法是 getUserName/setUserName

但是,还有一些特殊情况:

  1. 如果属性名的 第二个字母大写,那么该属性名直接用作 getter/setter 方法中 get/set 的后部分,就是说 大小写不变。例如属性名为 uName,方法是 getuName/setuName

  2. 如果属性名的 前两个字母是大写(一般的专有名词和缩略词都会大写),也是属性名直接用作 getter/setter 方法中 get/set 的后部分。例如属性名为 URL,方法是 getURL/setURL

  3. 如果属性名的 首字母大写,也是属性名直接用作 getter/setter 方法中 get/set 的后部分。例如属性名为 Name,方法是 getName/setName,这种是最糟糕的情况,会找不到属性出错,因为默认的属性名是 name。

  4. 如果属性名 以 "is" 开头,则 getter 方法会省掉 getset 方法会去掉 is 。例如属性名为 isOK,方法是 isOK/setOK

需要注意的是有些开发工具自动生成的 getter/setter 方法,并没有考虑到上面所说的特例情况,会导致 bug 的产生。

我们在定义 JavaBean 的属性名时,应该尽量避免属性名的头两个字母中任意一个为大写以及属性名以 "is" 开头。

阿里巴巴 Java 开发手册 1.2.0 中也规定了 不允许以 is 开头作为字段名.

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.