更好的方法:混合枚举或2个枚举

发布于 2025-02-09 23:47:01 字数 1288 浏览 0 评论 0原文

我有两种移动的对象 - 盒子和汽车 - 移动有些不同。

方框:设置向右移动时,向左,向上或向下将移动1像素到所需的方向。

汽车:设置向右移动或向左移动时 - 将顺时针/逆时针旋转。当设置向前/向后移动时 - 将根据汽车的脸部和背面移动。因此,如果汽车旋转45度,我们将其设置为向前移动,则汽车将向右侧半像素和半像素(假设汽车启动朝上)。

我想知道以下哪种是更好的方法,或者有更好的方法:

public abstract class MovingObject
{
    public abstract void Move();

    public enum Direction { UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARD }

    public Direction CurrentDirection { get; set; }

    //some other fields and properties


    public class Box : MovingObject
    {
        public override void Move(){...}
        //some Box related things
    }


    public class Car : MovingObject
    {
        public override void Move(){...}
        //some Car related things
    }
}

public abstract class MovingObject
{
    public abstract void Move();

    //some fields and properties


    public class Box : MovingObject
    {
        public enum Direction { UP, DOWN, LEFT, RIGHT }

        public Direction CurrentDirection { get; set; }

        public override void Move(){...}
    }


    public class Car : MovingObject
    {
        public enum Direction { FORWARD, BACKWARD, LEFT, RIGHT }

        public Direction CurrentDirection { get; set; }

        public override void Move(){...}
    }
}

I have two kinds of moving objects — Box and Car — which move a bit different.

Box: when set to move right, left, up or down will move 1 pixel to the desired direction.

Car: when set to move right or left - will rotate clockwise/counterclockwise. And when set to move forward/backward - will move according to the face and back of the Car. So if the car is rotated by 45 degrees and we set it to move forward the car will go half a pixel to the right and half a pixel up (assuming the car is starting face up).

I would like to know which of the following is better approach or if there is a better way:

public abstract class MovingObject
{
    public abstract void Move();

    public enum Direction { UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARD }

    public Direction CurrentDirection { get; set; }

    //some other fields and properties


    public class Box : MovingObject
    {
        public override void Move(){...}
        //some Box related things
    }


    public class Car : MovingObject
    {
        public override void Move(){...}
        //some Car related things
    }
}

public abstract class MovingObject
{
    public abstract void Move();

    //some fields and properties


    public class Box : MovingObject
    {
        public enum Direction { UP, DOWN, LEFT, RIGHT }

        public Direction CurrentDirection { get; set; }

        public override void Move(){...}
    }


    public class Car : MovingObject
    {
        public enum Direction { FORWARD, BACKWARD, LEFT, RIGHT }

        public Direction CurrentDirection { get; set; }

        public override void Move(){...}
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-02-16 23:47:01

我建议创建两个枚举和一个通用基类,以枚举,代码看起来像这样。

public enum BoxDirection
{
    UP,
    DOWN,
    LEFT,
    RIGHT
}

public enum CarDirection
{
    LEFT,
    RIGHT,
    FORWARD,
    BACKWARD
}

public abstract class MovingObject<T> where T: Enum
{
    public abstract void Move();

    public T CurrentDirection { get; set; }

    //some other fields and properties


    public class Box : MovingObject<BoxDirection>
    {
        public override void Move() { ... }
        //some Box related things
    }

    public class Car : MovingObject<CarDirection>
    {
        public override void Move() { ... }
        //some Car related things
    }
}

I would suggest creating two enums and a generic base class that would take an enum, the code would look like this.

public enum BoxDirection
{
    UP,
    DOWN,
    LEFT,
    RIGHT
}

public enum CarDirection
{
    LEFT,
    RIGHT,
    FORWARD,
    BACKWARD
}

public abstract class MovingObject<T> where T: Enum
{
    public abstract void Move();

    public T CurrentDirection { get; set; }

    //some other fields and properties


    public class Box : MovingObject<BoxDirection>
    {
        public override void Move() { ... }
        //some Box related things
    }

    public class Car : MovingObject<CarDirection>
    {
        public override void Move() { ... }
        //some Car related things
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文