【问题标题】:calling base method using new keyword使用 new 关键字调用基本方法
【发布时间】:2012-05-09 02:36:01
【问题描述】:

在这个link,他们有这个代码:

public class Base
{
   public virtual void Method(){}
}

public class Derived : Base
{
   public new void Method(){}
}

然后这样调用:

Base b = new Derived();
b.Method();

我的实际代码是这样的:

public class Base
{
   public void Method()
   {
        // bla bla bla
   }
}

public class Derived : Base
{
   public new void Method()
   {
        base.Method();
   }
}

有必要用base.Method();打电话吗?
还是将派生类中的方法留空?

【问题讨论】:

  • 好问题。大多数关于推导的在线帮助往往会忽略这一点。

标签: c# methods new-operator keyword


【解决方案1】:

如果你真的需要调用基类的方法,你需要'base'。 base.Method(); 是正确的方法。

Knowing When to Use Override and New Keywords (C# Programming Guide)

【讨论】:

猜你喜欢
  • 2016-03-06
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 2021-12-08
  • 1970-01-01
相关资源
最近更新 更多