我有一个继承自pawn
的课程piece
:piece的函数moveTo
声明如下:
public virtual void moveTo(Coord newLoc)
{
Coord origLoc = location;
Transform transform = gameObject.GetComponent<Transform>();
Coord transCoord = newLoc - location;
if (LogicMaster.currentBoard[newLoc].piece != null) {
if (LogicMaster.currentBoard[transCoord].piece.targeted) {
UnityEngine.Object.Destroy(LogicMaster.currentBoard[transCoord].actor);
LogicMaster.currentBoard[transCoord].actor = null;
}
}
if (LogicMaster.currentBoard[newLoc].actor == null)
{
Vector3 transVector = new Vector3();
transVector.x = transform.position.x + transCoord.x * Constants.oneRight.x;
transVector.z = transform.position.z + transCoord.y * Constants.oneForward.y;
transVector.y = transform.position.y;
transform.position = transVector;
LogicMaster.currentBoard[newLoc].actor = gameObject;
LogicMaster.currentBoard[newLoc].piece = this;
LogicMaster.currentBoard[newLoc].piece.location = newLoc;
LogicMaster.currentBoard[this].actor = null;
LogicMaster.currentBoard[this].piece = null;
LogicMaster.selectedPiece = null;
LogicMaster.pieceSelected = false;
LogicMaster.currentBoard.resetColours();
LogicMaster.currentBoard.hideMarkers();
LogicMaster.whiteTurn = !LogicMaster.whiteTurn;
}
}
是否可以在pawn
类中实现覆盖,以实现moveTo
中的代码,但最后添加一些代码?否则我认为它只是一个复制和粘贴工作。
感谢。
答案 0 :(得分:2)
正如CodeCaster所提到的,你可以覆盖方法并调用base.MoveTo(..)然后添加你自己的逻辑。
实施例
基类:
public class CodeFirstInstallationService : IInstallationService
{
public virtual void InstallData(string defaultUserEmail,
string defaultUserPassword, bool installSampleData = true)
{
// Doing basic logic
}
}
从基础派生的类:
public class MyInstallationService : CodeFirstInstallationService
{
public override void InstallData(string defaultUserEmail,
string defaultUserPassword, bool installSampleData = true)
{
// Let the base class do it´s install logic
base.InstallData(defaultUserEmail, defaultUserPassword, installSampleData);
// Do my own logic
// Install flux capacitor etc
}
}