Thinking in java -6 隐藏的实现 Hidden implementation

1.从不同角色的角度看类的存在

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 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 uniformed client programmer, so hiding the implementation reduces program bug.
作为类的使用者和类的创建者而言他们所需要的功能是不同的(类的使用者只需将类作为一种新的数据类型使用,并不关心其实现细节;类的创建者则需要精心设计类的具体接口,如类的方法),所需要提供的功能也是不同的。
如此设计有利于数据隐藏,防止类数据遭到破坏,提高代码鲁棒性。

2.实现手段

Java uses three explicit keywords to set the boundaries in a class: public, private and protected.
通过使用public/private/protected关键字及默认不加任何修饰手段方法,修饰类的方法和变量来设置访问界限。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值