C#中某些多态行为的解释(Explanation of a certain polymorphic behavior in C#)

编程入门 行业动态 更新时间:2024-10-28 01:12:06
C#中某些多态行为的解释(Explanation of a certain polymorphic behavior in C#)

我发现可以创建子类的实例并将其分配给父类的变量。 我无法绕过这种情况。

在我的代码中,尽管实例是Human类。 它在控制台中记录“我是动物”

我有几个问题:

它甚至意味着什么?

可能的结果是什么?

为什么以及何时会有人做这种事情?

家长班:

public class Species { public String Shout() { return "I am an animal"; } }

儿童班:

public class Human : Species { public new String Shout() { return "I am a Human"; } }

实例:

class Program { static void Main(string[] args) { Species h = new Human(); Console.WriteLine(h.Shout()); } }

I've found that it is possible to create an instance of a child class and assign it to a variable of type parent class. I cannot wrap my head around this scenario.

Here in my code, though the instance is of Human class. It logs "i am an Animal" in the console

I have a few question regarding this:

What does it even mean?

What is the possible outcome?

Why & when would someone do this kind of stuff?

Parent class:

public class Species { public String Shout() { return "I am an animal"; } }

Child class:

public class Human : Species { public new String Shout() { return "I am a Human"; } }

Instantiation:

class Program { static void Main(string[] args) { Species h = new Human(); Console.WriteLine(h.Shout()); } }

最满意答案

如果要有效地使用面向对象编程或OOP,多态性非常有用。

假设你拥有一个动物园。 在这个动物园里面(显然)有一些动物。 如果您希望将它们全部存储在一个数组或列表中,则必须为它们使用基类型 ,在本例中为Animal 。 现在,从我们的基类型Animal派生的类共享相同的属性,因此也使用相同的方法。

class Animal { public string Shout () { return "I am an animal!"; } }

现在让我们为狮子宣布一个班级。

class Lion : Animal { }

虽然我们没有在Lion类中编写Shout方法,但Lion实例可以使用Shout方法,因为它们来自Animal类。

但是,如果我们希望Lion类能够Shout不同的东西呢? 我们可以使用virtual和override关键字。 这些可用于属性和方法。 virtual表示可以覆盖方法或属性,而override执行实际覆盖。 两者都需要为这个工作设置正确的位置。

要覆盖我们的Shout方法,我们必须首先将其标记为virtual 。

public virtual string Shout () { ... }

现在,在我们的Lion类中,我们可以对Shout方法执行覆盖。

public override string Shout () { return "Rooooaaar, I am a lion!"; }

现在我们澄清了这一点,让我们来看看它的一些用法。


Lion lion1 = new Lion();

这将通过调用类的构造函数来创建Lion类的新实例。 如果我们现在打电话给lion1.Shout() ,我们会得到"Rooooaaar, I am a lion!" 作为返回值。

Animal animal1 = new Lion();

这也有效! 由于我们的Lion类派生自Animal类型,因此该对象可以存储在Animal类型的对象中。 但不要被愚弄! 如果我们现在调用animal1.Shout() ,我们仍会得到"Rooooaaar, I am a lion!" 回。

Animal animal2 = new Animal();

这只是基类的一个实例,这意味着方法animal2.Shout()会返回"I am an animal!" 。


我们现在说我们有更多的动物, Cat , Dog和Lion 。 不使用多态,我们无法将它们存储在单个数组中,因为它们没有任何共同之处! 但是,如果它们派生自Animal类,我们可以这样做:

List<Animal> animals = new List<Animal>(); animals.Add(new Cat()); animals.Add(new Dog()); animals.Add(new Lion()); animals.Add(new Animal());

以上所有这些都可以完美地运行!

最后,如果你想从这个Animal列表中检索一个元素,并且你想在它上面调用一些Dog -specific方法,你可以(如果它实际上是一个Dog对象)当然使用animals[1] as Dog来转换它一个Dog对象。 但是,如果它不是Dog对象,则只需从as运算符返回null 。

Polymorphism is very useful if you want to efficiently use Object Oriented Programming or OOP.

Let's say that you own a zoo. Inside this zoo there are (obviously) some animals. If you would want to store them all inside one array or list, you would have to use a base type for them, in this case Animal. Now, classes that derive from our base type Animal share the same properties, therefore also the same methods.

class Animal { public string Shout () { return "I am an animal!"; } }

Let's now declare a class for a lion.

class Lion : Animal { }

Even though we haven't written the Shout method inside the Lion class, instances of Lion will also have the Shout method available to them, since they derive from the Animal class.

But what if we want the Lion class to Shout something different? We can make use of the virtual and override keywords. These can be used on properties and methods. virtual means the method or property can be overridden, while override performs the actual override. Both need to be set the right place for this to work.

To override our Shout method, we will have to mark it as virtual first.

public virtual string Shout () { ... }

Now, in our Lion class, we can perform an override on the Shout method.

public override string Shout () { return "Rooooaaar, I am a lion!"; }

Now that we clarified this, let's look at some usage of it.


Lion lion1 = new Lion();

This creates a new instance of the Lion class by calling the class' constructor. If we call lion1.Shout() now, we would get "Rooooaaar, I am a lion!" as the returned value.

Animal animal1 = new Lion();

This works as well! Since our Lion class derives from the base type Animal, the object can be stored inside an object of the type Animal. But don't be fooled! If we call animal1.Shout() now, we would still get "Rooooaaar, I am a lion!" returned.

Animal animal2 = new Animal();

This would just be an instance of the base class, meaning that the method animal2.Shout() would return "I am an animal!".


Let's now say that we have some more animals, Cat, Dog, and Lion. Without using polymorphism, we couldn't store them inside a single array, since they have nothing in common! However, if they do derive from the Animal class, we can just do something like this:

List<Animal> animals = new List<Animal>(); animals.Add(new Cat()); animals.Add(new Dog()); animals.Add(new Lion()); animals.Add(new Animal());

All of these above would work perfectly fine!

Lastly, if you want to retrieve an element from this Animal list and you want to call some Dog-specific methods on it, you can (if it is actually a Dog object, of course) use animals[1] as Dog to convert it to a Dog object. However, if it isn't a Dog object, you would just get null back from the as operator.

更多推荐

本文发布于:2023-07-29 18:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多态   Explanation   behavior   polymorphic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!