当子类不使用抽象类中的函数时,是否有使用 NotImplementedException 的替代方法?

Mic*_*wid 1 c# oop notimplementedexception

我正在制作一个简单的网络表单来收集客户数据并将其输入到数据库中。我有5个亚类:Customer,Bank,Employee,Owner,和TradeReference它继承了抽象类,DataEntry。 DataEntry有一个功能public void InsertSelfIntoDataBase(int id);。id参数是来自Customers表的主键(Bank、Employee、Owner和TradeReference与Customer是多对一的关系),所以Customer不需要插入id(CustomerID在数据库中自动递增) )。

目前,我的代码设置,这样Bank,Employee,Owner,并TradeReference实现了InsertSelfIntoDataBase在父类的功能,同时Customer抛出一个NotImplementedException,因此对于代码Customer类代码看起来有点像这样:

public int InsertSelfIntoDataBase()
{
    int customerID = InsertCustomerAndReturnScalor();
    return customerID;
}


public override void insertSelfIntoDataBase(int id)
{    throw new NotImplementedException("Customer does not use this function");    }
Run Code Online (Sandbox Code Playgroud)

这个实现有效,但让我感到困扰的是我必须使用 NotImplementedException;就像我无法摆脱这种感觉,我的大学教授不知何故知道并默默地评判我。有没有更好的方法来做到这一点?

Ral*_*ing 5

不管罗伯特哥伦比亚指出的关于类设计的警告,我想谈谈我对NotImplementedException.

在 .NET Framework 中有另一个众所周知的 Exception,它更适合这个目的 - NotSupportedException. 它表示实现不支持操作——而是设计而不是缺少实现功能的代码。

该NotImplementedException是相当的indidicator,是一个实现将,应该在未来完成。