【问题标题】:Using an Inheriting Methods with a different variable (of a different type)使用具有不同变量(不同类型)的继承方法
【发布时间】:2011-04-07 23:17:59
【问题描述】:

我是 OOP 编程新手,我正在尝试正确使用继承的概念。

这是我尝试过的代码 (Link Here)

public class Base
{
    //Type?
    public abstract static object Adapter();              

    public static DataTable GetWOCost(DateTime Date, string WO)
    {
        Application.UseWaitCursor = true;
        DataTable dt = new DataTable();

        try
        {
            //Cast?
            dt = Adapter().GetDataByWO(WO, Date);
            Application.UseWaitCursor = false;
            return dt;

        } catch (Exception)
        {
            return null;
        }
    }
} 

public class Materiel : Base
    {
        static AllieesDBTableAdapters.CoutMatTableAdapter Adapter()
        {
            return new AllieesDBTableAdapters.CoutMatTableAdapter();        
        }
    }

    public class Labor : Base
    {
        static AllieesDBTableAdapters.CoutLaborTableAdapter Adapter()
        {
            return new AllieesDBTableAdapters.CoutLaborTableAdapter();
        }
    }

起初我的所有代码都在 Material 类中。但是我不得不添加第二个相同的类,但使用不同的 SQL 适配器。我尝试了不同的东西,但上面的代码有一个大问题。

由于类型正在改变,我使用了对象,但如果没有强制转换,它将无法工作。但是由于我不知道它将是什么类型,拥有 2 个或多个具有 GetWOCost 方法但具有不同适配器的类的正确方法是什么?

也许我应该更改为 .NET 4.0 并使用动态对象?

编辑:abstract 和 static 似乎也有问题,所以如果没有 Adapter() 的实例(在基类中),我不能在方法 GetWOCost() 上使用 static 修饰符。似乎只复制粘贴会更容易,但我正在尝试找出正确的方法。

【问题讨论】:

  • 你绝对不应该在这个任务中使用动态。这不是它的设计目的。

标签: inheritance c#-3.0


【解决方案1】:

在这种情况下,您应该做的是针对 interface 进行编程,该接口定义了 contract 用于您想要对 Adapter 对象执行的操作。如果您的实现类不共享一个公共接口,您可以为它们创建一个。

然后你可以创建这个接口的不同实现;并在每个子类中重写适配器方法以返回正确的接口实现。

你应该摆脱你的静态方法,因为多态类型的整个想法是你可以获得不同的实例来做同样的事情,但以不同的方式。

在代码中,这可能看起来像这样(简化):

public interface IAdapter 
{
    DataTable GetWOCost();  // Implementors must have a method with this signature
}

public class Base 
{
    public abstract IAdapter Adapter();  

    // Methods that use IAdapter instances here.
}

class Materiel : Base 
{
    public override IAdapter Adapter() { return new CoutMatTableAdapter(); } 
}

class Labor : Base 
{
    public override IAdapter Adapter() { return new CoutLaborTableAdapter(); }
}

那么你只需要让你的不同适配器实现你的新接口,例如:

public class CoutLaborTableAdapter : IAdapter 
{
    public DataTable GetWOCost() { /* implementation */ }
}

【讨论】:

  • 我确实考虑过使用接口,只是不确定。问题如下: - 我使用数据集设计器创建我的表适配器,因此当我更新某些内容时,更改任何内容都可能会被破坏。我还有其他用于其他用途的查询(在同一个表中)-您能否更具体地了解 GetWOCost 和 GetDataByWO(GetWOCost 是我的方法,被调用来获取数据,GetDataByWO 是我的 SQL 查询-我将放弃我的静态方法(没什么大不了的)
【解决方案2】:

好的,我确实使您的解决方案有效,GetDataByWO 和 GetWOCost 有点混乱。你没有使用正确的。

    public interface IAdapter
{
    DataTable GetDataByWO(string WO, DateTime Date);   
}

public abstract class Base
{
    public abstract IAdapter Adapter(); 

    public DataTable GetWOCost(DateTime Date, string WO)
    {
        Application.UseWaitCursor = true;
        DataTable dt = new DataTable();

        try
        {
            dt = Adapter().GetDataByWO(WO, Date);
            Application.UseWaitCursor = false;
            return dt;

        } catch (Exception)
        {
           return null;
        }
    }
}

public class Materiel : Base
{
    public override IAdapter Adapter()
    {
        return new CoutMatTableAdapter();        
    }
}

public class Labor : Base
{
    public override IAdapter Adapter()
    {
        return new CoutLaborTableAdapter();
    }
}

public class CoutMatTableAdapter: IAdapter
{
    public DataTable GetDataByWO(string WO, DateTime Date)
    {
        AllieesDBTableAdapters.CoutMatTableAdapter adpt = new AllieesDBTableAdapters.CoutMatTableAdapter();
        return adpt.GetDataByWO(WO, Date);
    }
}

public class CoutLaborTableAdapter : IAdapter
{
    public DataTable GetDataByWO(string WO, DateTime Date)
    {
        AllieesDBTableAdapters.CoutLaborTableAdapter adpt = new AllieesDBTableAdapters.CoutLaborTableAdapter();
        return adpt.GetDataByWO(WO, Date);
    }
}

现在要找出劳动阶级抛出异常的原因(生成的 SQL 部分)。因此,Abstract 和 static 似乎不能很好地结合在一起(我可以理解为什么,但是让我的方法静态会很棒)

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2023-02-09
    相关资源
    最近更新 更多