如何从主机上调用公共课程?

发布于 2025-02-07 22:37:27 字数 411 浏览 2 评论 0原文

因此,我正在创建一个小程序,并且很难从我的主机上召集公共课程。在主机中,如果用户选择某个选择,则应调用该类。该类在if-statement if(选择==“ 1”} {(class)} {(称为)}中。单独的类看起来像:

string actChoice = Console.ReadLine();

        if (actChoice == "2")
        {
            class Kitchen; 
   

        }
class Kitchen
{
    //this class will be played when you choose 2
    public class Ren
    {
        //setting method 
        public void Meal()

so I'm creating a small program and am having trouble calling a public class from my main console. In the main console, the class is to be called if the user selects a certain choice. The class is called within an if-statement if(choice == "1"} {(class is called)}. I'll also note there's a return statement at the bottom of the second class. Here's what the first three lines of the separate class look like:

string actChoice = Console.ReadLine();

        if (actChoice == "2")
        {
            class Kitchen; 
   

        }
class Kitchen
{
    //this class will be played when you choose 2
    public class Ren
    {
        //setting method 
        public void Meal()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2025-02-14 22:37:27

创建一个嵌套在另一类中的类的实例

var ren = new Kitchen.Ren() ;

string actChoice = Console.ReadLine();

switch (actChoice)
{
    case "1":
        
        break;
        case "2":
            Kitchen.Ren ren = new(); // One way to do it
        ren.Meal();
        break;
    default:
        break;
}
class Kitchen
{
    public class Ren
    {
        public void Meal()
        {
            Console.WriteLine("OK Meal");
        }
    }
}

Create an instance of a class nested inside another class

var ren = new Kitchen.Ren() ;

.

string actChoice = Console.ReadLine();

switch (actChoice)
{
    case "1":
        
        break;
        case "2":
            Kitchen.Ren ren = new(); // One way to do it
        ren.Meal();
        break;
    default:
        break;
}
class Kitchen
{
    public class Ren
    {
        public void Meal()
        {
            Console.WriteLine("OK Meal");
        }
    }
}
只是偏爱你 2025-02-14 22:37:27

如果要使用类,则需要创建该类的新实例。
除非该类是静态,在这种情况下,其中的所有内容(方法字段等)也应该是静态的。

例如,如何创建一个新课程:

var kitchenInstance = new Kitchen();

因此,这就是您应该做的:

string actChoice = Console.ReadLine();

if (actChoice == "2")
{
    var kitchenInstance = new Kitchen(); 
}

class Kitchen
{
    //this class will be played when you choose 2
    public class Ren
    {
        //setting method 
        public void Meal()
        {
        }
    }
}

在课堂上观看一些视频,以及一些示例,以增强您对它们的了解。

If you want to use a class you need to create a new instance of that class.
Unless this class is static, and in that case everything inside it (methods fields etc..) should be static as well.

How to create a new class for example:

var kitchenInstance = new Kitchen();

So this is how you should do it:

string actChoice = Console.ReadLine();

if (actChoice == "2")
{
    var kitchenInstance = new Kitchen(); 
}

class Kitchen
{
    //this class will be played when you choose 2
    public class Ren
    {
        //setting method 
        public void Meal()
        {
        }
    }
}

Watch a few videos on classes and some example of them to strengthen your knowledge about them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文