【问题标题】:How to use an interface between classes and derived classes?如何在类和派生类之间使用接口?
【发布时间】:2015-03-11 01:52:06
【问题描述】:

我目前正在尝试制作国际象棋游戏并尝试实现接口但我无法访问该接口。

public interface IChessPiece
{
     bool CheckMove(int row, int col);
}

public class ChessPiece { ... }

public class Pawn : ChessPiece, IChessPiece
{
     public bool CheckMove(int row, int col) { ... }
}

public class ChessPieces {  public List<ChessPieces> chessPieces; ... }

我似乎无法访问 CheckMove() 方法。

board.chessPieces.Find(x => <condition>).CheckMove(row, col);

【问题讨论】:

    标签: c# class interface


    【解决方案1】:

    您可以将ChessPiece 实现为抽象类

    public interface IChessPiece {
      bool CheckMove(int row, int col);
    }
    
    // Note "abstract"
    public abstract class ChessPiece: IChessPiece {
      ... 
    
      // Note "abstract"
      public abstract bool CheckMove(int row, int col);
    }
    
    // Pawn implements IChessPiece since it's derived form ChessPiece
    public class Pawn: ChessPiece {
      // Actual implementation
      public override bool CheckMove(int row, int col) { ... }
    }
    

    【讨论】:

      【解决方案2】:

      您的类还需要实现IChessPiece 接口,并且很可能使其成为abstract,因为它不应该被直接实例化。然后你应该将板上的List 更改为IChessPiece 类型:

      public class ChessPiece : IChessPiece { ... }
      
      public class Pawn : ChessPiece, IChessPiece
      {
           public bool CheckMove(int row, int col) { ... }
      }
      
      public class ChessPieces {  public List<IChessPieces> chessPieces; ... }
      

      【讨论】:

        【解决方案3】:

        ChessPiece 类中实现IChessPiece

        public class ChessPiece : IChessPiece { ... }
        

        我似乎无法访问 CheckMove() 方法。

        因为您知道 ChessPieces 实现 CheckMove,但编译器没有。

        如果你不想在ChessPiece类中实现IChessPiece接口,那么你需要像这样进行类型转换

          ((IChessPiece)(board.chessPieces.Find(x => <condition>))).CheckMove(row, col);
        

        【讨论】:

          【解决方案4】:

          两种可能:

          1. 您可能希望在 ChessPiece 类中实现接口 - 由于接口名称,这对我来说更有意义。如果您需要在派生类中实现该方法,则将其设为抽象方法。

          2. 获取实现接口的所有棋子列表:ChessPieces.OfType&lt;IChessPiece&gt;

          【讨论】:

            【解决方案5】:

            ChessPiece 没有CheckMove 方法。你可以这样做:

            public abstract class ChessPiece : IChessPiece
            {
                public abstract bool CheckMove(int row, int col);
            }
            

            这确保任何从 ChessPiece 基类派生的人也必须实现 CheckMove 方法。任何从 ChessPiece 派生的类也将实现 IChessPiece。

            public class Pawn : ChessPiece // implicitly also implements IChessPiece
            {
                public override bool CheckMove(int row, int col) 
                {
                }
            }
            

            但是,接口的想法是,在使用它们时,实现应该无关紧要。因此,您的 List&lt;ChessPiece&gt; 确实应该是 List&lt;IChessPiece&gt; - 这实际上就足够了,因为添加到该列表的任何项目必须实现 IChessPiece,但基类无关紧要。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-01-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-11-05
              • 2011-11-12
              • 2018-10-14
              • 1970-01-01
              相关资源
              最近更新 更多