如何使用循环调用多个方法,请帮忙 [英] how to call multiple methods using loop, please help

查看:39
本文介绍了如何使用循环调用多个方法,请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void Main(string[] args)
       {
           for (int i = 1; i<=5; i++)
           {
                   Journali();
           }
       }

public static void Journal1()
       {
           console.write("a");
       }
public static void Journal2()
       {
           console.write("b");
       }
public static void Journal3()
       {
           console.write("c");
       }





有没有像使用循环调用多个方法所描述的abowe。



is there any way like described abowe to call multiple methods using loop.

推荐答案

是的,凭借代表的力量:

Yes, with the power of delegates:
class Program
{
  delegate void JournalMethod();

  public static void Journal1()
  {
    Console.Write("a");
  }
  public static void Journal2()
  {
    Console.Write("b");
  }
  public static void Journal3()
  {
    Console.Write("c");
  }

  static void Main(string[] args)
  {
    JournalMethod[] jm = { new JournalMethod(Journal1), new JournalMethod(Journal2), new JournalMethod(Journal3) };
    for (int n = 0; n < 3; ++n)
      jm[n]();
  }
}


如果我假设您的目标是想出一种使用selector参数调用各种方法的方法:
If I assume your goal here is to come up with a way to call various methods using a selector argument:
using System;
using System.Collections.Generic;

namespace YourNameSpace
{
    public static class TestClass
    {
        static TestClass()
        {
            IntToAction = new Dictionary<char, Action>
            {
                {'a', Journal1},
                {'b', Journal2},
                {'c', Journal3}
            };
        }

        public static Dictionary<char, Action> IntToAction; 

        // 'args set to null here so they can be ignored
        public static void Test(string[] args = null)
        {
            for (char theChar = 'a'; a < 'd'; a++)
            {
                // invoke the method selected by the char Key
                IntToAction[theChar]();
            }
        }

        public static void Journal1()
        {
            Console.WriteLine("a");
        }

        public static void Journal2()
        {
            Console.WriteLine("b");
        }

        public static void Journal3()
        {
            Console.WriteLine("c");
        }
    }
}

这个方法调度示例使用一个字典,其中Key是Type'char,而Value是Type Action(没有参数) )。 .NET中的字典提供非常有效的查找。



'Action是一种委托,允许更简洁的声明性语法。请参阅C. Pallini的解决方案,了解使用Delegates的示例。

This example of "method dispatch" uses a Dictionary where the Key is Type 'char, and the Value is Type Action (with no parameters). Dictionaries in .NET provide very efficient look-up.

'Action is a type of Delegate that allows a more terse declarative syntax. See the solution here by C. Pallini for an example of using Delegates.


明显版本究竟出现了什么问题?
What exactly is wrong with the obvious version?
public static void Main(string[] args)
{
    Journal1();
    Journal2();
    Journal3();
}
 
public static void Journal1()
{
    console.write("a");
}
public static void Journal2()
{
    console.write("b");
}
public static void Journal3()
{
    console.write("c");
}

请编辑您的问题,以便更清楚您要阻止的内容。

Please edit your question to make more clear what you're trying to prevent.


这篇关于如何使用循环调用多个方法,请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆