简体   繁体   English

抽象方法中的可选参数? 可能吗?

[英]Optional Parameters in Abstract method? Is it possible?

I have a abstract base class. 我有一个抽象基类。

I have 2 derived classes from this base class. 我有2个从该基类派生的类。

Is there anyway that one of my classes can ignore the string parameter in the abstract overide usage? 无论如何,我的一个类可以忽略抽象覆盖用法中的string参数吗? Or do I have to just send in a blank one and ignore it? 还是我只需要发送一个空白的邮件而忽略它? (making readability drop slightly) (使可读性略有下降)

Can I have one function that has some sort of optional parameter so that both of the following derived classes would compile? 我是否可以使用一个具有某种可选参数的函数,以便以下两个派生类都可以编译?

PS - The following code is riddled with in-compilable code for the example of what I would like to do PS-下面的代码充满了不可编译的代码,作为我想做的示例

PS PS - Yes i have compiled the following code already - see above comment for outcome PS PS-是的,我已经编译了以下代码-有关结果,请参见上面的注释

public abstract class MyBaseClass
{                                            //optional string?
    public abstract void FunctionCall(int i, string s = "");
}

public class MyDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i)
    {
        MessageBox.Show(i.ToString());
    }
}

public class YourDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i, string s)
    {
        MessageBox.Show(s + " " + i.ToString());
    }
}

If you don't absolutely need FunctionCall to be abstract, you can declare two versions of it: 如果您绝对不需要FunctionCall是抽象的,则可以声明它的两个版本:

public abstract class MyBaseClass
{
    public virtual void FunctionCall(int i)
    {
        this.FunctionCall(i, "");
    }
    public virtual void FunctionCall(int i, string s)
    {

    }
}

public class MyDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i)
    {
        MessageBox.Show(i.ToString());
    }
}

public class YourDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i, string s)
    {
        MessageBox.Show(s + " " + i.ToString());
    }
}

Otherwise, if it must be abstract to ensure it is implemented, you could still add two versions, it just makes the inheritors more verbose: 否则,如果必须抽象它以确保实现,则仍可以添加两个版本,这只会使继承者更加冗长:

public abstract class MyBaseClass
{
    public abstract void FunctionCall(int i);
    public abstract void FunctionCall(int i, string s);
}

public class MyDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i, string s)
    {
        throw new NotImplementedException();
    }
    public override void FunctionCall(int i)
    {
        MessageBox.Show(i.ToString());
    }
}

public class YourDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i)
    {
        throw new NotImplementedException();
    }
    public override void FunctionCall(int i, string s)
    {
        MessageBox.Show(s + " " + i.ToString());
    }
}

It will throw a compile error: "Abstract Inherited member 'MyBaseClass.FunctionCall(int, string)' is not implemented". 它将引发编译错误:“未实现Abstract继承的成员'MyBaseClass.FunctionCall(int,string)'”。

So no, the short answer is you can't do this. 所以不,简短的答案是您不能这样做。

Instead, you would have to do method overloading and implement BOTH abstract methods. 相反,您将必须进行方法重载并实现两个抽象方法。

public abstract class MyBaseClass
{    
    public abstract void FunctionCall(int i);                                        
    public abstract void FunctionCall(int i, string s = "");
}

public class MyDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i, string s = "") { }

    public override void FunctionCall(int i)
    {
        MessageBox.Show(i.ToString());
    }
}

public class YourDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i, string s)
    {
        MessageBox.Show(s + " " + i.ToString());
    }

    public override void FunctionCall(int i) {}
}

However this seems quite messy. 但是,这似乎很混乱。 Perhaps the best option is to always use the optional parameter and simply not pass in a value if it is not needed or handle it as you already seem to be doing. 也许最好的选择是始终使用可选参数,并且在不需要时根本不传递值,或者像您已经在做的那样处理它。

public class MyDerivedClass : MyBaseClass
{
    public override void FunctionCall(int i, string s = "") 
    {
        if (!string.IsNullOrEmpty(s))
            MessageBox.Show(i.ToString());
        else
           // handle other path here
    }
}

One possible way is to use extension methods to add missing overrides (which also works with interfaces) 一种可能的方法是使用扩展方法添加缺少的替代(也适用于接口)

static class MyBaseClassExtensions
{
       public void FunctionCall(MyBaseClass this item, int i)
       {
            item.FunctionCall(i, null);
       }
}

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

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