属性:访问指定
当我参考一本书时,我得到以下陈述:
当一个数据类型或方法被定义为 public 时,其他对象可以直接访问它。当数据类型或方法被定义为 private 时,只有特定的对象可以访问它。
现在这真的很令人困惑。 Public 和 Private 是访问说明符,仅定义属性或方法的范围。
为什么对象与访问说明符混合在一起?对象是否必须对 public 、 private 或 protected 做任何事情,除了以下事实:如果某件事被定义为 public 那么对象也将能够访问,无论范围如何
While I was refering to a book , I got the following statements:
When a data type or method is defined as public , Other Objects can directly access it. When a data type or method is defined as private , only the specific object can access it.
Now this is really confusing. Public and Private are Access Specifiers which only define the scope of a attribute or method.
Why object is mixed with access specifiers? Does object has to do any thing with public , private or protected apart from the fact that if some thing is defined as public then objects too will be able to access irespective of the scope
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是范围问题,而是访问限制修饰符。如果将函数声明为私有,则意味着只有该类可以调用该函数。
民众:
任何人都可以调用这些函数
私人的:
只有那个类和反射引擎
受保护:
仅该类及其派生成员
内部的:
中的所有班级公开
向该集会
This is not a scope question but an access limitation modifier. If you declare a function as private that means only that class can call that function.
Public:
Any one can call these functions
Private:
Only that class and the refection engine
Protected:
Only that class and its derived member
Internal:
Public to all the classes in that assembly
A small ex
这定义了对象的行为。因此,访问说明符对于对象很重要。想象一下,如果您有一个外观类型的对象,您不想公开操作的所有细节,而是希望向消费者公开一个简单的公共接口(例如,Save() 方法)。这就是为什么您必须考虑对象的说明符。
And that defines the behaviour of the object. Hence the access specifiers are important for the object. Imagine if you have a facade kind of object, you don't want to expose all the details of the operation, rather you want a simple public interface (eg, Save() method) to be exposed for the consumers. That's why you have to consider the specifiers for objects.