简体   繁体   English

强制子对象实现静态方法

[英]force child objects to implement static method

I want to allow all my child objects to implement static method. 我想让我所有的子对象都实现静态方法。 For example: 例如:

public abstract class A {}
public class B : A {}
public class C : A {}

I want B and C to have DoSomething() static method, where in A there will be default implementation of DoSomething(). 我希望B和C具有DoSomething()静态方法,其中A中将有DoSomething()的默认实现。

What is the bes practice to do so? bes这样做的做法是什么?

You can't do so. 你不能那样做。 You wouldn't be able to call the static method polymorphically anyway. 无论如何,您将无法多态调用静态方法。 Basically, static members and polymorphism don't work together. 基本上,静态成员和多态性不能一起使用。 Either give up on polymorphism, or create a parallel type hierarchy where those are instance methods. 要么放弃多态,要么创建一个并行类型层次结构,其中这些都是实例方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM