类的简化是相似的

时间:2019-07-14 08:06:27

标签: c# class inheritance return

我的班级为MainModel,内部类为MainModelBaseContractModelWorkerModel。最后两个基本相同。它们在执行两种方法时的区别以及许多方法的返回值。

DeleteContract()DeleteWorker()是不同的代码(LoadPage()也是)。但是ReloadPage()FirstPage()等-是相同的。返回值的差异。

我可以将此移植代码简化为MainModelBase吗?有道理吗?

整个代码需要300多行。所以这里的结构。完整代码here

public class ContractModel : MainModelBase
{
    public ContractModel()


    internal bool DeleteContract(int id)

    internal List<ContractTableRow> ReloadPage()
    internal List<ContractTableRow> FirstPage()
    internal List<ContractTableRow> LastPage()
    internal List<ContractTableRow> NextPage()
    internal List<ContractTableRow> PreviousPage()
    internal List<ContractTableRow> CustomPage(int customPage)

    private List<ContractTableRow> LoadPage()
}

public class WorkerModel : MainModelBase
{
    public WorkerModel()

    internal bool DeleteWorker(int id)

    internal List<WorkerTableRow> ReloadPage()
    internal List<WorkerTableRow> FirstPage()
    internal List<WorkerTableRow> LastPage()
    internal List<WorkerTableRow> NextPage()
    internal List<WorkerTableRow> PreviousPage()
    internal List<WorkerTableRow> CustomPage(int customPage)

    private List<WorkerTableRow> LoadPage()
}

public class MainModelBase
{
    private int count;

    internal int MaxPage 
    internal int MinPage 
    internal int Count 
    internal int Size 
    internal int Page 
    internal string NumPageText 


    internal void Added()

    private int GetMaxPage()
}

更新

方法CustomPage()调用LoadPage(),但对于每个类(Delete()也不同)。

这样做正确吗?

public class ContractModel : PagedModelBase<ContractTableRow>
{
    public ContractModel()

    internal override bool Delete(int id)
    internal override List<ContractTableRow> LoadPage()
}

public class WorkerModel : PagedModelBase<WorkerTableRow>
{
    public WorkerModel()

    internal override bool Delete(int id)
    internal override List<WorkerTableRow> LoadPage()

}

public abstract class PagedModelBase<T>
{
    internal void Added()
    internal abstract bool Delete(int id);

    internal List<T> ReloadPage()
    internal List<T> FirstPage()
    internal List<T> LastPage()
    internal List<T> NextPage()
    internal List<T> PreviousPage()
    internal List<T> CustomPage(int customPage)

    internal abstract List<T> LoadPage();

    private int GetMaxPage()
}

1 个答案:

答案 0 :(得分:1)

似乎是使用泛型的不错选择:

public class PagedModelBase<T> : MainModelBase
{
    internal bool Delete(int id)

    internal List<T> ReloadPage()
    internal List<T> FirstPage()
    internal List<T> LastPage()
    internal List<T> NextPage()
    internal List<T> PreviousPage()
    internal List<T> CustomPage(int customPage)

    private List<T> LoadPage()
}

public class ContractModel : PagedModelBase<ContractTableRow> 
{
    // contract specific implementation here
}

public class WorkerModel : PagedModelBase<WorkerTableRow>
{
    // worker specific implementation here
}