多态如何为方法返回类型工作? [英] How does polymorphism work for method return types?

查看:82
本文介绍了多态如何为方法返回类型工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解多态性是如何工作的。我理解隐式和显式转换如何为引用类型工作但我无法理解返回父类型工作的继承方法如何工作



例如:

  class  SimpleClass 
{
public SimpleClass ShallowCopy (SimpleClass sc)
{
return sc.MemberwiseClone();
}
}





此代码不起作用。我需要为返回的值提供显式强制转换。但它是如何工作的? System.Object中的方法MemberwiseClone()返回一个对象。所以当它返回对象时,它的类型应该只是对象?对?那为什么我能够将返回的值显式地转换为SimpleClass?这是如何运作的?有人可以向我解释这个行为吗?



我尝试了什么:



抓我的头。研究Microsoft Docs中的更多信息,但没有解释那里的内容。

解决方案

假设你有三个类:

< pre lang =c#> class A {}
class B:A { public string S { get ; set ; }
class C:B { public string S2 { get ; set ; }}



所以C来自B,它是从A派生的。

这意味着C& B的实例B是A加一点的实例,并且C实例也是B的实例。但是A不是B或C,B不是C.

 A  - > B  - > C 

因此很明显,您可以将B或C的实例分配给A类变量:

 A a = new B() ; 
a = new C();

但你不能这样反过来:

 B b = new C() ; //很好
b = new A(); //错误!

如果可以,那么当您尝试访问 bS 时会出现问题,因为A的实例不包含变量好吧。



有了 - 希望 - 清楚,让我们来看看你在做什么。

 return sc .MemberwiseClone(); 

MemberwiseClone返回对象,因为这是 所有的基类 - 所有其他类隐式派生自object。因此,当你返回一个SimpleClass时,你必须将该对象强制转换为你的类或者我可以这样做:

 string LetSlipTheDogsOfWar(int i)
{
对象o = i; //完全合法,int派生自对象
return o;
}
...
string s = LetSlipTheDogsOfWar(666); // Cry Havoc!



系统正确地抱怨你正在做一些非常危险的事情!


I understand how polymorphism works. I understand how implicit and explicit castings work for reference types but I cannot understand how inherited methods that return parent's type work

For example:

class SimpleClass
    {
        public SimpleClass ShallowCopy(SimpleClass sc)
        {
            return sc.MemberwiseClone();
        }
    }



This code won't work. I need to supply an explicit cast to the value being returned. But how does it work? The method "MemberwiseClone()" from the System.Object returns an object. So when it returns the object it's type should be only object? right? Then why am I able to explicitly cast the returned value to SimpleClass? How does this work? Could anybody explain this behavior to me?

What I have tried:

Scratching my head. Researching for more information in Microsoft Docs but there isn't anything like that explained there.

解决方案

Suppose you have three classes:

class A {}
class B : A { public string S { get; set ; } }
class C : B { public string S2 { get; set ; } }


So C is derived from B which is derived from A.
That means that instances of both C & B are instances of A plus a bit, and that a C instance is also an instance of B. But that A is not a B or a C, and B is not a C.

A -> B -> C

So it's pretty obvious that you can assign an instance of B or C to a variable of type A:

A a = new B();
a = new C();

But you can't so it the other way around:

B b = new C(); // Fine.
b = new A();   // ERROR!

If you could, then you'd have problems when you tried to access b.S because an instance of A does not contain the variable at all.

With that - hopefully - clear, let's look at what you are doing.

return sc.MemberwiseClone();

MemberwiseClone returns an object because that is the base class for everything - all other classes implicitly derive from object. So when you return a SimpleClass you have to cast that object to your class or I could do this:

string LetSlipTheDogsOfWar(int i)
   {
   object o = i;  // perfectly legal, int is derived from object
   return o;
   }
...
string s = LetSlipTheDogsOfWar(666);  // Cry Havoc!


The system rightly complains that you are doing something very dangerous!


这篇关于多态如何为方法返回类型工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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