“避免多个”;如果;迭代对象列表时的语句c#
我有各种生成excel图形的类 每个类生成一个不同的图 它们都共享相同的私有变量,具有不同的值 我希望编写一个通用代码,以防止使用“if”语句来确定它是哪个图形 以下是其中一个类的示例:“避免多个”;如果;迭代对象列表时的语句c#,c#,oop,object,types,casting,C#,Oop,Object,Types,Casting,我有各种生成excel图形的类 每个类生成一个不同的图 它们都共享相同的私有变量,具有不同的值 我希望编写一个通用代码,以防止使用“if”语句来确定它是哪个图形 以下是其中一个类的示例: using System; namespace GraphsGenerator { public class GraphOne { #region Private Members private string m_baseDir = ""; privat
using System;
namespace GraphsGenerator
{
public class GraphOne
{
#region Private Members
private string m_baseDir = "";
private static string m_graphName = "GraphOne";
private string m_imageFile = m_graphName + Utils.ImageExtension;
#endregion Private Members
#region Properties
public string BaseDir
{
set { m_baseDir = value; }
}
public string GraphName
{
get { return m_graphName; }
}
public string ImageFile
{
get { return m_imageFile; }
set { m_imageFile = value; }
}
#endregion Properties
#region Constructor
public HandTrackingGraphs(string baseDir)
{
m_baseDir = baseDir;
}
#endregion Constructor
}
}
我试着在我的主要工作中做到这一点:
List<object> listOfGraphs = new List<object>();
listOfGraphs.Add(new GraphOne());
listOfGraphs.Add(new GraphTwo());
listOfGraphs.Add(new GraphThree());
foreach (object currentGraph in listOfGraphs)
{
string imageFile = currentGraph.ImageFile;
}
List listOfGraphs=new List();
添加(新的GraphOne());
添加(newgraphtwo());
Add(newgraphthree());
foreach(listOfGraphs中的对象currentGraph)
{
字符串imageFile=currentGraph.imageFile;
}
但这当然不能做到
有什么想法吗
它们都共享相同的私有变量,具有不同的值
它们都应该实现相同的接口,该接口公开了ImageFile
属性。例如:
public interface IGraph
{
// TODO: Consider making this read-only in the interface...
public string ImageFile { get; set; }
}
然后你可以有:
List<IGraph> listOfGraphs = new List<IGraph>();
listOfGraphs.Add(new GraphOne());
listOfGraphs.Add(new GraphTwo());
listOfGraphs.Add(new GraphThree());
foreach (IGraph currentGraph in listOfGraphs)
{
string imageFile = currentGraph.ImageFile;
}
List listOfGraphs=new List();
添加(新的GraphOne());
添加(newgraphtwo());
Add(newgraphthree());
foreach(图形列表中的IGraph currentGraph)
{
字符串imageFile=currentGraph.imageFile;
}
您也可以使用抽象基类而不是接口。这有点限制,但这意味着图形也可以共享公共实现
(如果您真的希望灵活性和代码重用,您甚至可以创建一个由抽象基类实现的接口。)使所有类都从公共GraphBase抽象类继承。将公共属性作为抽象属性放在此类上,然后在派生类中重写它们 但这当然不能做到 它可以使用接口。定义包含要运行的方法的接口:
public interface IGraphWithImageFile
{
string ImageFile { get; }
}
然后将接口应用于所有类,并将列表声明为
list
已建议的接口,以便为您提供另一种选择-您可以使用基类,因为您不仅共享公共属性/方法,而且还共享公共实现,例如
那么你的用法就变成了
List<Graph> listOfGraphs = new List<Graph>();
listOfGraphs.Add(new GraphOne());
listOfGraphs.Add(new GraphTwo());
listOfGraphs.Add(new GraphThree());
foreach (IGraph currentGraph in listOfGraphs)
{
string imageFile = currentGraph.ImageFile;
}
List listOfGraphs=new List();
添加(新的GraphOne());
添加(newgraphtwo());
Add(newgraphthree());
foreach(图形列表中的IGraph currentGraph)
{
字符串imageFile=currentGraph.imageFile;
}
在这种情况下,您只需实现
要了解更多信息,请参阅此代码
abstract class AbsGraph
{
public string ImageFile { get; protected set; }
//other properties
public abstract void DrawGraph();
//other methods
public void CommonMethod()
{ }
//other common method
}
class Graph1 : AbsGraph
{
public override void DrawGraph()
{
//do graph specific task
}
}
class Graph2 : AbsGraph
{
public override void DrawGraph()
{
//do graph specific task
}
}
class Graph3 : AbsGraph
{
public override void DrawGraph()
{
//do graph specific task
}
}
现在你可以做了
var absGraphs = new List<AbsGraph>
{
new Graph1(),
new Graph2(),
new Graph3()
};
foreach (var graph in absGraphs)
{
graph.DrawGraph();
}
var absGraphs=新列表
{
新图形1(),
新图形2(),
新图表3()
};
foreach(absGraphs中的var图)
{
graph.DrawGraph();
}
var absGraphs = new List<AbsGraph>
{
new Graph1(),
new Graph2(),
new Graph3()
};
foreach (var graph in absGraphs)
{
graph.DrawGraph();
}