当 child 函数应该覆盖时调用基础 class 函数

Base class function being called when child function should override

我正在制作国际象棋游戏并遇到错误,其中 child class 中的覆盖方法并不总是覆盖 parent class.

更具体地说是temp2.setgetMoves(一代),但仅在第二次用于一块。发生该错误是因为我在 parent 中使用了具有虚拟方法的继承,并在 child classes 中使用了覆盖方法。例如在parent class 中的方法是:

abstract class Piece
{
    String pieceType;
    int pieceVal;
    Panel piecePanel;


    public Piece(String type, int value, Panel image)
    {

        image.BackgroundImage = (Image)(Properties.Resources.ResourceManager.GetObject(type));

        pieceType = type;
        pieceVal = value;
        piecePanel = image;
    }

    public abstract List<Panel> setgetMoves(BoardGen board);

    public virtual List<Panel> getMoves()
    {
        return null;
    }

    public String getType()
    {
        return pieceType;
    }

    public void setPanel(Panel newPanel)
    {
        piecePanel = newPanel;
    }

    public Panel getPanel()
    {
        return piecePanel;
    }

    public int getValue()
    {
        return pieceVal;
    }
}

并且在child class中(例如,这是一个未完成的棋子class,它还没有吃子或过路人或晋升,但它是全部到目前为止我已经解决了)代码是:

class Pawn : Piece
{
    public List<Panel> possibleMoves;
    public Pawn(string type, int value, Panel image) : base(type, value, image)
    {

    }

    public override List<Panel> setgetMoves(BoardGen board)
    {
        possibleMoves = new List<Panel>();
        foreach (Panel x in board.getPanels())
        {
            if (this.getType().Substring(0, 1).Equals("W"))
            {
                if (this.getPanel().Location.Y == 240 && ((x.Location.Y == this.getPanel().Location.Y - 40) || (x.Location.Y == this.getPanel().Location.Y - 80)) && (x.Location.X == this.getPanel().Location.X))
                {
                    possibleMoves.Add(x);
                }

                else if((x.Location.Y == (this.getPanel().Location.Y - 40)) && (x.Location.X == this.getPanel().Location.X))
                {

                    possibleMoves.Add(x);
                }
            }
            else if (this.getType().Substring(0,1).Equals("B"))
            {
                if (this.getPanel().Location.Y == 40 && ((x.Location.Y == this.getPanel().Location.Y + 40) || (x.Location.Y == this.getPanel().Location.Y + 80)) && (x.Location.X == this.getPanel().Location.X))
                {
                    possibleMoves.Add(x);
                }
                else if (x.Location.Y == this.getPanel().Location.Y + 40 && (x.Location.X == this.getPanel().Location.X))
                {
                    possibleMoves.Add(x);
                }
            }
        }
        return possibleMoves;
    }

    public override List<Panel> getMoves()
    {
        return possibleMoves;
    }

任何关于为什么它不能覆盖第二次的帮助都会很有帮助。谢谢。

由于反复的评论发现了一个可能的 X-Y 问题,我想帮助修复您的 setPanel() 函数。希望这允许您重用正确类型的现有对象并避免创建没有正确类型的全新对象(因此找不到正确的覆盖)。

您的构造函数对其 Panel 参数的 BackgroundImage 属性 进行了更改:

image.BackgroundImage = (Image)(Properties.Resources.ResourceManager.GetObject(type));

因此,当我们更改面板时,我们应该撤消之前与原始面板的交互,并为新面板重做:

public void setPanel(Panel newPanel)
{
    newPanel.BackgroundImage = piecePanel.BackgroundImage;
    piecePanel.BackgroundImage = null;
    piecePanel = newPanel;
}