C#多态性问题 [英] C# Polymorphism Question

查看:117
本文介绍了C#多态性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的朋友们。我在采访中遇到过这个问题。这是:



Hello my friends. I've encountered this question in a interview. Here it is:

public Class A{
  public void Print()
  {
     Console.WriteLine("Hello From A");
  }
}

public Class B : A{
  public void Print()
  {
     Console.WriteLine("Hello From B");
  }
}

public Class C : A{
  public void Print()
  {
     Console.WriteLine("Hello From C");
  }
}





现在在Main()中。我有:



Now in the Main(). I have the :

A objA;





问题是:如何使用objA来调用C类中的Print()?

注意:必须打印HELLO from C这一行!!

你能解释一下吗?它给我?



任何帮助都会有所体现:_)



The question is : How to use objA to call the Print() in the class C ?
NOTE THAT : THE LINE "HELLO FROM C" MUST BE PRINTED!!
And can you explain it to me?

Any helps would be appreciate :_)

推荐答案

你必须创造(或使用)C类型的对象;

You have to create (or use) an object of type C;
A objA = new C();
 objA.Print();





A是C类的基类,在C类中你重写打印因此,当使用类型C的对象时,即使将对象声明为类型A(基类),也将使用C类的方法。类似的可能是其他兄弟班:B。



您可以在下一个 MSDN链接 [ ^ ]



因此,您可以从此链接提供的说明和示例中看到,您必须标记您的从基类打印方法为 virtual ,然后在派生类(B和C)中使用覆盖



The A is the base class for C class and in C class you override the Print method, so when an object of type C is used, the method of C class will be used even the object was declared as type A (base class). Similar could be for the other sibling class: B.

You can find more detailed explanation and examples in the next MSDN link[^]

So as you can see from the explanation and examples provided at this link, you have to mark your Print method from the base class as virtual, then in the derived classes (B and C) with override .


如果源代码是由测试人员给出的,并且假设您不允许修改给定的类,则解决方案是下一个一个:



If the source code was given by your tester, and supposing that you are not allowed to modify the given classes, the solution is the next one:

namespace ConsoleApplication1
{
    public class A
    {
        public void Print()
        {
            Console.WriteLine("Hello From A");
        }
    }

    public class B : A
    {
        public void Print()
        {
            Console.WriteLine("Hello From B");
        }
    }

    public class C : A
    {
        public void Print()
        {
            Console.WriteLine("Hello From C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A test = new C();
            ((C)test).Print();
        }
    }
}





因此测试对象是C类型,它有两个 Print()方法(一个继承自其基类),如果你想访问它自己的 Print()方法您必须先使用显式强制转换才能调用它。



So test object is of type C and it has two Print() method (one inherited from its base class), and if you want to access its own Print() method you must use explicit cast before to invoke it.


让我们考虑修改后的代码:

Let's consider modified code:
public Class A {
  public virtual void Print() { Console.WriteLine("Hello From A"); }
}

public Class C : A{
  public override void Print() { Console.WriteLine("Hello From C"); }
}

// ...

A someObject = new C();
someObject.Print();





可选, A.Print 可以是 abstract 。然后,只有这样你才能得到你想要的东西。



这个机制是OOP的核心,被称为后期绑定。这还不是多态,当你的基类的编译时类型知道一整套对象时,会发生这种情况,运行时 i> type是不同的(因此 poly ),然后它们可以在共同的基础上处理,只能通过该集合的基本类型提供的接口

,而不需要知道运行时类型。



有关OOP核心机制的一些解释,请参阅我过去的答案:我无法弄清楚这部分C#代码 [ ^ ] 。



-SA



Optionally, A.Print could be abstract. Then and only then you will get what you want.

This mechanism is the heart of OOP and is called late binding. This is not yet polymorphism, which takes place when you have a whole set of object known by their compile-time type of the base class, and runtime type are different (hence poly), then they can be processed on the common basis, only through the interface
provided by a base type of the set, without knowing of the runtime types.

For some explanation of this core mechanism of OOP, please see my past answer: I couldn't figure out this portion of C# code[^].

—SA


这篇关于C#多态性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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