C#:如何从派生类的静态方法调用基类的静态方法?
在 C# 中,我有基类 Product 和派生类 Widget。
产品包含静态方法 MyMethod()。
我想从静态方法 Widget.MyMethod() 调用静态方法 Product.MyMethod()。
我无法使用 base 关键字,因为它仅适用于实例方法。
我可以显式调用 Product.MyMethod(),但如果我稍后将 Widget 更改为从另一个类派生,我必须修改该方法。
C# 中是否有一些类似于 base 的语法,允许我从派生类的静态方法调用基类的静态方法?
In C#, I have base class Product and derived class Widget.
Product contains a static method MyMethod().
I want to call static method Product.MyMethod() from static method Widget.MyMethod().
I can't use the base keyword, because that only works with instance methods.
I can call Product.MyMethod() explicitly, but if I later change Widget to derive from another class, I have to revise the method.
Is there some syntax in C# similar to base that allows me to call a static method from a base class from a static method of a derived class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
静态方法基本上是一种从面向对象概念回退的方法。 因此,它们在继承层次结构中不是很灵活,并且不可能直接执行这样的操作。
我能想到的最接近的东西是
using
指令。static
methods are basically a method to fallback from object oriented concepts. As a consequence, they are not very flexible in inheritance hierarchies and it's not possible to do such a thing directly.The closest thing I can think of is a
using
directive.可以这样做,但我不建议这样做。
It can be done, but I don't recommend it.
使用反射调用静态方法与调用实例方法完全相同,只是为实例传递 null 。 您需要 FlattenHierarchy 因为它是在祖先中定义的。
进一步的阅读和思考让我提出了与其他回复者相同的问题:为什么要使用这样的静态方法? 您是否正在尝试进行函数式编程?如果是这样,为什么不使用 lambda 表达式呢? 如果您想要具有共享状态的多态行为,实例方法会更好。
Calling a static method using reflection is exactly the same as calling an instance method except that you pass null for the instance. You need FlattenHierarchy because it's defined in an ancestor.
Further reading and thinking leaves me asking the same questions as others who have responded: why use static methods like this? Are you trying to do functional programming, and if so why not use lambda expressions instead? If you want polymophic behaviours with shared state, instance methods would be better.
它可以做到:
对于单元测试受保护的静态方法很有用(例如)。
It can be done:
Can be useful for unit testing protected static methods (for example).
首先也是最重要的,如果您担心重新为类指定父级,那么您可能在继承方面做错了。 继承应该用于建立“is-a”关系,而不仅仅是促进代码重用。 如果您需要单独重用代码,请考虑使用委托,而不是继承。 我想你可以在子类型和它的父类型之间引入一个中间类型,但我会让这种可能性驱动我的设计。
其次,如果您需要使用基类中的功能但扩展它并且用例调用静态方法,那么您可能需要考虑使用一些外部类来保存该功能。 我心目中最经典的案例就是工厂模式。 实现工厂模式的一种方法是通过工厂方法,这是构造该类的实例的类上的静态方法。 通常构造函数是受保护的,因此工厂方法是从外部构建类的唯一方法。
在继承层次结构中实现工厂方法重用的一种方法是将公共代码放入受保护的方法中,并从工厂方法调用该方法,而不是直接从子类型工厂方法调用基类工厂方法。 更好的实现可能使用相同的技术,但将工厂方法移动到工厂类并使用构造函数逻辑(现在是内部的,不是私有的),也许与初始化方法结合使用来创建对象。 如果您继承的行为是来自类的外部行为(解密/验证/等),则可以在工厂内使用共享方法(或组合)以允许在工厂方法之间重用。
如果不知道使用静态方法的目标,就很难给您一个确切的方向,但希望这会有所帮助。
First and foremost, if you're worried about re-parenting a class, then you're probably doing inheritance wrong. Inheritance should be used to establish "is-a" relationships, not simply foster code reuse. If you need code re-use alone, consider using delegation, rather than inheritance. I suppose you could introduce an intermediate type between a sub-type and its parent, but I would let that possibility drive my design.
Second, if you need to use functionality from the base class but extend it AND the use case calls for a static method, then you might want to consider using some external class to hold the functionality. The classic case for this in my mind is the Factory pattern. One way to implement the Factory pattern is through Factory Methods, a static method on a class that constructs an instance of that class. Usually the constructor is protected so that the factory method is the only way to build the class from outside.
One way to approach re-use with Factory Methods in an inheritance hierarchy would be to put the common code in a protected method and call that method from the Factory Method rather than directly call the base class Factory Method from a sub-types Factory Method. A better implementation might use the same technique but move the Factory Methods to a Factory class and use the constructor logic (internal now, not private), perhaps in conjunction with an initialization method(s), to create the object. If the behavior you are inheriting is external from the class (decryption/validation/etc), you can use shared methods (or composition) within the Factory to allow re-use between the Factory methods.
Without knowing the goal of your use of static methods it's difficult to give you an exact direction, but hopefully this will help.
静态方法不是多态的,所以你想做的事是不可能的。
尝试找到一种将静态方法视为多态的方法是可能的,但很危险,因为语言本身不支持它。
一些建议:
Static methods are not polymorphic, so what you want to do is impossible.
Trying to find a way to treat static methods as polymorphic is possible but dangerous, because the language itself doesn't support it.
Some suggestions:
考虑到静态方法不应该在实例数据中中继...您应该有一个静态“MyMethod”,它根据参数或类似的东西而表现不同。
请记住,在一个类中定义的静态方法只是一种对代码进行排序的方法...但如果该方法放置在其他地方也是一样的...因为它不会中继它所在的对象。
编辑:我认为您最好的选择是明确使用 Product.MyMethod...如果您认为...您的 Widget 应该不可能更改其基类...并且也在在这种情况下,只需对代码进行微小的更改。
Having into account that a Static method shoudn't relay in instance data... you should have a static "MyMethod" that behaves diferent based on a parameter or something like that.
Remember that the static method defined in one class is just a way to order your code... but it is the same if that method is placed elsewhere... because it don't relay on the object where it is placed.
EDIT: I think your best choise is to use Product.MyMethod explicitly... If you think it... it should't be probable that your Widget change its base clase... and also in that case it is a minor change in code.
静态方法是“类级”方法。 它们旨在成为适用于特定类的所有实例的方法。 因此,继承此方法没有意义,因为它会改变应用于类实例的含义。 例如,如果您循环遍历产品集合(一些是 Widget,一些不是)并在每个产品上调用 MyMethod,则调用的方法将由类的实例确定。 这违反了静态方法的目的。
您可能可以通过根本不使用静态方法来更干净地完成您想要的事情,因为在您的示例中,MyMethod 似乎并不适用于 Product 的所有实例。 但是,您可以通过使用“IMyMethod”等接口类来实现您所描述的相同效果。 这种方法仍然没有使用静态方法。 我想我没有看到需要静态方法。 为什么要首先使用静态方法?
Static methods are "Class-Level" methods. They are intended to be methods that apply to all instances of a specific class. Thus, inheritance of this method does not make sense as it would change the meaning to apply to instances of the class. For instance, if you were looping through a collection of Products (some of Widget, some not) and called MyMethod on each Product then the method that was called would be determined by the instance of the class. This violates the purpose of static methods.
You can probably do something like you want more cleanly by simply not using static methods at all, because in your example it does not seem like MyMethod applies to all instances of Product. However, you can achieve the same affect you are describing by using an interface class like ‘IMyMethod’. This approach is still not using a static method. I guess I’m not seeing a need for a static method. Why do you want to use a static method to begin with?
这很简单。 比使用别名、反射等简单得多。也许在 .NET、IDK 的新添加中它变得更容易,但这工作得很好。 就像实例方法一样,访问基方法不需要使用
base
,它是可选的,当继承类和基类具有相同名称的方法时通常是必需的。 即使没有base
关键字,您也可以访问基类的静态方法,就像它们位于您调用的类中一样。 我仅在从派生类的 static 方法调用基类 static 方法时对此进行了测试。 如果您从实例方法调用静态方法,则可能不起作用。It's very simple. Much simpler than using aliasing, reflections, etc. Maybe it's been made easier in newer additions of .NET, IDK, but this works perfectly fine. Just like with instance methods, accessing base methods doesn't require
base
be used, it is optional, usually necessary when the inheriting and base class have a method of the same name. Even without thebase
keyword, you can access the static methods of a base class as if they were in the class you are calling from. I only tested this when calling a base static method from a derived class's static method. Might not work if you are calling from an instance method to a static method.