开发者

Conditionally instantiate a class in C#

开发者 https://www.devze.com 2023-03-19 19:53 出处:网络
I\'ve been busting my hump trying to get a 开发者_如何学Cnew code to work. I have been able to do this in a simpler but more extensive way, through if statements. However, as this is going to be versi

I've been busting my hump trying to get a 开发者_如何学Cnew code to work. I have been able to do this in a simpler but more extensive way, through if statements. 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.

What I am trying to do is to conditionally instantiate a class. It would go like this:

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. 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.

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. 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. 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?

Make an interface (IAircraft) and have them both implement it.

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. 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.


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. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消