重载静态变量是否需要静态后期绑定?

发布于 2024-12-14 09:36:30 字数 200 浏览 2 评论 0原文

我的一个朋友问我他​​是否可以在 Java 中重写静态变量,我很惊讶他竟然想到了如此奇怪的编码方式。然后他向我解释说这在 PHP 中是可能的,我想知道优秀的开发人员是否应该这样做有充分的理由。在我看来,静态成员的特征是类成员,与对象无关,因此它们与类的派生无关,但我无法说服他,因为他是如此天真和固执。

谁能对这整件事提出一个很好的论据,或者让我相信这是一个很酷的功能?

A friend of mine asked me whether he can override a static variable in Java and I was shocked that he even thought about such a weird way of coding. Then he explained me that this is possible in PHP and I want to know whether there are a good reasons why a good developer should do that. In my opinion static members are characterized as class members and not related to an object and therefore they are not related to derivation of classes, but I cannot convince him as he is so naive and stubborn.

Can anyone give either a good argument against this whole thing or convince me that this is a cool feature?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-12-21 09:36:30

静态继承没有任何意义。并不是说不可能,只是你没有从中得到任何好处。

通过正常继承,您可以获得对同一事物有不同实现的好处,并且不知道/关心将使用哪个实现。使用静态继承,您没有可操作的对象,并且使用的是类名,因此无法利用多态性。

例如,如果您正在调用 Child.someMethod() ,那么您将与子级的实现相关联,如果您实际上只需要父级,则可以只执行 Parent.someMethod() 。如果您需要向 Parent 实现添加一些内容,您只需创建一个 Child.someOtherMethod() ,在其中调用 Parent 并在之后执行一些其他操作。静态继承只是语法糖......

The static inheritance does not make any sense. It is not that it is not possible, just that you get no benefit from it.

With normal inheritance you get the benefit of having a different implementation for the same thing and not knowing/caring which implementation will be used. With static inheritance you don't have an object to operate with and you are using a class name, so you cannot take advantage of polymorphism.

E.g. if you are calling Child.someMethod() you are tied to implementation of the child and if you actually just need the parent, you can just do Parent.someMethod() instead. If you need to add something to Parent implementation, you just make a Child.someOtherMethod() where you call the parent and do some other things after. The static inheritance is just syntactic sugar...

羁绊已千年 2024-12-21 09:36:30

据我所知,Java中的static关键字是用来定义Class变量的。类变量对于该类的所有对象都有一个实例。所以在Java中你不能覆盖静态变量,这是没有意义的。对一个类中的静态变量所做的任何更改都会传播到另一个类。这就是 JAVA 中 static 的用途。

这与 PHP 中的工作方式相同(我并不是真正的 PHP 专家),但是如果您的朋友可以提供代码,显示 PHP 中的静态变量被覆盖并且该变量具有与另一个类不同的值,我会很高兴。

As far as I know, the static keyword in Java is used to define Class variables. A Class variable has one instance for all Objects of that class. So in Java you can not override a static variable, it doesn't make sense. Any changes done to a static variable in one Class propagates to another class. This is what static is used for, in JAVA.

This is the same way IT SHOULD WORK in PHP (I am not really a PHP expert), but if your friend can provide code showing that a static variable in PHP was overridden and the variable has a different value that from another class, I will be very glad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文