I have two classes inherited from a base class, is it possible to use different enum
on their respective class?
我有两个从基类继承的类,可以在它们各自的类上使用不同的枚举吗?
public abstract class BaseClass
{
public enum BaseEnum {a,b,c}
public void Function1(BaseEnum myEnum)
{
// Something here
}
}
public class DerivedClass1 : BaseClass
{
public enum DerivedEnum {d,e,f}
public void Function1(DerivedEnum myEnum)
{
// Something here
}
}
public class DerivedClass2 : BaseClass
{
public enum DerivedEnum2 {g,h,i}
public void Function1(DerivedEnum2 myEnum)
{
// Something here
}
}
I tried to hide the base enum
where Function1 will perform different operations based on MyEnum, I am not sure am I doing this right. Is this a good way to use different enum
in inherited class?
我试图隐藏基本枚举,其中Function1将根据MyEnum执行不同的操作,我不确定我这样做是正确的。这是在继承类中使用不同枚举的好方法吗?
public abstract class BaseClass
{
public enum MyEnum {a,b,c}
public void Function1(MyEnum myEnum)
{
// Something here
}
}
public class DerivedClass1 : BaseClass
{
public new enum MyEnum {d,e,f}
}
public class DerivedClass2 : BaseClass
{
public new enum MyEnum {g,h,i}
}
更多回答
you can achieve similar functionality by using method overloading.
您可以使用方法重载来实现类似的功能。
优秀答案推荐
You can use generics like this :
您可以使用如下泛型:
public abstract class BaseClass<TEnum> where TEnum : Enum
{
public void Function1(TEnum myEnum)
{
// Something here
}
}
public class DerivedClass1 : BaseClass<DerivedEnum>
{
}
更多回答
我是一名优秀的程序员,十分优秀!