【发布时间】:2019-06-07 12:28:48
【问题描述】:
我一直在努力理解 OOP 的一些基础知识。 我正在通过创建一个国际象棋游戏来练习,在该游戏中我创建了一个初始化所有棋子的所有运动属性的类。
public class Piece //<T> // using ' T ' as the generic variable of all the below-
{
//Movement of all pieces are constructed from this properties of this base class ('Piece')-
private int StepsLeft;
private int StepsRight;
private int StepsUp;
private int StepsBack;
//Diaganols:
private int DiagTopL;
private int DiagTopR;
private int DiagBotL;
private int DiagBotR;
public int StartPositionVert; // Vertical starting value: '1 thru 8' -
public string StartPositionHoriz; // Horizontal starting value: ' a thru h' -
//property
public int Left{
get { return StepsLeft; }
// Setting it equal to 'T' ?
set {
Left = StepsLeft; }
}
public int Right
{
get { return StepsRight; }
// Setting it equal to 'T' ?
set { Right = StepsRight; }
}
public int Up
{
get { return StepsUp; }
// Setting it equal to 'T' ?
set { Up = StepsUp; }
}
public int etc.
我为 pawn 创建了一个子类,但我似乎不明白构造函数如何工作得足够好来创建一个从父类继承属性的函数式..
class Pawn : Piece
{ // class for a single pawn piece
public Pawn() // << RED SYNTAX ERROR RIGHT HERE
{
bool FirstMove = true;
Left = 0;
Right = 0;
Up = 2; //< start it at two?-
Back = 0;
DTopLeft = 0; //start these off at zero-
DTopRight = 0; // - ^
DBotLef = 0; // < always -0-
DBotRite = 0; // < always -0-
}
public override void Move()
{
base.Move();// <<==- replace
}
}
Visual Studio 在单词“Pawn”(我的构造函数)上显示错误
我怎么用错了?可以在构造函数中调用和分配属性,但是我应该在 () 中包含哪些值......例如。 Pawn(int value, int value 2, int propertyName, etc)
我现在已经看了一百个教程视频,但我还是不明白。我希望我正在努力完成的事情甚至是有意义的!
悬停在红线上,实际的错误信息是:
没有给出与“Piece.Piece(int, int)”的所需形参“StepsLeft”相对应的参数
【问题讨论】:
-
就像我不明白为什么它需要更多参数。我应该在 pawn 类中的其他地方初始化它吗?
-
firstMove 定义在哪里?错误信息是什么?
-
错误信息是什么?
-
将鼠标悬停在红线上,告诉我们工具提示中显示的消息是什么。
-
其他一些想法,在等待该信息时...考虑将
Piece类抽象化,因为它只是一个基类,您不会期望创建该类型的类,只是派生类型。同样,其中的私有字段可以更改为受保护:然后Pawn类可以直接从构造函数中设置字段,而不是通过属性设置器(即bad idea)
标签: c# oop inheritance parent