C# 代码重构#

C# 代码重构#,c#,C#,我正在开发一个小型应用程序来掌握C#,并且我已经编写了一个小型应用程序,该应用程序目前可以将项目的值相加(当前已预定义),以下是我目前所掌握的内容: //Defining classes public class Item1{ public string Type{get{return "Item1";}} } public class Item2{ public string Type{get{return "Item2";}} } //Methods public void

我正在开发一个小型应用程序来掌握C#,并且我已经编写了一个小型应用程序,该应用程序目前可以将项目的值相加(当前已预定义),以下是我目前所掌握的内容:

//Defining classes
 public class Item1{
  public string Type{get{return "Item1";}}
 }
 public class Item2{
  public string Type{get{return "Item2";}}
 }

//Methods
public void CalcItems(Item1 item1, int val){
 this.Log(item1.Type + "Val:" + val);
 this.total += val;
}

public void CalcItems(Item2 item2, int val){
 this.Log(item2.Type + "Val:" + val);
 this.total += val;
}

//Calling these methods
Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);

如何通过一种计算方法传递项目1和项目2?

使用
界面

public interface IItem
{
    string Type { get; }
}
然后在类声明上实现接口:

public class Item1 : IItem
{
    ...
    public string Type { get; }
    ...
}

public class Item2 : IItem
{
    ...
    public string Type { get; }
    ...
}
现在我们可以将方法
CalcItems()
定义为接受
IItem
参数:

public void CalcItems(IItem item, int val)
{
    this.Log(item1.Type + "Val:" + val);
    this.total += val;
}
这样,下面将引用相同的方法:

Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);

使用
界面

public interface IItem
{
    string Type { get; }
}
然后在类声明上实现接口:

public class Item1 : IItem
{
    ...
    public string Type { get; }
    ...
}

public class Item2 : IItem
{
    ...
    public string Type { get; }
    ...
}
现在我们可以将方法
CalcItems()
定义为接受
IItem
参数:

public void CalcItems(IItem item, int val)
{
    this.Log(item1.Type + "Val:" + val);
    this.total += val;
}
这样,下面将引用相同的方法:

Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);

向项目添加IItem接口,并用IItem替换Calcitems中的Item1。然后,您不需要两个calitem

将IItem接口添加到项目中,并用IItem替换calitems中的Item1。然后您不需要两个calitem

您可以为
Item1
Item2
定义一个接口,因为它们都共享公共属性
Type


您可以为
Item1
Item2
定义一个接口,因为它们都共享公共属性
Type

你可以利用。为项目对象定义接口,并按如下方式声明方法:

 void CalcItems<T>(T item, int val) where T : IItem
void calitems(T项,int val),其中T:i项
您可以利用。为项目对象定义接口,并按如下方式声明方法:

 void CalcItems<T>(T item, int val) where T : IItem
void calitems(T项,int val),其中T:i项

您可以使用策略设计模式进行重构。声明一个接口并将该接口实现到Item1和Item2类。使用该接口执行计算操作

public interface IItem {
    string Type { get; }
}

public class Item1: IItem {
    public string Type{get{return "Item1";}}
}

public class Item2: IItem {
    public string Type{get{return "Item2";}}
}

public void CalcItems(IItem item, int val){
    this.Log(item.Type + "Val:" + val);
    this.total += val;
}

Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);

您可以使用策略设计模式进行重构。声明一个接口并将该接口实现到Item1和Item2类。使用该接口执行计算操作

public interface IItem {
    string Type { get; }
}

public class Item1: IItem {
    public string Type{get{return "Item1";}}
}

public class Item2: IItem {
    public string Type{get{return "Item2";}}
}

public void CalcItems(IItem item, int val){
    this.Log(item.Type + "Val:" + val);
    this.total += val;
}

Items.CalcItems(new Item1(), 30);
Items.CalcItems(new Item2(), 12);

你说的“团体”是什么意思?是否希望将
Item1
Item2
传递给相同的
calitems
方法?@Jamiec是的,通过相同的方法传递这两个项目。对不起,找不到用词哈哈,你说的“团体”是什么意思?是否希望将
Item1
Item2
传递给相同的
calitems
方法?@Jamiec是的,通过相同的方法传递这两个项目。很抱歉,找不到“哈哈”这句话。我可以在所有类中实现它吗?您可以在单独的文件中看到问题。您可以在类定义中实现
IItems
。如果您无权访问源代码,请继承您正在使用的类以实现接口。(例如,
公共类MyItem1:Item1,IItem
可能是一个类声明)。完美!你有什么建议可以让我读一些C#编程的阅读材料,因为我的C#编程知识不如其他语言熟练。感谢练习总是最好的;使用类似的资源搜索您的答案。另外,这是一个很好的资源,为经常遇到的情况提供了一些很好的解释。我如何在所有类中实现这一点,您可以在单独的文件中看到这一点。您可以在类定义中实现
IItems
。如果您无权访问源代码,请继承您正在使用的类以实现接口。(例如,
公共类MyItem1:Item1,IItem
可能是一个类声明)。完美!你有什么建议可以让我读一些C#编程的阅读材料,因为我的C#编程知识不如其他语言熟练。感谢练习总是最好的;使用类似的资源搜索您的答案。此外,这是一个很好的资源,为经常遇到的情况提供了一些很好的解释。