Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从一个类调用对象、方法和属性,并在另一个类中显示它们和信息_C#_Oop - Fatal编程技术网

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

C# 从一个类调用对象、方法和属性,并在另一个类中显示它们和信息,c#,oop,C#,Oop,我有一门课叫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. publ

我有一门课叫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.
} 
/==定义机器人的可能方向。
//==注意,顺序对于以有意义的方式执行循环非常重要。
公共枚举罗盘
{
北、东、南、西
};
//==基本机器人。
公共类BaseRobot
{
//---已识别的行为属性以及关联的状态。
//---机器人识别号。
私人int mId;
公共整数id
{
获取{return mId;}
}
//---机器人当前面对的方向。
私人罗盘监护;
公共罗盘定位
{
获取{return mOrientation;}
设置{mOrientation=value;}
}
//---机器人的当前位置。
私密点位;
公共点位置
{
获取{return mPosition;}
}
//---机器人最初创建的初始位置。
公共点mHome;
公共点之家
{
获取{return mHome;}
}
//---向左(逆时针)或向右(顺时针)转动方向。
//---实现依赖于枚举值的N、E、S、W顺序来允许算法工作。
公共空间左转()
{
--催眠;
如果(mOrientation<0)mOrientation=指南针。西;
}//结束左转方法。
公共道路右转()
{
Morentation=(指南针)((int)Morentation+1)%4);
}//结束右转方法。
//---在当前方向上向前移动一个单元。
公开作废动议()
{
开关(应急)
{
案例指南针。北:mPosition.Y++;中断;
案例指南针。东:mPosition.X++;中断;
案例指南针。南:位置:Y-;断开;
案例指南针。西:mPosition.X--;中断;
}
}//结束移动方法。
//---构造函数方法。
公共基地机器人(int-aId)
{
mId=援助;
mHome.X=0;
mHome=新点(0,0);
mPosition=mHome;
}
公共基站机器人(int-aId、int-aX、int-aY)
{
mId=援助;
mHome=新点(aX,aY);
mPosition=mHome;
}//结束BaseRobot构造函数方法。
} 
在program类中,我希望从这段代码中调用对象、方法和属性,以显示重新分级机器人状态的信息,例如机器人原点、方向和位置。我希望控制台显示如下内容:

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);
机器人在家里 它朝北,目前正处于


有什么办法可以做到这一点吗?

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

public Point Home { get; private set; }
public Point Position { get; private set; }
public Compass Orientation { get; private set; }
现在回到将机器人格式化为字符串。您可以重写robot类的
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);
}
或者只需将robot实例传递给以下方法:

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}
。如果需要将其格式化为
,则应手动为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);

首先,我建议你读一下。这将使您的代码看起来像:

public Point Home { get; private set; }
public Point Position { get; private set; }
public Compass Orientation { get; private set; }
现在回到将机器人格式化为字符串。您可以重写robot类的
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);
}
或者只需将robot实例传递给以下方法:

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}
。如果需要将其格式化为
,则应手动为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);

请尝试Console.WriteLine,调用类方法以获取参数值,有关详细信息,请参见此处


请尝试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);
//假设机器人r已经存在
Writeline(“机器人的原点位于且正对着{2},当前位于”,r.home.X,r.home.Y,r.Orientation,r.Position.X,r.Position.Y);
另一种方法是:

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);
}
public重写字符串ToString(){
返回string.Format(“Robot在家中,面向{2},当前在家中”,home.X,home.Y,Orientation,Position.X,Position.Y);
}
但是,在这里重写ToString()似乎不正确,因为您显示的机器人状态信息实际上不是“机器人的字符串表示”,而这正是ToString()应该提供的。我要么使用上面的Console.Writeline方法,要么在Robot上有一个string Status属性来获取此类信息。

可以很容易地实现这一点

您可以这样使用:

// 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);
//假设机器人r已经存在
Writeline(“机器人的原点位于且正对着{2},当前位于”,r.home.X,r.home.Y,r.Orientation,r.Position.X,r.Position.Y);
另一种方法是:

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);
}
public重写字符串ToString(){
返回string.Format(“Robot在家中,面向{2},当前在家中”,home.X,home.Y,Orientation,Position.X,Position.Y);
}

但是,在这里重写ToString()似乎不正确,因为您显示的机器人状态信息实际上不是“机器人的字符串表示”,而这正是ToString()应该提供的。我会使用上面的Console.Writeline方法,或者在Robot上有一个string Status属性来获取此类信息。

您的程序是否有
BaseRobot
的实例?您的程序是否有
BaseRobot
的实例?您的字符串需要封装在“;)有时候我们都会忘记一些事情,这没什么大不了的pHow我可以更改代码使机器人朝南吗?@user2657462此代码输出当前方向。若它并没有朝向南方,那个么转动它直到机器人将面对它south@lazyberezosky我该怎么转方向盘