简体   繁体   English

C#抽象类覆盖方法

[英]C# Abstract Class Override method

Using C#.NET4.5 and Visual Studio 2012 Ultimate. 使用C#.NET4.5和Visual Studio 2012 Ultimate。

Im currently trying out absract classes with my Label Printing program, Ive Used Interfaces before. 我目前在我的标签打印程序“我以前使用过的接口”中尝试过抽象类。

I used Interfaces to De-Couple my two classes, it works great. 我使用接口去分离了我的两个班级,它很棒。

Now Im trying out the following. 现在,我正在尝试以下方法。

1st. 1号 My Abstract Class... 我的抽象课...

abstract class Label
{

    public virtual IList<Microsoft.Reporting.WinForms.ReportParameter> NewReportSetup(string part, string batch, string locn, string wheel, string gear, string length,
                                string fits, string newbar, string newbarnum, string abs)
    {
        IList<Microsoft.Reporting.WinForms.ReportParameter> parameters = new List<Microsoft.Reporting.WinForms.ReportParameter>();
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramPart", part));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBatch", batch));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramLocn", locn));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramWheel", wheel));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramGear", gear));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramLength", length));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramABS", abs));

        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBuyer", fits));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBarCode", newbar));
        parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBartxt", newbarnum));

        return parameters;
    }
}

2nd. 2号 My ReportShaft Inherits Label... 我的ReportShaft继承标签...

class ReportShaft : Label
{
    public virtual IList<Microsoft.Reporting.WinForms.ReportParameter> NewReportSetup()
    {         
        return new List<Microsoft.Reporting.WinForms.ReportParameter>();
    }
}

3rd. 第三名 My Form Instantiates the ReportShaft Class and calls the NewReportSetup()... 我的表单实例化ReportShaft类并调用NewReportSetup()...

private void NewReportSetupSHAFT()
{           
    if(txtABS.Text.ToString() == "" || txtABS.Text == null)
    {
        txtABS.Text = "N/A";
    }

    IList<Microsoft.Reporting.WinForms.ReportParameter> param = new List<Microsoft.Reporting.WinForms.ReportParameter>();

    param = reportshaft.NewReportSetup(txtNewPart.Text.ToString(),
        txtBatch.Text.ToString(), txtLocation.Text.ToString(), txtWheel.Text.ToString(), txtGear.Text.ToString(), txtLength.Text.ToString(),
        txtFits.Text.ToString(), txtNewBar.Text.ToString(), txtNewBarNum.Text.ToString(), txtABS.Text.ToString());

    reportViewer1.LocalReport.SetParameters(param);                        
}

This works fine(although I sense I'm using Abstract classes in the wrong way, not sure). 这很好用(尽管我感觉我以错误的方式使用Abstract类,不确定)。

My Question is: 我的问题是:

I want to Create a new Report Class. 我想创建一个新的报告类。 I want the Class to call the very Same method but alow me to Change the Top 2 Parameter names, and skip the last one out completly. 我希望Class调用完全相同的方法,但让我更改前2个参数名称,并完全跳过最后一个

Now would this require an overide to the method? 现在,这是否需要对该方法的补充 if so how would one do this? 如果是这样,怎么办? Will the Label method need changing from a Virtual Function ? Label方法是否需要从虚拟功能更改?

Many Thanks Guys! 非常感谢你们!

UPDATE:: Ok may seem to be some confusion when i mentioned parameters. 更新::好的,当我提到参数时似乎有些混乱。

I meant to say i wish to Call 1 method from my abstract class, then in my report class that inherits this label class and method, i wish to change the "report paramaters" I mean the body of the method. 我的意思是说我希望从抽象类中调用1方法,然后在继承该标签类和方法的报告类中,我希望更改“报告参数”,即方法的主体。

The reason for this is because if I simply make another method and call that for each different report i will be using methods almost identical. 这样做的原因是因为如果我只是简单地创建另一个方法并针对每个不同的报表调用该方法,则我将使用几乎相同的方法。

Heres example:: change this.. 继承人的例子::改变这个。

parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramPart", part));

Too this .. 太..

parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramchanged!!", part));

this is one example. 这是一个例子。 So from what I gather i override my label class method in my report class. 因此,根据我收集的信息,我在报告类中覆盖了我的标签类方法。 But then im stuck, if i try to change the body i must type out the rest of code that goes with it. 但是随后我陷入了困境,如果我尝试更改主体,则必须键入与之配套的其余代码。 to me im still gona end up with a bunch of methods that look identical. 对我来说,即时通讯仍然以一堆看起来相同的方法结束。

Is there anyway to change "partial part of the methods body" without having to type out the rest. 无论如何,是否有必要更改“方法主体的一部分”而不必键入其余部分。

Hope this clears confusion. 希望这能消除混乱。

I am not sure that what you are doing is the best approach. 我不确定您在做什么是最好的方法。 I would refactor the code so that the NewReportSetup takes an object. 我将重构代码,以便NewReportSetup接受一个对象。 Then, you could have the object have properties that you access, and if they are not set, then you dont use them. 然后,您可以让对象具有您要访问的属性,如果未设置它们,则不要使用它们。 You can then create a builder class. 然后,您可以创建一个构建器类。

 NewReportSetup(ReportProperties propertiesObject)
 {
      ...
 }

 public class ReportProperties
 {
     public String Part{get;set;}
     ...
 }

Then, you can create your child classes that use/format the given ReportProperties object as specified by the implementation 然后,您可以创建子类,以使用/格式化实现中指定的ReportProperties对象

Otherwise, a more direct answer is Micah's, which also works. 否则,一个更直接的答案是米卡的,它也有效。 The difference is one of Composition over Inheritance . 区别是组成比继承之一 Inheritance has its place, but should not be overused when composition is the better approach 继承有它的位置,但是当组合是更好的方法时,不应过度使用

For your update 为了您的更新

Here is what it sounds like you can do for your update (however, the ReportProperties object will still work...you can have it contain all of your metadata also) 这听起来像是您可以进行的更新(但是, ReportProperties对象仍然可以使用...您还可以使其包含所有元数据)

Your abstract class: 您的抽象类:

public virtual void NewReportSetup(params)
{
    ...
    parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBatch", batch));
    ...
    FinalizeParameters(parameters, paramsThatAreImplSpecific);
}

protected abstract void FinalizeParameters(List, paramsThatAreImplSpecific);

Your concrete implementation: 您的具体实现:

protected override void FinalizeParameters(List, paramsThatAreImplSpecific)
{
    parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramPart or paramChanged", part));
}

Now, you could make FinalizeParameters have a root implementation that you override, but you want to be careful of breaking Liskov's Substitution Principle . 现在,您可以使FinalizeParameters具有您可以重写的根实现,但是您要小心打破Liskov的Substitution Principle It really does seem to me that you should use a ReportProperties object with the appropriate metadata, whether through a dictionary or some other mechanism. 在我看来,确实确实应该通过适当的元数据使用ReportProperties对象,无论是通过字典还是其他机制。 But, I would say forcing the final param adds via an abstract method is your best option if you dont want to go any other way. 但是,我想说如果您不想采用任何其他方式,则通过抽象方法强制添加最终参数是您的最佳选择。

You want to overload the method in the base class not override . 您想在基类中重载方法而不是重写

Override changes functionallty where overloading changes the signature. 覆盖更改功能,而重载会更改签名。 you can do this like this 你可以这样

class ReportShaft : Label
{
  public virtual IList<Microsoft.Reporting.WinForms.ReportParameter> NewReportSetup()
    {
   base.NewReportSetup("part", "batch", "locn", "wheel","gear", "length", "fits", "newbar",  "newbarnum", null)

}
}

This is helpful if you use alot of the same parms or have a default param that you call alot. 如果您使用许多相同的参数或具有很多您调用的默认参数,这将很有帮助。 Also note that I am calling base which will call the base classes method instead of the method in the class I am working in 还要注意,我正在调用base,它将调用基类方法而不是我正在使用的类中的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM