私有类成员与具有返回类型的方法

时间:2014-08-28 16:21:54

标签: c#

我最近一直在修补C#并遇到了一个问题:

我有四个班级:

  • 程序:使用Main方法。
  • 游戏:一切都在发生的地方。
  • 实体:基本实体类。
  • 播放器:派生自实体

(实体和玩家都在游戏类中)

现在,我的问题是,哪个更好:

  1. 在我的Game类中创建Player的私有对象并使用一些公共方法访问所述对象,或者......

  2. 制作一些公共方法并从整个类中访问的那些方法返回一个新的Player对象?

  3. 这可能会得到很好的解释,所以这里有一个例子:

    至1:

    namespace Test
    {
        class Game
        {
            public Game()
            {
                createChar();
            }
            private Player player;
    
            public class Entity
            {
                public string type;
            }
            public class Player : Entity
            {
                public string name;
            }
    
            private void createChar()
            {
                player = new Player();
    
                Console.WriteLine("What type of being is your character?");
                player.type = Console.ReadLine();
                Console.WriteLine("Name your character.");
                player.name = Console.ReadLine();
                Console.WriteLine();
            }
    
            public void run()
            {
                Console.WriteLine("Your character's name is:");
                Console.WriteLine(player.name);
            }
        }
    
        class Program
        {
            public static void Main(string[] args)
            {
                Game game = new Game();
                game.run();
            }
        }
    }
    

    到2:

    namespace Test
    {
        class Game
        {
            public class Entity
            {
                public string type;
            }
            public class Player : Entity
            {
                public string name;
            }
    
            public Player createChar()
            {
                Player player = new Player();
    
                Console.WriteLine("What type of being is your character?");
                player.type = Console.ReadLine();
                Console.WriteLine("Name your character.");
                player.name = Console.ReadLine();
                Console.WriteLine();
    
                return player;
            }
    
            public void run(Player player)
            {
                Console.WriteLine("Your character's name is:");
                Console.WriteLine(player.name);
            }
        }
    
        class Program
        {
            public static void Main(string[] args)
            {
                Game game = new Game();
                Game.Player player = game.createChar();
                game.run(player);
            }
        }
    }
    

    这个问题不应该是开放性的,如果它似乎是道歉,但为了使我的问题更具体一点,这里列出了我对这段代码的所有问题:

    1. 哪个程序更好1.或2.?
    2. 哪个程序更具可扩展性?
    3. 有没有更好的方法来编写程序?
    4. 我错过了可能会被问到的任何问题吗?
    5. 另外,请忽略类Entity和Player的空虚。

1 个答案:

答案 0 :(得分:0)

选项二听起来更好,因为游戏课程中需要的代码更少(您不需要方法来处理您希望游戏的用户类能够访问玩家的每一段数据),并且它“#”;更灵活(对Player类的更改不需要在Game类中更改方法)。

我可以看到选项1的唯一原因是,如果你有另一个类直接访问有关Player对象的数据(所以不通过Game类),并且这些数据确实不应该被类访问使用Game类来访问Player数据,因此证明在Game中有方法只允许Game的用户类从Player访问某些数据。

相关问题