C#中:如何调用基类的静态方法从派生类的静态方法? [英] C#: How do I call a static method of a base class from a static method of a derived class?

查看:591
本文介绍了C#中:如何调用基类的静态方法从派生类的静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我有基类产品和派生类的Widget。

In C#, I have base class Product and derived class Widget.

产品包含一个静态方法的MyMethod()。

Product contains a static method MyMethod().

我想从静态方法Widget.MyMethod()调用静态方法Product.MyMethod()。

I want to call static method Product.MyMethod() from static method Widget.MyMethod().

我不能使用base关键字,因为这仅适用于实例方法。

I can't use the base keyword, because that only works with instance methods.

我可以调用Product.MyMethod()明确,但如果我以后更改的Widget从另一个类派生,我不得不修改方法

I can call Product.MyMethod() explicitly, but if I later change Widget to derive from another class, I have to revise the method.

有一些语法在C#类似于基地,让我从一个派生类的静态方法?

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?

推荐答案

静态方法基本上是从面向对象的概念,回退的方法。因此,他们不是在继承层次结构非常灵活,它不可能直接做这样的事情。

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.

我能想到的最接近的是一个<$ C 。$ C>使用指令

The closest thing I can think of is a using directive.

using mybaseclass = Namespace.BaseClass;

class MyClass : mybaseclass {

  static void MyMethod() {  mybaseclass.BaseStaticMethod();  }

}

这篇关于C#中:如何调用基类的静态方法从派生类的静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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