简体   繁体   English

如何从另一个类到主类调用函数

[英]How to call a function from another class to main class

I created another class (calculations) and created a function in it which checks if a number is even or odd. 我创建了另一个类(计算),并在其中创建了一个函数来检查数字是偶数还是奇数。 I want to call this function in my program class so it can check if variable (result) is even or odd. 我想在程序类中调用此函数,以便它可以检查变量(结果)是偶数还是奇数。

I tried to call the method like: CheckEvenOrOdd(result). 我试图调用该方法,如:CheckEvenOrOdd(result)。

class Program
{
    static void Main(string[] args)
    {
        int number1;
        int number2;
        int result;

        Console.Write("Enter a number: ");
        number1 = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        number2 = int.Parse(Console.ReadLine());

        result = number2 * number2;

        Console.WriteLine($"The total is: {result} ");
        Console.WriteLine("AND");

       // i tried this here but it doesn't work: CheckEvenOrOdd(result)

    }


}


class Calculations
{
    public static void CheckEvenOrOdd(int numb)
    {
        if (numb % 2 == 0)
        {
            Console.WriteLine("The number is even");

        }
        else
        {
            Console.WriteLine("The number is odd ");
        }

    }

}

Because your method is in a different class, you're going to have to make it static and then call it by class name first then method name. 由于您的方法位于不同的类中,因此必须将其设置为静态,然后先按类名然后按方法名进行调用。 If it's not static you're going to have to instantiate a new instance of that class before being able to access any of its methods. 如果不是静态的,则必须先实例化该类的新实例,然后才能访问其任何方法。

(btw you're multiplying number2 by number2, changed it for you as well in the codes provided below hehe) (顺便说一下,您要将number2乘以number2,并在下面提供的代码中也为您进行了更改)

Static: 静态的:

class Program
{
    static void Main(string[] args)
    {
        int number1;
        int number2;
        int result;

        Console.Write("Enter a number: ");
        number1 = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        number2 = int.Parse(Console.ReadLine());

        result = number1 * number2;

        Console.WriteLine($"The total is: {result} ");
        Console.WriteLine("AND");

        Calculations.CheckEvenOrOdd(result);
        Console.ReadLine();
     }
}

public static class Calculations
{
    public static void CheckEvenOrOdd(int numb)
    {
        if (numb % 2 == 0)
        {
            Console.WriteLine("The number is even");
        }
        else
        {
            Console.WriteLine("The number is odd ");
        }
    }
}

Not Static: 不是静态的:

class Program
{
    static void Main(string[] args)
    {
        int number1;
        int number2;
        int result;

        Console.Write("Enter a number: ");
        number1 = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        number2 = int.Parse(Console.ReadLine());

        result = number1 * number2;

        Console.WriteLine($"The total is: {result} ");
        Console.WriteLine("AND");

        Calculations calc = new Calculations();
        calc.CheckEvenOrOdd(result);
        Console.ReadLine();
     }
}

public class Calculations
{
    public void CheckEvenOrOdd(int numb)
    {
        if (numb % 2 == 0)
        {
            Console.WriteLine("The number is even");
        }
        else
        {
            Console.WriteLine("The number is odd ");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM