是否可以在继承中进行方法重载 [英] Is it possible to do method overloading in inheritance

查看:99
本文介绍了是否可以在继承中进行方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


是否可以使用c#

Hi,
Is it possible to do method overloading in inheritance using c#

推荐答案

在继承中进行方法重载?这个问题的答案是肯定的
参见以下示例
Anser to this question is yes
see below example
class Base
  {
      public virtual void Foo(int x)
      {
          Console.WriteLine("Base.Foo(int)");
      }
  }

  class Derived : Base
  {
      public override void Foo(int x)
      {
          Console.WriteLine("Derived.Foo(int)");
      }

      public void Foo(string o)
      {
          Console.WriteLine("Derived.Foo(string)");
      }
  }

  class Test
  {
      static void Main()
      {
          Derived d = new Derived();
          int i = 10;
          d.Foo(i);  // it prints ("Derived.Foo(int)"
          d.Foo("Hii");//it prints ("Derived.Foo(string)"
          Console.ReadKey();
      }
  }


会有什么疑问?让我以更一般的方式来解释它.

重载"在技术上没有任何意义,除了命名和方法解析之外,什么也没有影响.但这只是允许不同方法的相同名称.术语超载"非常令人困惑,因为没有什么是真正的超载".实际上,这是唯一的问题.

—SA
What would be a reason for doubt? Let me explain it in a bit more general way.

"Overloading" technologically means nothing and effects nothing except naming and hence method resolution. This is nothing but allowed identical names of the different methods. The term "overloading" it very confusing, because nothing is really "loaded". That''s the only problem, in fact.

—SA


这篇关于是否可以在继承中进行方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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