【发布时间】:2026-02-15 17:35:01
【问题描述】:
这是一个可以接受的设计吗??
抽象类
public abstract class SomethingBase
{
public abstract int Method1(int number1);
public abstract int Method2(int number2);
}
public class Class1 : SomethingBase
{
public override int Method1(int number1)
{
//implementation
}
//i dont want this method in this class
public override int Method2(int number2)
{
throw new NotImplementedException();
}
}
public class Class2 : SomethingBase
{
//i dont want this method in this class
public override int Method1(int number1)
{
throw new NotImplementedException();
}
public override int Method2(int number2)
{
//implementation
}
}
我的意思是我的 Class1 中需要 method1 而不需要 method2 而 Class2 的情况。事实上,这些方法在派生类中相互排斥。
【问题讨论】:
-
NotSupportedException或InvalidOperationException在这种情况下更有意义。
标签: c# oop abstract-class