简体   繁体   English

C#中对象实例化左侧的抽象类

[英]Abstract class on left side of object instantiation in C#

I have two questions regarding the use of an abstract class on left side of object instantiation. 关于对象实例左侧的抽象类的使用,我有两个问题。

AbstractClass MyClass = new ConcreteClass();

1 - What is this called? 1-这叫什么? (when you declare the abstract class on the left.) (当您在左侧声明抽象类时。)

eg 例如

Car MyNiceCar = new NiceCar();

I know this relates to Polymorphism, but I'm specifically asking how to describe/verbalize the scenario when declare the abstract class on the left. 我知道这与多态性有关,但是我要特别问的是,在左侧声明抽象类时如何描述/语言化场景。

2 - And why do it? 2-为什么呢? ie Why would you do: 即你为什么要做:

Car MyNiceCar = new NiceCar();

And not: 并不是:

NiceCar MyNiceCar = new NiceCar();

?

Would the answer to question 2 possibly be so that i can do the following? 问题2的答案可能是这样,以便我可以执行以下操作?

Car MyNiceCar = new NiceCar();

.
. //do some logic to decide if I can have a nicer car.
.

MyNiceCar = new EvenNicerCar();

1) You're creating a base class reference to your Derived class. 1)您正在创建对派生类的基类引用。
Edit: 编辑:
Other words for BaseClass: SuperClass, ParentClass BaseClass的其他词语:SuperClass,ParentClass
Other words for DerivedClass: SubClass, ChildClass Don't ask why there are so many words for each. DerivedClass的其他词语:SubClass,ChildClass不要问为什么每个词语有这么多词语。 It's kinda a Spaces vs Tabs type thing. 这有点像Spaces vs Tabs类型的东西。

2) You do it so that you can use virtual functions/properties that you know all the derived classes will have. 2)您这样做,以便可以使用所有派生类都知道的virtual函数/属性。 You want to do something that a Car can do, and you don't care if its a CrapCar , NiceCar or SuperNiceCar 您想做一辆汽车可以做的事情,并且不在乎它是CrapCarNiceCar还是SuperNiceCar

Car car = new MyNiceCar();
car.honk(); //meep!
car = new SuperNiceCar();
car.honk(); //beep beep!

However, you can't go the other way around. 但是,您不能反其道而行之。

SuperNiceCar may support UseTurbo() , but MyNiceCar does not. SuperNiceCar可能支持UseTurbo() ,但MyNiceCar不支持。

But you don't care, you just want the car to honk, so you cast it as a Car , because you know all Car s can honk. 但您不在乎,您只想将汽车鸣喇叭,因此就将其投射为Car ,因为您知道所有Car都可以鸣喇叭。

See also 也可以看看

1 - What is this called? 1-这叫什么?

This is a type of polymorphism that's called "inclusion polymorphism". 这是一种多态性,称为“包含多态性”。 It allows you to create restrictions on the type of variable that can be used in your code, while still allowing some type flexibility. 它允许您对可以在代码中使用的变量的类型进行限制,同时仍然允许某些类型的灵活性。 Java and C#, for instance, have generics that allow various types and references to use the same code. 例如,Java和C#具有允许各种类型和引用使用同一代码的泛型。 However, these often run into run-time issues that could otherwise be caught by a static code analyzer. 但是,这些通常会遇到运行时问题,否则静态代码分析器可能会捕获这些问题。 It's generally considered to be less risky to rely on the static analyzer (assuming your IDE has a good one) than to wait and find out your code has bugs in it after your release. 通常认为,使用静态分析器(假设您的IDE很好)比发布后等待发现代码中的错误的风险要小。

2 - And Why do it? 2-为什么呢?

In addition to the reason I gave above, one of the most common applications of this is the polymorphic array. 除了上面给出的原因外,多态数组是最常见的应用之一。

Consider the following: 考虑以下:

// Both classes contain an Accelerate() function
NiceCar MyNiceCar = new NiceCar();
EvenNicerCar MyEvenNicerCar = new EvenNicerCar();

// We're forced to use these methods separately
MyNiceCar.Accelerate();
MyEvenNicerCar.Accelerate();

////////////////////////////////////////////////////////////////////////////////////////////////////

// We can make this code much smaller and less prone to copy/paste errors using polymorphism!
Car[] MyPolymorphicArray = new Car[] { new NiceCar(), new EvenNicerCar() };
foreach(c in MyPolymorphicArray) { c.Accelerate(); }

Using this type of syntax, you can see that I'm able to make the code smaller, more-manageable, type-safe, and our static code analyzer will complain if we get it wrong, rather than waiting until runtime to find out. 使用这种类型的语法,您可以看到我能够使代码更小,更易于管理,类型安全,并且如果我们弄错了代码,我们的静态代码分析器将抱怨,而不是等到运行时才查找出来。 When you use this type of architecture, make sure you require the methods intended to be used by the instances to either be in an interface or in the parent class of that object in order to help avoid implementation errors. 使用这种类型的体系结构时,请确保要求实例打算使用的方法位于该对象的接口或父类中,以帮助避免实现错误。 Additionally, take note that if you do this, the Accelerate() method may only need to be written in the parent class, which helps make the code even smaller. 此外,请注意,如果执行此操作,则Accelerate()方法可能只需要在父类中编写,这有助于使代码更小。

Following on from two excellent answers from @Mars and @chris-mauer which both answered Question 2 of my post perfectly. 接下来是来自@Mars和@ chris-mauer的两个出色答案,它们都完美回答了我帖子的问题2。 And allowed me to study this concept further. 并允许我进一步研究这个概念。

I want to include that the answer to Question 1 of my post as follows. 我想将我的帖子的问题1的答案包括在内。 I asked: 我问:

AbstractClass MyClass = new ConcreteClass();

1 - What is this called? 1-这叫什么? (when you declare the abstract class on the left.) (当您在左侧声明抽象类时。)

From the article below, I believe the correct answer is: 从下面的文章中,我相信正确的答案是:

Upcasting 上流

For more on the overall concept, which is Inclusion Polymorphism, I studied the following article: http://www.msdotnet.co.in/2014/05/how-to-implement-polymorphism-concepts.html 有关整体概念(包含多态)的更多信息,我研究了以下文章: http : //www.msdotnet.co.in/2014/05/how-to-implement-polymorphism-concepts.html

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

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