最终的静态类方法可能吗? [英] Is a final static class method possible?

查看:151
本文介绍了最终的静态类方法可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个类来继承另一个类。我在基类中有一个静态函数 _init(),我不希望在派生类中继承该方法。我尝试了 final 关键字,但它不起作用。我该怎么办?

I need a class to inherit another class. I have a static function _init() in the base class, and I don't want that method to be inherited in the derived class. I tried the final keyword but it's 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.

您可以使用抽象超类来实现这种功能,该超类定义了一般与支付网关的接口方式,然后为每个支付网关创建一个子类想用(贝宝,贝琪,等等)。

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中的最终版本(实际上大多数实现它或类似概念的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.

这篇关于最终的静态类方法可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆