具有相同方法和不同参数的不同类
本文关键字:参数 同类 方法 | 更新日期: 2023-09-27 18:21:00
我有三个类:ToBeMum
、NewMum
和ExperiencedMums
。这些类有不同的方法,它们做相同的事情,但参数不同。如何重组代码以避免在所有三个类中复制/粘贴?例如:
NewMum:
public List<Question> GetMotherActualDayCareAge()
{
List<QuestionInfo> mumsHabits;
var answers = Answers.GetAnswers;
string answerValue = string.Empty;
var oneA = from a in answers
where a.Questionid == "Q49d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == "Q65d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var temp = oneA.Union(oneB).ToList();
mumsHabits = temp.Intersect(mothers, new UserIdEqualityComparer()).ToList();
return mumsHabits;
}
经验妈妈
public List<QuestionInfo> GetMotherActualDayCareAge()
{
List<QuestionInfo> mumsHabits;
var answers = Answers.GetAnswers;
string answerValue = string.Empty;
var oneA = from a in answers
where a.Questionid == "Q49c"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == "Q65c"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var temp = oneA.Union(oneB).ToList();
mumsHabits = temp.Intersect(mothers, new UserIdEqualityComparer()).ToList();
return mumsHabits;
}
这将非常简单。您可以在一个单独的类中编写具有相同概念的通用方法。
例如,创建一个名为Helper.cs的类,该类应包含以下方法
public List<QuestionInfo> GetMotherActualDayCareAge(string var1, string var2)
{
List<QuestionInfo> mumsHabits;
var answers = Answers.GetAnswers;
string answerValue = string.Empty;
var oneA = from a in answers
where a.Questionid == var1
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == var2
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var temp = oneA.Union(oneB).ToList();
mumsHabits = temp.Intersect(mothers, new UserIdEqualityComparer()).ToList();
return mumsHabits;
}
现在只要在需要的地方调用这个方法。在您的情况下,您可以从ToBeMum.cs、NewMum.cs和ExperiencedMams.cs调用此方法。
对于前任,在NewMum.cs,你可以这样打电话。
List<QuestionInfo> quesList = GetMotherActualDayCareAge("Q49d", "Q65d");
在ExperiencedMam.cs中,您可以这样调用。
List<QuestionInfo> quesList = GetMotherActualDayCareAge("Q49c", "Q65c");
所以现在这变成了动态的,只写一个方法,并且在没有复制/粘贴的情况下多次调用该方法。
这是用户2549110的第二个问题的答案。
尝试创建一个抽象类,如AbsMumSurvey.cs,它应该包含以下
public abstract class AbsMumSurvey
{
public abstract void IsDayCare();
public abstract void IsEconomicalToEducateChild();
public int NoOfBabies()
{
return 2;
//This is the sample logic just for understanding. You can write your own logic.
}
}
现在IsDayCare()和IsEconomicToEducationChild()是抽象方法,应该在派生类中强制重写。
现在在GoingToDeliver.cs中,您可以执行以下操作。
public class GoingToDeliver : AbsMumSurvey
{
public override void IsDayCare()
{
//Write your logic here.
}
public override void IsEconomicalToEducateChild()
{
//Write your logic here.
}
}
您可以实现也可以不实现NoOfbabais()。如果您还想实现NoOfBabies(),请尝试以下代码。
public new int NoOfBabies()
{
return base.NoOfBabies();
}
同样,您可以从许多类派生这些方法。
我将为以下内容提取一个通用方法:
var oneA = from a in answers
where a.Questionid == "Q49d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
var oneB = from a in answers
where a.Questionid == "Q65d"
select new QuestionInfo { questionId = a.Questionid, userId = a.UserId };
并将QuestionId作为参数传入,而不是在代码中对其进行硬编码。这个新方法可以从两个现有方法中调用,并避免重复代码