如何使一个方法调用同一类中的另一个方法

本文关键字:方法 一类 另一个 何使一 调用 | 更新日期: 2023-09-27 18:11:01

我有一个叫做methods.cs的类,它包含两个方法。

例如method1method2

我想让method2调用method1

代码说明:

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

我想让Method2呼叫Method1。我怎样才能做到呢?

如何使一个方法调用同一类中的另一个方法

很高兴看到你寻求帮助!为了在包含在同一个类中的另一个方法中调用方法是非常简单的。直呼其名!这是一个关于方法的小教程,我的例子在下面!

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();
    }
}
祝你好运,希望这对你有帮助!

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

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

这和你刚才问的问题几乎完全一样:

如何使方法调用另一个类c# ?

但是…

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