Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这是一个使用抽象类的好例子吗?_C#_Abstract Class - Fatal编程技术网

C# 这是一个使用抽象类的好例子吗?

C# 这是一个使用抽象类的好例子吗?,c#,abstract-class,C#,Abstract Class,明天我得做一个关于抽象工作的简短报告。我尝试了一些我不理解的东西,但我不确定我是否做对了。但它很好用 基本上,我的表单上有三个文本框和一个按钮控件。我可以在前两个文本框控件中输入一些内容,按下按钮控件,加起来的结果显示在第三个文本框中。如果是数字,则显示总和。如果它是其他内容,它会将其作为字符串处理,并将其连接起来 主要方法: private void button1_Click(object sender, EventArgs e) { AbstractAddU

明天我得做一个关于抽象工作的简短报告。我尝试了一些我不理解的东西,但我不确定我是否做对了。但它很好用

基本上,我的
表单上有三个
文本框和一个按钮控件。我可以在前两个
文本框
控件中输入一些内容,按下
按钮
控件,加起来的结果显示在第三个
文本框
中。如果是数字,则显示总和。如果它是其他内容,它会将其作为字符串处理,并将其连接起来

主要方法:

    private void button1_Click(object sender, EventArgs e)
    {
        AbstractAddUp numberadding = new Numbers();
        AbstractAddUp stringadding = new TextStrings();

        //not really important, they are just used to satisfy parameters of int.Parse() method
        int firstnumber;
        int secondnumber;

        //If values in textboxes are numbers...
        if (int.TryParse(tbFirstOperand.Text, out firstnumber) && int.TryParse(tbSecondOperand.Text, out secondnumber))
        {
            object a = (object)(tbFirstOperand.Text);
            object b = (object)(tbSecondOperand.Text);
            tbResult.Text = numberadding.addValues(a, b).ToString();          
        }
        //...otherwise, if they are of some other type, treat them as string
        else
        {
            object a = (object)(tbFirstOperand.Text);
            object b = (object)(tbSecondOperand.Text);
            tbResult.Text = stringadding.addValues(a, b).ToString();
        } 
    }
抽象类:

public abstract class AbstractAddUp
{
    //abstract method that does the actual calculation or concatenation
    protected abstract object DoOperation(object a, object b);

    //public method to be called to initiate calculation/concatenation
    public object addValues(object a, object b)
    {
        return DoOperation(a, b);
    }
}

public class Numbers : AbstractAddUp
{
    //overridden method for adding two numbers
    protected override object DoOperation(object a, object b)
    {
        int firstnumber = int.Parse(a.ToString());
        int secondnumber = int.Parse(b.ToString());

        int result = firstnumber + secondnumber;

        return (object)(result);
    }
}

public class TextStrings : AbstractAddUp
{
    //overridden method for concatenating two strings
    protected override object DoOperation(object a, object b)
    {
        string strA = a.ToString();
        string strB = b.ToString();

        string result = strA + " " + strB;

        return (object)(result);
    }
}

不,我不认为这是
抽象类的好用法。类之间没有共享有用的实现,也没有真正共享的方法。您的示例非常简单,应该使用更简单的代码

//Check if value in textbox is a number or something else and add up
if (int.TryParse(tbFirstOperand.Text, out firstnumber) && int.TryParse(tbSecondOperand.Text, out secondnumber))
{              
    tbResult.Text = (firstnumber + secondnumber).ToString();
}
else
{
    tbResult.Text = tbFirstOperand.Text + " " + tbSecondOperand.Text;
    // or
    tbResult.Text = string.Format("{0} {1}", tbFirstOperand.Text, tbSecondOperand.Text);
}

如果你想用不同的方法来添加相似的类型,例如,字符串在中间加上一个空格,中间可能有逗号,那么有可能更复杂一些。如果这种情况会从某种多态性中受益,我认为一种好的方法是使用泛型

接口
抽象类
(根据经验,如果
抽象类
不提供要使用的实现或
受保护的
属性,则可能使用
接口
):


我在同一行中考虑的是Tim,但不会使用
if
。依赖类型会期望
Add
类,而不会关心它是
AddStrings
还是
AddInts
。或者,基类可以从接口继承,进一步

然后,您可以让一个工厂类决定返回哪种类型,从那时起,您的应用程序不需要知道,也不需要关心

public abstract class Add<T>
{
    public abstract T AddSomething(T left, T right);
}

public class AddStrings : Add<string>
{
    public override string AddSomething(string left, string right)
    {
        return left + right;
    }
}

public class AddInts : Add<int>
{
    public override int AddSomething(int left, int right)
    {
        return (left + right);
    }
}
公共抽象类添加
{
公共摘要T AddSomething(T左,T右);
}
公共类AddStrings:Add
{
公共重写字符串AddSomething(字符串左、字符串右)
{
返回左+右;
}
}
公共类附加项:添加
{
公共覆盖int AddSomething(int left,int right)
{
返回(左+右);
}
}

这不是一个很好的抽象类示例,因为该类根本不必是抽象的,代码才能工作。拥有一个抽象类的意义在于它本身不能工作,它需要从具体实现中获得一些东西才能完成

下面是一个抽象类的示例,该类以不同的方式组合字符串,具体实现告诉我们如何组合字符串:

public abstract class Combinator {

  // the base class needs to ask for the separator to use
  protected abstract string GetSeparator();

  public string Combine(string value1, string value2) {
    return value1 + GetSeparator() + value2;
  }

}

public class SpaceCombinator : Combinator {

  // the subclass needs to implement the abstract method for the code to compile
  protected override string GetSeparator() {
    return " ";
  }

}

public class DashCombinator : Combinator {

  public override string GetSeparator() {
    return "-";
  }

}
用法:

Combinator combinator;

combinator = new SpaceCombinator();
string name = combinator.Combine("John", "Doe");

combinator = new DashCombinator();
string phone = combinator.Combine("555", "524 855");

您的问题有点不清楚,因为抽象作为一个通用术语与抽象类无关。我不认为它是一个好例子,因为我读了它后不理解它。但只是一个注释。其他人可能可以更好地解释:-)您的抽象类有虚拟方法,虚拟方法!=抽象方法。您所做的看起来更适合接口。您最好有一个带有抽象方法的基类
Add
,然后让派生类实现它们。一个可以使用字符串和concat,另一个可以使用int。这将是一个更好的例子。您可以在基类中使用泛型T,并在具有所需类型(string和int)的派生类中交换它。抽象类可以从IAdd派生,这意味着消费类将只依赖于IAdd,而不关心是否添加字符串或intsok。我已尝试根据您的示例更改代码。你能快速回顾一下,看看现在是否更好了吗?@LeonidasFett:是的,看起来更好了,但是你应该使用
string
作为参数类型,而不是
object
,因为你实际上是在到处传递字符串。只是一张纸条;如果你编辑了你的原始问题,那么其他人就不会理解它的答案。您应该添加更新的代码,而不是更改原始代码。感谢您的帮助,由于您的示例,我现在对它有了更好的理解。不幸的是,我没有其他代码了,但我会记住;)
public abstract class Combinator {

  // the base class needs to ask for the separator to use
  protected abstract string GetSeparator();

  public string Combine(string value1, string value2) {
    return value1 + GetSeparator() + value2;
  }

}

public class SpaceCombinator : Combinator {

  // the subclass needs to implement the abstract method for the code to compile
  protected override string GetSeparator() {
    return " ";
  }

}

public class DashCombinator : Combinator {

  public override string GetSeparator() {
    return "-";
  }

}
Combinator combinator;

combinator = new SpaceCombinator();
string name = combinator.Combine("John", "Doe");

combinator = new DashCombinator();
string phone = combinator.Combine("555", "524 855");