问题描述
我需要一个类来继承另一个类,让它们分别是派生类和基类。我在基类中有一个静态函数_init(),我不想让这个方法在派生类中继承。我试过Final关键字,但它不工作。我应该怎么办?
I need a class to inherit another class, let them be derived class and base class respectively. i have a static function _init() in base class, I don't want that method to be inherited in the derived class. I tried the Final keyword but its not working. What should i do?
推荐答案
你误解了什么是最终的意思。
You're misunderstanding what final means.
OOP的基本原则之一是,如果一个类是另一个类的子类,那么该类将获得它继承的类的所有功能。你不能继承只是一些超类的功能,它是全部或没有。
One of the foundation principles of OOP is that if a class is a subclass of another class, then that class gets all the functionality of the class it inherits from. You can't inherit just some of the superclass functionality, it's all or nothing.
这样做的原因有点难以掌握,但一旦你这样做,你会意识到这是有道理的。也就是说,如果一段代码能够处理特定类的项目,那么它也必须能够处理项目的任何子类。如果一个子类没有它的超类的所有功能,你不能这样做。
The reason for this is a bit tricky to get to grips with but once you do, you'll realise that it makes sense. Namely, if a piece of code is able to process items of a particular class, then it must also be able to process any subclass of the item as well. If a subclass didn't have all the functionality of its superclass, you couldn't do that.
例如,假设您有一个类定义与支付网关的接口。由于所有付款网关都不同,因此您无法(或至少不应)实施可与任何可用付款网关连接的单一课程。但一般来说,支付网关以相同的基本方式工作。将发送请求,其中包括用户的信用卡详细信息和您要从卡中扣除的金额。支付网关响应一条消息,说明付款是否被授权。
For example, imagine you have a class that defines interfacing with a payment gateway. As all payment gateways are different, you couldn't (or at least shouldn't) implement a single class that can interface with any of the payment gateways that are available. But in general, payment gateways work in the same basic way. A request is sent, including a user's credit card details and amount you want to debit from the card. The payment gateway responds with a message saying whether payment was authorized or not.
您可以使用抽象超类来实现这种功能,该类定义了如何与付款网关接口,然后为每个付款网关创建一个子类想要使用(paypal,sagepay,任何)。
You'd implement that kind of functionality with an abstract superclass that defines how you interface with payment gateways in general, then you'd create a subclass for each payment gateway you want to use (paypal, sagepay, whatever).
由于所有这些子类继承自同一个基类,因此您可以确定它们都实现了某些方法,而且您可以毫不费力地向这些方法发送消息。只要你处理的支付类是抽象支付类的子类,那么一切都应该工作。
As all these subclasses inherit from the same base class, you can be certain that they all implement certain methods, and you can send messages to those methods without worry. As long as the payment class you're dealing with is a subclass of your abstract payment class, then everything should work.
如果其中一个子类不能从抽象超类中实现某个东西,那么你不能确定向子类发送一个特定的消息是否有效,抛出异常。这将使继承而没有意义。
If one of the subclasses was able to not implement something from the abstract superclass, then you couldn't be sure that sending a particular message to a subclass would work, or throw an exception. This would make inheritance rather pointless.
PHP中的final(确实是大多数实现它的OOP语言或类似的概念)意味着这是这个方法的最终实现如果一个程序员子类化我,不能用自己的实现替换我的最终方法。这并不意味着方法将不会在子类中可用,只是你不能改变它们。
Final in PHP (and indeed most OOP languages that implement it or similar concepts) means "This is the final implementation of this method. If a programmer subclasses me, then he can't replace my final methods with his own implementation". It doesn't mean that the methods will not be available in the subclass, just that you can't change them.
如果您真的必须从子类中删除功能,可以通过覆盖超类方法并将其替换为空方法(不做任何操作)来实现。但不要这样做,因为它会导致上面讨论的类型的问题。
If you really must remove functionality from a subclass, you can do it by overriding the superclass method and replacing it with an empty method (one that does nothing). But don't do that, as it will cause problems of the type discussed above.
一般情况下,如果你遇到的情况下,你有一个超类,你不需要在一个子类中的功能,它是一个代码气味,告诉你,你可能需要重新思考你的设计。
Generally, if you run into situations where you have functionality in a superclass that you don't need in a subclass, it's a code smell that's telling you that you might need to rethink your design.
这篇关于它最终的静态类方法可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!