The hidden implementation(被隐藏的具体实现)

It is helpful to break up the playing field into class creators (those who create new data types) and client programmers (the class consumers who use the data types in their applications).将程序开发人员按照角色分为类创建者(那些创建新数据类型的程序员)和客户端程序员(那些在应用中使用数据类型类的消费者)是大有裨益的.

The goal of the client programmer is to collect a toolbox full of classes to use for rapid application development.客户端程序员的目标是收集各种用来实现快速应用开发的类.

The goal of the class creator is to build a class that exposes only what's necessary to the client programmer and keeps everything else hidden.类的创建者是构建类,这个类只向客户端程序员暴露必须的部分,而隐藏其他部分.

Why? Because if it's hidden, the client programmer can't access it, which means that the class creator can change the hidden portion at will without worrying about the impact on anyone else.为什么呢?因为如果被隐藏,客户端程序员将不能够访问它,这意味着类的创建者可以改变隐藏的部分并且不用担心会对其他人产生影响.

The hidden portion usually represents the tender insides of an object that could easily be corrupted by a careless or uninformed client programmer, so hiding the implementation reduces program bugs. 被隐藏部分通常代表着对象的内部脆弱的部分,它很容易粗心的不知内情的客户端程序员所毁坏,因此将实现隐藏起来可以减少bug.


In any relationship it's important to have boundaries that are respected by all parties involved. 在任何关系模型中,建立边界对所有参与者都是有益的.

When you create a library, you establish a relationship with the client programmer, who is also a programmer, but one who is putting together an application by using your libray, possibly to build a bigger library.当创建一个类库时,就建立了与客户端程序员之间的关系,他们同样也是程序员,但是他们是使用你的类库来构建程序或者构建更大类库的程序员.

If all the members of a class are available to everyone, then the client programmer can do anything with that class and there's no way to enforce rules.如果一个类的所有成员对所有人可见,那么客户端程序员就可以对这个类做任何操作了,而不受任何约束.

Even thougn you might really prefer that the client programmer not directly manipulate some of the members of your class without access control there's no way prevent it.即使你希望客户端程序员不要直接操作你的类中的某些成员,但是如果没有任何访问控制,将无法阻止此事发生.

Everything's naked to the world.所有的事情都将赤裸裸的暴露在世人面前.


So the first reason for access control is to keep client programmers' hands off portions they shouldn't touch--parts that are necessary for the internal operation of the data type but not part of the interface that users need in order to solve their particular problems.因此,访问控制存在的第一个原因就是让客户端程序员无法触及他们不应该触及到部分--这些部分对数据类型的内部操作是必需的,但并不是用户解决待定问题所需的接口的一部分.

This is actually a service to client programmers because they can easily see what's important to them and what they cann ingore.实际上,这对客户端程序员来说是一个服务,因为他们可以很容易的看出哪些东西对他们很重要,哪些可以忽略.


The second reason for access control is allow the library designer to change the internal workings of class without worrying about how it will affect the client programmer.访问控制存在的第二个原因就是允许库的设计者改变类内部的工作方式而不用担心会影响都客户端程序员.

For example, you might implement a particular class in a simple fashion to ease development, and then later discover that you need to rewrite it in order to make it run faster.例如,你可能为了减轻开发任务二以某种简单的方式实现了某个特定类,但稍后发现你必须改写它才能使其运行得更快。

If the interface and implementation are clearly separated and protected, you can accomplish this easily.如果接口和实现可以清晰地分离并得以保护,那么你就可以轻而易举地完成这项工作。


Java uses three explicit keywords to set the boundries in class: public, private, and protected.Java用三个关键字在类的内部设定边界:public、private、protected。

These access specifiers determine who can use the definitions that follow. 这些访问指定词决定了紧跟其后被定义的东西可以被谁使用。

public means the following element is available to everyone.public表示紧随其后的元素对任何人都是可用的.

The private keyword, on the other hand, means that no one can access that element except you, the creator of the type, inside methods of that type.而private这个关键字表示除类型创建者和类型的内部方法之外的任何人都不能访问的元素。

private is a brick wall between you and the client programmer.private就像你与客户端程序员之间的一堵砖墙。

Someone who tries to access a private member will get a compile-time error.如果有人试图访问private成员,就会在编译时得到错误信息。

The protected keyword acts like private, with the exception that an inheriting class has access to protected members, but not private members.protected关键字与private作用相当,差别仅在于继承的类可以访问protected成员,但是不能访问private成员。

Inheritance wil be introduced shortly.稍后将会对继承进行介绍。


Java also has a "default" access, which comes into play if you don't use one of the aforementioned specifiers.Java还有一种默认的访问权限,当没有使用前面提到的任何访问指定词时,它将发挥作用。

This is usually called package access because classes can access the members of other classes in the same package(library component), but outside of the package those same members appear to be private.这种权限通常被称为包访问权限,因为在这种权限下,类可以访问在同一个包(库构件)中的其他类的成员,但是在包之外,这些成员如同指定了private一样。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值