从一个类调用对象、方法和属性,并将它们与信息一起显示在另一个类中

本文关键字:信息 另一个 显示 一起 属性 一个 调用 方法 对象 | 更新日期: 2023-09-27 18:26:47

我有一个名为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>

有什么想法可以让我做到这一点吗?

从一个类调用对象、方法和属性,并将它们与信息一起显示在另一个类中

首先,我建议您阅读有关自动实现属性的内容。这将使您的代码看起来像:

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);

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

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

格式化字符串可以很容易地实现这一点。

你可以使用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()似乎不正确,因为您显示的是关于Robot的状态信息,而该Robot并不是真正的"机器人的字符串表示",这正是ToString()应该提供的。我要么使用上面的Console.Writeline方法,要么在Robot上使用字符串Status属性来获取此类信息。