关键字'Private'和'Final'有什么区别?(What is the difference between the keyword 'Priv

编程入门 行业动态 更新时间:2024-10-27 14:26:11
关键字'Private'和'Final'有什么区别?(What is the difference between the keyword 'Private' and 'Final'?)

PHP中的Private和Final之间存在混淆。

例如,我有3个类:

A类(父类) B级(儿童班) C级(其他类)

我的理解:

答:A类,B类和C类可以访问公共变量和方法 B: 私人变量和方法只能由A类访问。 C: 受保护的变量和方法只能由A类和B类访问 D: 最终方法只能由A级访问,而不是B级。

我的问题是:

使用私有后我们可以实现最终的功能然后我们使用final?

我问这个问题只是为了我自己的澄清。

Having a confusion between Private and Final in PHP.

For example I have 3 classes:

Class A (parent class) Class B (child class) Class C (other class)

What I understand:

A: Public variables and methods are accessible to the class A, class B and class C B: Private variables and methods are only accessible to the class A. C: Protected variables and methods are only accessible to the class A and class B D: Final methods are only accessible to the class A not to class B.

My Question is:

After using private we can achieve functionality like final then why we use final?

I am asking this question only for my clarification for myself.

最满意答案

为了说清楚,关键字final不必对方法的可见性做任何事情。 方法的可见性由关键字定义: public , protected和private 。

final关键字定义了另一个类是否可以覆盖该方法(如果方法是最终的,它不能被antoher类覆盖),而另一个类可以访问该方法。 否则它甚至无法访问该方法,因此它既不能使用/调用该方法也不能覆盖它。

也只有方法可以是最终的,它不能与属性一起使用。


A,B和C是正确的,正如我上面所说,关键字final与可见性没有任何关系,因此D不正确。

有关更多信息,请参阅相应的手册页:

final关键字 属性和方法可见性

Just to make it clear, the keyword final doesn't have to do anything with the visibility of a method. The visibility of a method is defined by the keywords: public, protected and private.

The final keyword defines if another class can overwrite the method or not (if a method is final it can't be overwritten by antoher class), when the other class has access to the method. Otherwise it won't even have access to the method, so it neither can use/call the method nor overwrite it.

Also only methods can be final it can't be used with properties.


A, B and C are correct and as I said above the keyword final doesn't have anything to do with the visibility, so D is not correct.

For more information see the corresponding manual pages:

final keyword property and method visibility

更多推荐

本文发布于:2023-07-31 07:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   关键字   Private   Final   difference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!