【问题标题】:How to make method call another method in the same class? [duplicate]如何让方法调用同一个类中的另一个方法? [复制]
【发布时间】:2013-04-20 01:48:00
【问题描述】:

我有一个名为methods.cs 的类包含两个方法。

例如method1method2

我想让method2 打电话给method1

代码说明:

public static void Method1()
{
// Method1
}

public static void Method2()
{
// Method2
}

我想让Method2 打电话给Method1。我怎样才能做到这一点?

【问题讨论】:

  • 同意,你刚才问的两个问题强烈建议一个非常新的初学者,应该咨询初学者级别的内容。
  • 你在用什么 C# 书?您应该退货并要求退款。

标签: c# class methods


【解决方案1】:

也许我从你的问题中遗漏了一些东西,但它应该像这样简单:

public static void Method1()
{

}

public static void Method2()
{
    Method1();
}

【讨论】:

    【解决方案2】:
    public static void Method2()
    {
    // Method2
        Method1();
    }
    

    【讨论】:

      【解决方案3】:

      这与您刚才提出的问题几乎完全相同:

      How to make method call another one in classes C#?

      但是……

      public class MyClass
      {
         public static void Method1()
          {
              // Method1
          }
      
          public static void Method2()
          {
              Method1();
          }
      }   
      

      【讨论】:

        【解决方案4】:

        很高兴看到您寻求帮助!为了在同一个类中包含的另一个方法中调用方法非常简单。就叫它的名字吧! Here is a nice little tutorial on Methods 下面是我的例子!

        public class ClassName
        {
            // Method definition to call in another Method
            public void MethodToCall()
            {
                // Add what you want to be performed here
            }
        
            // Method definition performing a Call to another Method
            public void MethodCalling()
            {
                // Method being called. Do this by using its Method Name
                // be sure to not forget the semicolon! :) 
                MethodToCall();
            }
        }
        

        祝您好运,希望对您有所帮助!

        【讨论】:

          猜你喜欢
          • 2013-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-15
          • 1970-01-01
          • 2011-01-14
          • 2013-10-08
          相关资源
          最近更新 更多