这个类会导致意外行为吗?

问题描述 投票:0回答:0

我遇到了一个类的例子,它似乎被设计用来表示 2D 空间中的通用形状对象。

这是三角形、直线、矩形和梯形类的基类。这些类用于在控制台中构建 ship 并沿 x 轴移动

    internal class Figure
    {
        protected char ch;
        protected string name;
        protected int x;
        protected int y;
        protected int speed;
        public Figure() : this(0, 10, 1, "", '*')
        {
        }
        public Figure(int x, int y, int speed, string name, char ch)
        {
            this.ch = ch;
            this.x = x;
            this.y = y;
            this.speed = speed;
            this.name = name;
        }
        public void SetCh(char c) { ch = c; }
        public char GetCh() { return ch; }
        public void SetX(int k) { x = k; }
        public int GetX() { return x; }
        public void SetY(int s) { y = s; }
        public int GetY() { return y; }
        public void SetName(string n) { name = n; }
        public string GetName() { return name; }
        public void SetSk(int k) { speed = k; }
        public int GetSk() { return speed; }
        virtual public void Draw() { }
        public void Move() { x += speed; }
    }

我绝对不喜欢这门课的某些东西,但我不知道它是什么。

在我看来,这个类被奇怪地封装了。尝试确认或反驳我的想法。

代码在某些情况下会导致意外行为吗?

c# oop inheritance polymorphism encapsulation
© www.soinside.com 2019 - 2024. All rights reserved.