简体   繁体   English

在类(C#)中访问方法

[英]Accessing Methods Within a Class (C#)

I have a very simple question. 我有一个非常简单的问题。 This being said, I have tried to solve it by searching through stackexchange's previously answered questions but I came up short. 话虽这么说,我试图通过搜索stackexchange先前回答的问题来解决它,但是我很简短。 This is because I want to know how to tell the program's entry point to access other classes. 这是因为我想知道如何告诉程序的入口点访问其他类。

I had previously written a simple finite state machine but the code got congested because I did not know how to break it up into classes. 我以前曾编写过一个简单的有限状态机,但由于我不知道如何将其分解为类,所以代码变得很拥挤。 In my new program, I am trying to break it up so it can be better managed. 在我的新程序中,我试图对其进行分解,以便对其进行更好的管理。

The entry point starts off by accessing the class I created, NewState() . 入口点通过访问我创建的类NewState() If you run the code, you will observe that despite it compiling correctly, the function inside NewState() does not produce the Console.WriteLine statement I wanted it to. 如果您运行该代码,则会观察到,尽管编译正确,但NewState()内的函数不会产生我想要的Console.WriteLine语句。

So, my question is this: 所以,我的问题是这样的:

How do I make my code access the static void State() method within the NewState class and display the Console.WriteLine statement? 如何使我的代码访问NewState类中的static void State()方法并显示Console.WriteLine语句?

Program class: 程序类别:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Finite_State_Machine_3
{
    class Program
    {
        static void Main(string[] args)
        {
            new NewState();
        }
    }
}

NewState class: NewState类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Finite_State_Machine_3
{
    class NewState
    {
        static void State()
        {
                Console.WriteLine("Hello world");
        }
    }
}

The method is static, so instead of creating an instance, make the State method public and try this 该方法是静态的,因此不要创建实例,而是将State方法公开,然后尝试

static void Main(string[] args)
{
    NewState.State();
}

But if you're going to be calling it like that, you'd probably be better off putting it in the same class. 但是,如果要这样称呼它,最好将它放在同一个类中。

class Program
{
    static void Main(string[] args)
    {
        State();
    }

    static void State()
    {
        Console.WriteLine("Hello world");
    }
}

If you do want it in a separate class and call it from an instance of that class, you need to make the State method non-static (as well as public) and call it from the instance 如果确实要在单独的类中使用它,并从该类的实例中调用它,则需要使State方法成为非静态的(以及公共的)并从该实例中调用它

class Program
{
    static void Main(string[] args)
    {
        NewState MyVariable = new NewState();
        MyVariable.State();
    }
}


class NewState
{
    public void State()
    {
        Console.WriteLine("Hello world");
    }
}

If it's static, then you'll call it from the class not from the instance. 如果它是静态的,那么您将从类而不是实例中调用它。

So, 所以,

NewState.State()

That said, if I remember my design patterns right you actually want to be passing instances around, not using static methods. 就是说,如果我没记错我的设计模式,那么您实际上是想传递实例,而不是使用静态方法。

您需要将方法设置为public staticinternal static ,然后通过调用NewState.State()进行调用

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

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