Java:Access levels:修订间差异
imported>Soleverlee 以“{| class="wikitable" border="1" |- ! Modifier ! Class ! Package ! Subclass ! World |- | public | Y | Y | Y | Y |- | protected | Y | Y | Y | N |- | no modifier | Y |...”为内容创建页面 |
imported>Soleverlee 无编辑摘要 |
||
第31行: | 第31行: | ||
| N | | N | ||
|} | |} | ||
=Tips on Choosing an Access Level:= | |||
If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this. | |||
*Use the most restrictive access level that makes sense for a particular member. Use <font color="red">private</font> unless you have a good reason not to. | |||
*<font color="red">Avoid public fields</font> except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code. | |||
[[Category:Programe]] | [[Category:Programe]] |
2016年9月25日 (日) 15:17的最新版本
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
Tips on Choosing an Access Level:
If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.
- Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
- Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.