【问题标题】:Assigning methods inside class constructor在类构造函数中分配方法
【发布时间】:2018-08-05 10:37:22
【问题描述】:

编辑:检查我在下面发布的答案。我创建了一个调用其他函数的代理方法。

我有一个类,我想分配来自不同类的方法。最好的方法是什么?

到目前为止,我已经尝试过:

namespace LeagueProj.DataGrabber{
    public class DataGrabber : IDataGrabber
    {
        private SummonerMethod sm = new SummonerMethod();
        private MatchlistMethod mlm = new MatchlistMethod();
        private MatchMethod mm = new MatchMethod();

        public delegate Summoner GetSummoner();
        public delegate Matchlist GetMatchlist();
        public delegate Data GetAllMatchData();

        public DataGrabber()
        {
            GetSummoner = sm._GetSummoner;
            GetMatchlist = mlm._GetMatchlist;
            GetAllMatchData = mm._GetAllMatchData;
        }
    }
}

但是,IDE 表示在构造函数中,GetSummoner、GetMatchlist 和 GetAllMatchData 是类型,但被用作变量(这是可以理解的)。我意识到我可能做错了,那么最好的方法是什么?

【问题讨论】:

标签: c# methods constructor


【解决方案1】:

您声明了委托,但未声明具有此类委托的字段:

public class DataGrabber : IDataGrabber
{
    private SummonerMethod sm = new SummonerMethod();
    private MatchlistMethod mlm = new MatchlistMethod();
    private MatchMethod mm = new MatchMethod();

    public delegate Summoner GetSummoner();
    public delegate Matchlist GetMatchlist();
    public delegate Data GetAllMatchData();

    public GetSummoner Method1;
    public GetMatchlist Method2;
    public GetAllMatchData Method3;

    public DataGrabber()
    {
        Method1 = sm._GetSummoner;
        Method2 = mlm._GetMatchlist;
        Method3 = mm._GetAllMatchData;
    }
}

【讨论】:

  • 类的前三行可能仍然是一个错误,因为我假设这些是方法,而不是对象。
  • @RonBeyer 哦,我想这看起来会让人困惑哈哈,new SummonerMethod 确实指的是一个类。我想我应该说清楚。
【解决方案2】:

您需要:

  • 将代表移到课堂外(不是强制性的,但通常是这样做的)
  • 添加委托类型的构造函数参数
  • 将构造函数中传递的值赋给类级变量

例子:

internal class MyClass
{
    public MyClass(GetElement getElement)
    {
        GetElement = getElement;
    }

    public GetElement GetElement { get; }
}

internal delegate Element GetElement();

internal class Element
{
}

【讨论】:

    【解决方案3】:

    您可以使用 Action 来做到这一点:

    public class MyActioner
    {
        private Action MyAction;
    
        public MyActioner()
        {
            MyAction = () => MyActionMethod();
        }
    
        public void MyActionMethod() 
        {
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      好的,所以一个朋友向我推荐我只是代理方法并让它们返回其他方法。这是一个更好的主意,imo。这是供将来参考的代码:

      namespace LeagueProj.DataGrabber
      {
          public class DataGrabber
          {
              private SummonerMethod sm;
              private MatchlistMethod mlm = new MatchlistMethod();
              private MatchMethod mm = new MatchMethod();
      
              public Summoner GetSummoner(string summonerName) { return sm._GetSummoner(summonerName); }
              private List<Matchlist> GetMatchlist(long accountId) { return mlm._GetMatchlist(accountId); }
              public List<Data> GetAllMatchData(List<Matchlist> matchlist, long accountId) { return mm._GetAllMatchData(matchlist, accountId); }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-08-21
        • 2021-11-24
        • 2013-11-05
        • 2022-01-18
        • 2016-08-22
        • 2015-05-08
        • 2018-10-03
        • 2013-05-28
        • 2016-02-09
        相关资源
        最近更新 更多