从 API 读取数据时,我试图减少代码。我有很多非常相似的方法,我确信我可以将它们重构为一种,我只需要指出正确的方向。下面是一个非常简化的代码版本 -
我想将“ReturnManagerName”和“ReturnTrainerName”重构为一种方法,有人可以帮忙吗?
void Main()
{
Team team = new Team();
Manager manager = new Manager();
manager.ManagerName = "Mr Manager";
team.TeamManager = manager;
Console.WriteLine ("Manager: " + ReturnManagerName(team));
Console.WriteLine ("Trainer: " + ReturnTrainerName(team));
}
string ReturnManagerName(Team team)
{
if(team.TeamManager == null)
return "None";
return team.TeamManager.ManagerName;
}
string ReturnTrainerName(Team team)
{
if(team.TeamTrainer == null)
return "None";
return team.TeamTrainer.TrainerName;
}
public class Team
{
public Manager TeamManager { get; set; }
public Trainer TeamTrainer { get; set; }
}
public class Manager
{
public string ManagerName { get; set; }
}
public class Trainer
{
public string TrainerName { get; set; }
}
谢谢你的帮助。