gpt4 book ai didi

php - 为什么 PHP 允许从其他实例调用私有(private)方法?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:40 28 4
gpt4 key购买 nike

PHP 允许调用私有(private)方法,只要它们来自同一类的实例。

这就是the PHP docs解释一下:

Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.

这不会破坏封装吗? 实现的具体细节是已知的这一事实如何证明它的合理性?

附注是否有其他(面向对象的)语言可以做到这一点?

最佳答案

对于大多数语言都是如此,在 java 中你也可以这样做,这是因为对象来自同一个实例,它“知道”同一个实例具有的所有属性

关于php - 为什么 PHP 允许从其他实例调用私有(private)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16519597/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com