如何在另一个C#中使用一个类中的代码

本文关键字:一个 代码 另一个 | 更新日期: 2023-09-27 18:28:37

我正试图将一个随机座位代码链接到我的用户界面类,但我不知道如何通过我的用户接口随时调用它。以下内容当前保存在FirstClassCarriage类中。

    {
        Random rand = new Random();
        bool[] seats = new bool[32];
        //To keep a separate list of seats taken          
        List<int> seatsBooked = new List<int>();
        bool quit = false;
        do
        {
            Console.Clear();
            //int seatAssignFirstClass = rand.Next(0, 32);-> Moved to switch-case 1: block
            int seatAssignThirdClass = rand.Next(32);
                    int seatAssignFirstClass; //Variable moved from main loop
                    //Are there any seats booked already or this is the first?
                    if (seatsBooked.Count == 0) //if this is the first seat to be booked...
                    {
                        seatAssignFirstClass = rand.Next(0, 32);
                        seats[seatAssignFirstClass] = true;
                        seatsBooked.Add(seatAssignFirstClass);  //Add seat to the list of booked seats.                            
                    }
                    else
                    {
                        do //while there are available seats and current seat has not being assigned before.
                        {
                            seatAssignFirstClass = rand.Next(0, 32);
                            if (!seatsBooked.Contains(seatAssignFirstClass)) //if seatAssignFirstClass is not booked.
                            {
                                seats[seatAssignFirstClass] = true;
                            }
                            //repeat while the random seat number is already booked and there are  avaialable seats
                        } while (seatsBooked.Contains(seatAssignFirstClass) && seatsBooked.Count < 32);
                        //IMPORTANT: Number on line bellow needs tos be one bigger than rest
                        if (seatsBooked.Count < 34) //if seatsBooked list is not full for First Class
                        {
                            seatsBooked.Add(seatAssignFirstClass); //Add current random-generated seat to the list.
                        }
                    }
                    //IMPORTANT: Number on line bellow needs tos be one bigger than rest
                    if (seatsBooked.Count >= 34) //If all seats are booked
                    {
                        Console.WriteLine("All seats for First Class are booked");
                        Console.WriteLine("Press enter to continue...");
                    }
                    else //Give customer their seat nmumber
                    {
                        Console.WriteLine("Train seat number: {0}", seatAssignFirstClass + 1);
                        Console.WriteLine("Press enter to continue to the main menu...");
                    }
        } while (!quit);

如何在另一个C#中使用一个类中的代码

C#中代码重用的最小单元是一个函数。你需要把这个代码放在一个函数中,然后你可以从其他地方调用它。最简单的是将其作为public static函数,但随着您对设计的了解越来越多,您可能会发现有更好的方法来共享该功能。

您可以使用using关键字和所需类的namespace 添加对其他文件中类的引用

示例:

namespace MyProject.MyCore {
    public class MyClass {
        public void MyMethod() { }
    }
}

然后在调用类中引用这个名称空间,如下所示:

using MyProject.MyCore

它允许您实例化类对象,如下所示:

var myInstantiatedClass = new MyClass();

并这样称呼它的方法:

myInstantiatedClass.MyMethod();

方法也可以标记为static,这样就不需要实例化类,而是使用Type.Method()语法进行调用,如MyClass.MyMethod()

您也可以通过使用完全限定的路径来放弃添加引用。

var myInstantiatedClass = new MyProject.MyCore.MyClass()

当然,如果此代码位于不同的项目或程序集中,则必须添加对该项目或二进制文件的引用才能访问它提供的类型。

这个例子可能是你的答案

或者你可以使用像这样的静态方法

namespace MyProject.MyCore {
    public class MyClass {
        public static void MyMethod() { }
    }
}

你可以从另一个类中使用这个方法

MyProject.MyCore.MyClass.MyMethod();