我有一个名为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.
}
在程序类中,我希望从此代码中调用对象,方法和属性,以显示重新定位机器人状态的信息,例如机器人主页,方向和位置。我正在寻找控制台来显示这样的东西:
机器人在&lt; 0,0&gt;处回家了。 它面向北方,目前处于&lt; 0,0&gt;
关于我如何实现这一目标的任何想法?
答案 0 :(得分: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类将转换为字符串{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);
答案 1 :(得分:0)
尝试使用Console.WriteLine,调用类方法获取参数值,有关详细信息,请参阅此处
答案 2 :(得分:0)
Format strings可以很容易地实现这一点。
您可以使用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);
Overriding 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属性来获取这类信息。