链表头节点标识符/指针访问问题
class LinkedList{
private $first;
public function Merge(LinkedList $ll){
//We can't access $ll->first for merging operation as it's private inside $ll.
//We can only access $this->first not $first of $ll
}
}
解决这个问题的办法是什么?
- 公开 $first 吗?这不是一个好主意
- 为 $first 属性实现 getter 方法
有人可以建议我访问 $first 的正确方法吗?
class LinkedList{
private $first;
public function Merge(LinkedList $ll){
//We can't access $ll->first for merging operation as it's private inside $ll.
//We can only access $this->first not $first of $ll
}
}
What could be the solution for this?
- Make $first public? that is not a nice idea
- Implement getter method for $first property
Can someone please suggest me correct way to access $first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问是通过类而不是实例来控制的。您可以从类中的代码访问该类的任何私有成员。无论您使用
$this
引用还是其他引用都没有关系。PHP、Java 和 C++ 中也是如此。
Access is controlled though the class, not the instance. You can access any private member of a class from code that's within the class. Whether you use the
$this
reference or another doesn't matter.This is the same in PHP, Java, and C++.