我正在制作一个简单的网络表单来收集客户数据并将其输入到数据库中。我有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;就像我无法摆脱这种感觉,我的大学教授不知何故知道并默默地评判我。有没有更好的方法来做到这一点?