如何从一个方法或另一个方法中使用的类访问信息

本文关键字:方法 信息 访问 另一个 一个 | 更新日期: 2023-09-27 17:58:08

我正在编写一个基于文本的冒险游戏,在这个游戏中,我想从一个类中访问信息,该类用于一种方法,用于另一种方法。我打算做的是将角色信息放在一个线程中,然后在游戏过程中的任何时候将其打印到文本文档中,类似于暂停菜单。

我将尝试在下面写一个我想做的事情的例子。

class Player
{
    public int health;
}
public class Program
{
    public static void Main(string[] args)
    {
        Player player = new Player();
        player.health = 20;
    }
    public static void method2()
    {
        //Here I want to access the "player" in the Main method.
        //Then I want to print this and other stats to a text document:
        string[] lines = { "Stat 1", "Stat 2", "Stat 3" };
         System.IO.File.WriteAllLines(@"C:'Users'Public'TestFolder'WriteLines.txt", lines);
        Process.Start(@"C:'Users'Public'TestFolder'WriteLines.txt");
    }
}

如何从一个方法或另一个方法中使用的类访问信息

我建议去掉main中的所有内容,然后创建一个包含玩家的游戏类。这样,当你需要的时候,你就可以接触到球员,而不必来回传球。当你实例化玩家健康时,你也可以初始化它

public class Program
{
    public static void Main()
    {
        Game myGame = new Game();
        myGame.PlayGame();
    }
}
public class Game
{
    public Player myPlayer = new Player();
    public void PlayGame()
    {
        // place your loop / logic here to request input from the user and update your game state
    }
    public void WritePLayerStats()
    {
        string[] lines = { myPlayer.stat1, myPlayer.stat2 };
        File.WriteAllLines(@"C:'Users'Public'TestFolder'WriteLines.txt", lines);
        Process.Start(@"C:'Users'Public'TestFolder'WriteLines.txt");
    }
}
public class Player
{
    public int health = 20;
    public string stat1 = "";
    public string stat2 = "";
}

只需将Player传递给您的method2()

public static void Main(string[] args)
{
    Player player = new Player();
    player.health = 20;
    method2(player);
}
public static void method2(Player player)
{
    //Here I want to access the "player" in the Main method.
    //Then I want to print this and other stats to a text document:
    string[] lines = { player.health, "Stat 1", "Stat 2", "Stat 3" };
    System.IO.File.WriteAllLines(@"C:'Users'Public'TestFolder'WriteLines.txt", lines);
    Process.Start(@"C:'Users'Public'TestFolder'WriteLines.txt");
}

您也可以将方法移动到Player对象中,例如:

public class Program
{
    public static void Main(string[] args)
    {
        Player player = new Player();
        player.health = 20;
        player.PrintStatistics();
    }
}   
public class Player
{
    public int health;
    public void PrintStatistics()
    {
        //Here I want to access the "player" in the Main method.
        //Then I want to print this and other stats to a text document:
        string[] lines = { this.health, "Stat 1", "Stat 2", "Stat 3" };
        System.IO.File.WriteAllLines(@"C:'Users'Public'TestFolder'WriteLines.txt", lines);
        Process.Start(@"C:'Users'Public'TestFolder'WriteLines.txt");
    }
}

您可以通过以下两种方法之一来实现这一点:您可以通过将播放器实例作为参数来调用该方法,从而将值传递到该方法中。

public static void method2(Player player)
{
    string[] lines = { "Stat 1", "Stat 2", "Stat 3" };
    System.IO.File.WriteAllLines(@"C:'Users'Public'TestFolder'WriteLines.txt", lines);
    Process.Start(@"C:'Users'Public'TestFolder'WriteLines.txt");
}
public static void Main(string[] args)
{
     Player player = new Player();
     player.health = 20;
     method2(player);
}

或者你可以让它成为类的静态成员,并在启动游戏的逻辑之前使用Main来设置类

public class Program
{
    private static Player player;
    public static void method2()
    {
        string[] lines = { "Stat 1", "Stat 2", "Stat 3" };
         System.IO.File.WriteAllLines(@"C:'Users'Public'TestFolder'WriteLines.txt", lines);
         Process.Start(@"C:'Users'Public'TestFolder'WriteLines.txt");
    }
    public static void Main(string[] args)
    {
         player = new Player();
         player.health = 20;
         method2(player);
    }
}