小编Mic*_*wid的帖子

当子类不使用抽象类中的函数时,是否有使用 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;就像我无法摆脱这种感觉,我的大学教授不知何故知道并默默地评判我。有没有更好的方法来做到这一点?

c# oop notimplementedexception

1
推荐指数
1
解决办法
1033
查看次数

标签 统计

c# ×1

notimplementedexception ×1

oop ×1