简体   繁体   English

如何实例化基类,然后将其转换为派生类?

[英]How can I instantiate a base class and then convert it to a derived class?

I was wondering how to do this, consider the following classes 我想知道如何做到这一点,考虑以下类

public class Fruit
{
    public string Name { get; set; }
    public Color Color { get; set; }
}
public class Apple : Fruit
{
    public Apple()
    {

    }
}

How can I instantiate a new fruit but upcast to Apple, is there a way to instantiate a bunch of Fruit and make them apples with the name & color set. 我如何实例化一个新的水果,但转发到苹果,有没有办法实例化一堆水果,并使他们苹果与名称和颜色设置。 Do I need to manually deep copy? 我需要手动深层复制吗?

Of course this fails 当然这失败了

Fruit a = new Fruit();
a.Name = "FirstApple";
a.Color = Color.Red;
Apple wa = a as Apple;
System.Diagnostics.Debug.Print("Apple name: " + wa.Name);

Do I need to pass in a Fruit to the AppleCTor and manually set the name and color( or 1-n properties) Is there an better design to do this? 我是否需要将Fruit传递给AppleCTor并手动设置名称和颜色(或1-n属性)是否有更好的设计来执行此操作?

By default, the a instance is an instance of Fruit and is not convertible to Apple . 默认情况下, a实例是Fruit一个实例,不能转换为Apple It's not an Apple instance, and will never be. 不是 Apple实例,永远不会。

You can, however, implement an explicit cast . 但是,您可以实现显式强制转换

In your model, a Fruit is not an Apple - if you want an Apple , instantiate one. 在你的模型中, Fruit不是Apple - 如果你想要Apple ,请实例化它。 Alternatively, if you want to create an Apple using a Fruit as a starting point, create a constructor for Apple that can accept a Fruit . 另外,如果你想创建一个AppleFruit作为一个起点,创建一个构造函数Apple是可以接受Fruit

You should note, that object models where intermediate classes in the inheritance hierarchy are instantiable (not abstract) is often a bad idea. 您应该注意,继承层次结构中的中间类可实例化(非抽象)的对象模型通常是个坏主意。 In some cases, such hierarchies may be appropriate - but I've found that in general they are more trouble than they are worth. 在某些情况下,这种等级可能是合适的 - 但我发现一般来说,它们比它们的价值更麻烦。 Think really hard about whether both Fruit and Apple need to be concrete and instantiable. 想想Fruit Apple是否需要具体和可实现的,真的很难。

It looks like you're trying to come up with a way to instantiate one of many possible objects derived from a common base class. 看起来您正试图想出一种方法来实例化从公共基类派生的许多可能对象中的一个。 Typically this is done through the Abstract Factory pattern. 通常,这是通过抽象工厂模式完成的。 For more details and implementation examples see: http://en.wikipedia.org/wiki/Abstract_factory_pattern 有关更多详细信息和实现示例,请参阅: http//en.wikipedia.org/wiki/Abstract_factory_pattern

If you create a Fruit, it isn't an Apple. 如果你创建一个Fruit,它就不是Apple。 You can't upcast it, because it probably won't have all of the attributes that it'll need. 你无法向上转播它,因为它可能不具备它所需要的所有属性。 If you want an Apple, create an Apple. 如果你想要Apple,请创建一个Apple。 You don't need to pass anything to anything. 你不需要传递任何东西。

I suspect the following will work: 我怀疑以下内容会起作用:

Fruit a = new Apple();
System.Diagnostics.Debug.Print("Apple name: " + a.Name);

...but I'm not a Java programmer. ...但我不是Java程序员。 That's basically what you'd do in C++ though. 这基本上就是你在C ++中所做的。 (Be sure to read the Abstract Factory Pattern link given in another answer, especially if you need to instantiate a bunch of different subtypes of Fruit.) (请务必阅读另一个答案中给出的Abstract Factory Pattern链接,特别是如果您需要实例化一堆不同的Fruit子类型。)

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

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