How do I use the interface between classes and derived classes?

I am currently trying to make a chess game and tried to implement the interface, but I cannot access the interface.

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; ... }

      

I am unable to access the CheckMove () method .

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

      

+3


source to share


5 answers


You can implement ChessPiece

as an abstract class:



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 derived form ChessPiece
public class Pawn: ChessPiece {
  // Actual implementation
  public override bool CheckMove(int row, int col) { ... }
}

      

+5


source


Your class also needs to implement an interface IChessPiece

and most likely render it abstract

, since it doesn't have to be directly instantiated. Then you have to change List

on the board to be of type IChessPiece

:



public class ChessPiece : IChessPiece { ... }

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

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

      

+2


source


implement IChessPiece

in the class ChessPiece

.

public class ChessPiece : IChessPiece { ... }

      

I am unable to access the CheckMove () method.

Because you know ChessPieces implements CheckMove, but the compiler doesn't.

If you don't want to implement the interface IChessPiece

in a class ChessPiece

then you need to inject a type like

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

      

+1


source


Two possibilities:

  • You might want to implement the interface in the ChessPiece class - it makes more sense to me because of the interface name. If you need to implement a method in derived classes, make it an abstract method.

  • Get a list of all ChessPieces implementing an interface: ChessPieces.OfType<IChessPiece>

+1


source


ChessPiece

has no method CheckMove

. You can do it:

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

      

This ensures that anyone receiving the base ChessPiece class must also implement the CheckMove method. Any class derived from ChessPiece will also implement IChessPiece.

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

      

However, the idea behind an interface is that when working with them, the implementation shouldn't matter. So yours List<ChessPiece>

should really be List<IChessPiece>

- that would be enough, since any items added to this list must implement IChessPiece, but the base class doesn't matter.

+1


source







All Articles