简体   繁体   English

有条件地在 C# 中实例化一个 class

[英]Conditionally instantiate a class in C#

I've been busting my hump trying to get a new code to work.我一直在努力让新代码正常工作。 I have been able to do this in a simpler but more extensive way, through if statements.通过 if 语句,我能够以更简单但更广泛的方式做到这一点。 However, as this is going to be version 2 of the application, I would like to get the code a little cleaner and more efficient.但是,由于这将是应用程序的第 2 版,我想让代码更简洁、更高效。

What I am trying to do is to conditionally instantiate a class.我要做的是有条件地实例化一个 class。 It would go like this:它会像这样 go :

int Airplane = 195;

if (Airplane == 190)
    _E190 Aircraft = new _E190();

else if (Airplane == 195)
    _E195 Aircraft = new _E195();

I have both classes, but I would like to use the keyword "Aircraft" so I don't have to create multiple parts of code for each class.我有两个类,但我想使用关键字“飞机”,所以我不必为每个 class 创建多个代码部分。 I have also found out that multiple inheritance (which, in my opinion, would do the trick) is not available in C#, and I am relatively new to this language.我还发现 C# 中没有多个 inheritance(在我看来可以解决问题),而且我对这种语言比较陌生。

So far, I have accomplished to instantiate the class the way I want, but I can't seem to use the variables declared inside each class.到目前为止,我已经按照我想要的方式实例化了 class,但我似乎无法使用每个 class 中声明的变量。 I have done this:我已经这样做了:

    int Airplane = 195;

    var Aircraft = Activator.CreateInstance( (Airplane == 195) ? typeof(_E195) : typeof(_E190));
    MessageBox.Show(Aircraft.GetType().ToString());

The MessageBox shows the type correctly, but again, I can't access the class itself using the "Aircraft" object. MessageBox 正确显示了类型,但同样,我无法使用“飞机”object 访问 class 本身。 I am certain that there are more ways to do this, but any of them will be just fine for me.我确信有更多方法可以做到这一点,但其中任何一种对我来说都很好。

Thanks!谢谢!

I take it they're each inherited from something else and that's why you need multiple inheritance?我认为它们都是从其他东西继承而来的,这就是为什么你需要多个 inheritance?

Make an interface (IAircraft) and have them both implement it.制作一个接口(IAircraft)并让他们都实现它。

interface IAircraft {
    /* Common methods here */
}

class _E190 : TheParentClass, IAircraft {
    /* Your code */
}

class _E195 : TheOtherParentClass, IAircraft {
    /* Your code */
}

/* ... */

int Airplane = 195;
IAircraft Aircraft;

if (Airplane == 190)
    Aircraft = new _E190();
else if (Airplane == 195)
    Aircraft = new _E195();

The reason you can't access the class using your previous method is because to do that, the compiler needs to know what type of class it is.您无法使用以前的方法访问 class 的原因是,编译器需要知道它是什么类型的 class。 And to do that, you'd need to cast it to the appropriate type... but that brings you right back to the same problem.为此,您需要将其转换为适当的类型......但这会让您回到同样的问题。 You could use reflection methods to continue with that, but it's much more efficient and clean to use inheritance or implementation.您可以使用反射方法继续此操作,但使用 inheritance 或实现会更加高效和简洁。

Why don't you define something like你为什么不定义类似的东西

public abstract class Aircraft
{
}

public class _E190 : Aircraft
{
}


public class _E195 : Aircraft
{
}

and then do the following:然后执行以下操作:

Aircraft Aircraft;
int Airplane = 195;

if (Airplane == 190)
    Aircraft = new _E190();

else if (Airplane == 195)
    Aircraft = new _E195();

EDIT: Additionally, as others propose, you can make use of interface instead of abstract class.编辑:此外,正如其他人建议的那样,您可以使用接口而不是抽象 class。 Note, that in this case you'll have to double-define all common properties and methods, if there are any.请注意,在这种情况下,您必须双重定义所有常见的属性和方法(如果有的话)。

Read up on the Object Factory Pattern阅读 Object 工厂模式

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

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