1

我有一个名为 BaseRobot 的课程。包含以下代码:

//=== Defines the possible orientation of the robot.
//=== Note the order is important to allow cycling to be performed in a meaningful manner.
public enum Compass
{
    North, East, South, West
};

//=== The basic robot.
public class BaseRobot
{
    //--- The behaviour properties that were identified, together with associated state.
    //--- The robot identification number.
   private int mId;
   public int id
   {
       get { return mId; }
   }

   //--- the direction in which the robot is currently facing.
   private Compass mOrientation;
   public Compass Orientation
   {
       get { return mOrientation; }
       set { mOrientation = value; }
   }

   //--- The robot's current position.
   private Point mPosition;
   public Point Position
   {
       get { return mPosition; }
   }


   //--- the robot's home position, where it was originally created.
   public Point mHome;
   public Point Home
   {
       get { return mHome; }
   }

    //--- Turn the orientation left (anti-clockwise) or right (clockwise).
    //--- Implementation relies on the N, E, S, W ordering of the enumeration values to allow the arithmetic to work.

    public void TurnLeft()
    {
        --mOrientation;
        if (mOrientation < 0) mOrientation = Compass.West;
    } // end turnLeft method.    

    public void TurnRight()
    {
        mOrientation = (Compass)(((int)mOrientation + 1) % 4);
    } // end turnRight method.

    //--- Move one unit forward in the current orientation.
    public void Move()
    {
        switch (mOrientation)
        {
            case Compass.North: mPosition.Y++; break;
            case Compass.East: mPosition.X++; break;
            case Compass.South: mPosition.Y--; break;
            case Compass.West: mPosition.X--; break;
        }
    } // end Move method.

    //--- Constructor methods.
    public BaseRobot(int aId)
    {
        mId = aId;
        mHome.X = 0;
        mHome = new Point(0, 0);
        mPosition = mHome;
    }
    public BaseRobot(int aId, int aX, int aY)
    {
        mId = aId;
        mHome = new Point(aX, aY);
        mPosition = mHome;
    } // end BaseRobot constructor methods.
} 

在程序类中,我希望从此代码中调用对象、方法和属性,以显示重新分级机器人状态的信息,例如机器人的主页、方向和位置。我正在寻找控制台以显示如下内容:

机器人在 <0,0> 有家 它朝北,目前在 <0,0>

关于我如何实现这一目标的任何想法?

4

3 回答 3

1

首先,我建议您阅读Auto-Implemented Properties。这将使您的代码看起来像:

public Point Home { get; private set; }
public Point Position { get; private set; }
public Compass Orientation { get; private set; }

现在回到将机器人格式化为字符串。您可以覆盖ToString()机器人类的方法:

public override string ToString()
{
   return String.Format("Robot has home at {0} It is facing {1} and is currently at {2}",
                        mHome, mOrientation, mPosition);
}

或者简单地将机器人实例传递给以下方法:

public void WriteToConsole(BaseRobot robot)
{
   Console.WriteLine("Robot has home at {0} It is facing {1} and is currently at {2}",
                     robot.Home, robot.Orientation, robot.Position);
}

注意:默认情况下 System.Drawing.Point 类将转换为 string {X=42,Y=8}。如果您需要它的格式,<42,8>那么您应该手动提供 X 和 Y 的值,如下所示:

String.Format("Robot has home at <{0},{1}> It is facing {2} and is currently at <{3},{4}>", 
              Home.X, Home.Y, Orientation, Position.X, Position.Y);

或者,如果 Point 是您自己的类,则只需覆盖它的ToString()方法:

return String.Format("<{0},{1}>", X, Y);
于 2013-08-08T09:14:06.720 回答
0

尝试 Console.WriteLine,调用您的类方法来获取参数值,请参阅此处了解更多信息

http://msdn.microsoft.com/en-us/library/aakt1eab.aspx

于 2013-08-08T09:15:07.093 回答
0

格式字符串可用于轻松实现此目的。

您可以使用Console.Writeline,如下所示:

// Assumes a Robot r exists already
Console.Writeline("Robot has home at <{0},{1}> and is facing {2} and is currently at <{3},{4}>", r.Home.X, r.Home.Y, r.Orientation, r.Position.X, r.Position.Y);

覆盖 ToString()是另一种方法:

public override string ToString(){
    return string.Format("Robot has home at <{0},{1}> and is facing {2} and is currently at <{3},{4}>", Home.X, Home.Y, Orientation, Position.X, Position.Y);
}

然而,在这里覆盖 ToString() 似乎不正确,因为您正在显示有关机器人的状态信息,而这并不是真正的“机器人的字符串表示”,而这正是 ToString() 应该提供的。我要么使用上面的 Console.Writeline 方法,要么在 Robot 上有一个字符串 Status 属性来获取此类信息。

于 2013-08-08T09:15:08.780 回答