我尝试为我的应用程序设置测试系统。我的想法是让添加更多测试变得非常简单,同时也让它们变得简单。每次测试都应该在那里。 (理智吧?)我做了一个“Head-Test”(我把它命名为'Horse'下面)类,它执行数组中的所有测试对象。 (好吧,我停在这里。如果没有代码,很难解释。)
以下是我提出的问题:(不,我没有按照这种方式命名这些课程,我只是想让它变得简单。)
class Dog
{
string type;
Result result;
public Dog(string typeIn)
{
this.type = typeIn;
this.result = new Result();
}
public Result execute()
{
result.message = "Dog and " + type + "say HI!";
return result;
}
}
class Bird : Dog
{
public static Bird bird = new Bird();
private Bird()
: base("Bird")
{
Horse.addDog(this);
}
}
class Result
{
public string message;
public Result()
{
this.message = "";
}
}
class Horse
{
private static List<Dog> dogs = new List<Dog>();
private static Result result = new Result();
public static void addDog(Dog dogIn)
{
dogs.Add(dogIn);
}
public static Result execute()
{
for (int i = 0; i < dogs.Count; i++)
{
Result tempResult = dogs[i].execute();
result.message += tempResult.message;
}
return result;
}
}
我很确定,错误是在“鸟”类附近 如果有人需要整个代码:My GitHub Repository
答案 0 :(得分:0)
如果您的关注只是一个类的实例,请查找Singleton模式。
https://msdn.microsoft.com/en-us/library/ff650316.aspx
你的马课应该看起来像这样
class Horse
{
private List<Dog> dogs = new List<Dog>();
private Result result = new Result();
private Horse()
{
}
private static readonly Horse instance = new Horse ();
public static Horse Instance {
get{
return instance;
}
}
public void addDog(Dog dogIn)
{
dogs.Add(dogIn);
}
public Result execute()
{
for (int i = 0; i < dogs.Count; i++)
{
Result tempResult = dogs[i].execute();
result.message += tempResult.message;
}
return result;
}
}
和Bird类消耗马匹
class Bird : Dog
{
public static Bird bird = new Bird();
private Horse horse = Horse.Instance;
private Bird()
: base("Bird")
{
horse.addDog(this);
}
}