简体   繁体   English

C# - 继承,覆盖

[英]C# - Inheritance, override

Can I do something like this? 我可以这样做吗? (Over-simplified because it's too complex) (过度简化,因为它太复杂了)

abstract class A {
  public string Print() {}

  public static string DoPrint(A a, Type T) {
    ((T)a).Print(); // <-- This should call eg. A3.Print(), not A.Print()
  }
}

class A1: A {
  public new string Print() {}
}

class A2: A {

}

class A3: A {
  public new string Print() {}
}

class Somewhere
{
  A3 a3 = new A3();
  a3.DoPrint();
}

I have many classes (A1, A2, A3, and so on) that inherits from a base class (A) 我有许多继承自基类的类(A1,A2,A3等)(A)

I am trying to create the DoPrint() function above in class A. Can it be done? 我正在尝试在A类中创建上面的DoPrint()函数。可以这样做吗?

I tried this 我试过这个

  public static string DoPrint(A a) {
    a.Print();
  }

but it calls A.Print(), not A3.Print() 但它调用A.Print(),而不是A3.Print()

EDIT: Changed the title from "Pass a type as parameter?" 编辑:更改标题“将类型作为参数传递?” because everyone was right, I could just use virtual (thank you!). 因为每个人都是对的,我可以使用虚拟(谢谢!)。 The problem lied somewhere else, irrelevant to this question. 这个问题在其他地方撒谎,与这个问题无关。

public static string DoPrint(A a) {
  a.Print();
}

would work, but you need to declare Print as virtual and override it. 会工作,但你需要将Print声明为虚拟并覆盖它。

    abstract class A {
      public virtual string Print() {}

      public static string DoPrint(A a) {
          a.Print();
      }
    }

    class A1: A {
      public override string Print() {}
    }

    class A2: A {
      public override string Print() {}
    }

    class A3: A {
      public override string Print() {}
    }

    class Somewhere
    {
      A3 a3 = new A3();
      A.DoPrint(a3);
    }

Instead of using new, mark the base class' Print method as virtual, and then use override in the subclasses method declarations. 而不是使用new,将基类'Print方法标记为虚拟,然后在子类方法声明中使用override。

public virtual string Print() { }

And in subclasses: 在子类中:

public override string Print() { }

Basically, no. 基本上没有。 You can't. 你不能。 Unless you go to the level of reflection and find and invoke the Print method defined on T via that. 除非你进入反射级别并通过它找到并调用T上定义的Print方法。 Anything like interfaces or dynamic would select the top-most implementation. 任何类似接口或dynamic东西都会选择最顶层的实现。

However, I wonder if Print() here should be virtual : 但是,我想知道这里的Print()是否应该是virtual

abstract class A {
  public abstract string Print();
}

class A1: A {
  public override string Print() {}
}

class A2: A {
  public override string Print() {}
}

class A3: A {
  public override string Print() {}
}

and just call a.Print(); 然后调用a.Print(); . Polymorphism will then call the most-derived override of Print() . 然后,多态性将调用Print()的派生最多的覆盖。

What's wrong with normal virtual methods and inheritance?? 普通的虚方法和继承有什么问题? Look at the use of virtual and override: 看看虚拟和覆盖的使用:

abstract class A { 
 public virtual string Print() {} 

  public static string DoPrint(A a) { 
     a.Print(); // <-- This WILL call eg. A3.Print(), not A.Print() 
  } 
} 

class A1 : A
{
    public override string Print() {}
}

class A2 : A
{
    public override string Print() {}
}
class A3 : A
{
    public override string Print() {}
}

DoPrint() is a static function. DoPrint()是一个静态函数。

You should call it this way: 你应该这样称呼它:

A3.DoPrint(a3, typeof(A3));

or 要么

A3.DoPrint(a3, a3.GetType());

Now, I don't understand why you want to do that. 现在,我不明白你为什么要那样做。 Why not simply have a virtual Print() in class A and override it in derived classes ? 为什么不在A类中简单地使用虚拟Print()并在派生类中覆盖它?

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

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