dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>java基础>文章内容
对象成员的访问控制
来源: 作者:
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:
  • At the top level—public, or package-private (no explicit modifier).
  • At the member level—public, private, protected, or package-private (no explicit modifier).

访问权限修饰词决定是否别的类可以使用一个特别的field或者调用一个特别的方法。下面是访问控制的两个水平:1、 top level--public,或者packgage-private//可以理解为类的级别                      2、member level--public,private,protected,or package-private //可以理解为类的内部级别

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes—you will learn about them in a later lesson.)

1、一个类可以被public修饰,意味着这个类可以被任何地方的任何类来访问。

2、无修饰词(默认形式,被称作package-private),他只能被他所在的包内的类访问

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

1、public,成员可以被任何地方的任何类来访问。

2、无修饰词(默认形式,被称作package-private),只能被他所在的包内的类访问。

3、private,成员只能被他自己的类访问,其他的外面的都不可以访问。

4、protected,成员只能被他所在的包和他的子类访问。比package-private访问范围大一些。

The following table shows the access to members permitted by each modifier.

Access Levels 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
The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class — declared outside this package — have access to the member. The fourth column indicates whether all classes have access to the member.
第一列表示类本身是否有权限访问被访问权限修饰符修饰的类成员,可以看到,类总是可以访问他们的成员。
第二列表示在同一个包内的类是否可以访问成员,可以看到,除了private修饰的,其他的都可以访问。
第三列表示是否类的子类(在包外面的)可以访问成员,可以看到,private,包内部的不可以访问,其他ok
第四列表示是否任何类可以访问,可以看到,只有被public修饰,任何类才可以访问。

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

访问控制权限通过两个方法影响你的编程。1、当你使用的类来自另外的地方,例如java平台,访问控制权限决定了这些类中的那些成员你可以使用。2、当你写类时,你需要决定你的成员参数和方法使用什么样子的访问权限,以决定你的类的用途。

Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related. Classes and Packages of the Example Used to Illustrate Access Levels

Classes and Packages of the Example Used to Illustrate Access Levels

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
Visibility Modifier Alpha Beta Alphasub Gamma
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.

选择访问控制的细节:

1、给一个特殊的成员使用最严格的访问控制权限。使用private除非你有一个恰当的理由不用它。

2、除非是常数,否则不要使用public。public fields使你进行一个特殊的执行,并且会限制你代码的自由度。


上一篇:在linux下使用u盘   下一篇:在jsp环境中配置使用FCKEditor
[收藏] [推荐] [评论(1条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·JUnit单元测试(2)
·JUnit单元测试(1)
·浅谈在Java语言中究竟是传值还是
·Java 5.0 多线程编程实践
·Java的文件 读和写
·对于JAVA基础测试中常见的异常问
·JAVA程序员面试33问,你能回答多
·Java Reflection (JAVA反射)详解
·JAVA中配置环境变量设置方法大全
·讲述java语言中内部类的研究
·详细解析Java中抽象类和接口的区
·Java开发中的事件驱动模型实例详
  相关文章
·JAVA写的MP3标签读写器
·如何使用Static的变量和方法
·JAVA如何调用DOS命令
·Java开发者需坚守的十大基本准则
·java多线程编程
·深入 JAVA对象的复制与比较
·Vector & ArrayList 哪一个更好
·java学习----理解面向对象的程序
·Java语言的克隆与深层次克隆技术
·Java语言的参数传递和处理方法 -
·Java/J2EE中文问题终极解决之道
·怎么向一个命令行Java程序传递参
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763