开发者

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) 开发者_C百科that inherits from a base class (A)

I am trying to create the DoPrint() function above in class A. Can it be done?

I tried this

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

but it calls A.Print(), not 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.

    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.

public virtual string Print() { }

And in subclasses:

public override string 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() {}
}


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. Anything like interfaces or dynamic would select the top-most implementation.

However, I wonder if Print() here should be 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();. Polymorphism will then call the most-derived override of Print().


DoPrint() is a static function.

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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜