编译错误:字段初始值设定项无法引用非静态字段、方法或属性

发布于 2024-10-01 02:54:16 字数 1217 浏览 9 评论 0原文

当我编译下面的代码时,编译器给出错误:字段初始值设定项无法引用此代码中的非静态字段、方法或属性(有星号)

 KingPiece kingPiece = new KingPiece(***siyahsah1***,ChessColor.White);

任何人都可以帮助我吗?

 class PiecePosition
{

    public enum ChessColor
    {
        White,
        Black,
    }
    public class ChessPiece
    {

        private Image DisplayedImage;
        private ChessColor DisplayedColor;
        private Point CurrentSquare;
        public Point[] ValidMoves;
        public ChessPiece(Image image, ChessColor color)
        {
            DisplayedImage = image;
            DisplayedColor = color;
        }
    }

    public  class KingPiece : ChessPiece
    {



        public KingPiece(Image image, ChessColor color)
            : base(image, color)
        {


            ValidMoves[0] = new Point(0, -1);    //  Up 1
            ValidMoves[1] = new Point(1, -1);  //  Up 1, Right 1
            ValidMoves[2] = new Point(1, 0);     //  Right 1

            ValidMoves[7] = new Point(-1, -1);  //  Left 1, Up 1
        }

        System.Drawing.Bitmap siyahsah1 = chess6.Properties.Resources.siyahsah1;
        KingPiece kingPiece = new KingPiece(siyahsah1,ChessColor.White);


    }

}

when i compile the code below the compiler give the error:A field initializer cannot reference the non-static field, method, or property in this code(which have stars)

 KingPiece kingPiece = new KingPiece(***siyahsah1***,ChessColor.White);

can anyone help me?

 class PiecePosition
{

    public enum ChessColor
    {
        White,
        Black,
    }
    public class ChessPiece
    {

        private Image DisplayedImage;
        private ChessColor DisplayedColor;
        private Point CurrentSquare;
        public Point[] ValidMoves;
        public ChessPiece(Image image, ChessColor color)
        {
            DisplayedImage = image;
            DisplayedColor = color;
        }
    }

    public  class KingPiece : ChessPiece
    {



        public KingPiece(Image image, ChessColor color)
            : base(image, color)
        {


            ValidMoves[0] = new Point(0, -1);    //  Up 1
            ValidMoves[1] = new Point(1, -1);  //  Up 1, Right 1
            ValidMoves[2] = new Point(1, 0);     //  Right 1

            ValidMoves[7] = new Point(-1, -1);  //  Left 1, Up 1
        }

        System.Drawing.Bitmap siyahsah1 = chess6.Properties.Resources.siyahsah1;
        KingPiece kingPiece = new KingPiece(siyahsah1,ChessColor.White);


    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无悔心 2024-10-08 02:54:16

您应该将该行移至构造函数中:

kingPiece = new KingPiece(siyahsah1,ChessColor.White);

问题是您还没有类实例,而编译器不喜欢这样。如果您只需将该行移到构造函数中,它就会起作用。

您仍然需要将该属性定义为私有字段。您可以通过以下方式执行此操作:

private KingPiece kingPiece;

在第一行的当前位置执行此操作。最终结果将与您现在所得到的完全一样,只是它会编译。

You should move the line into the constructor:

kingPiece = new KingPiece(siyahsah1,ChessColor.White);

The problem is that you don't yet have a class instance and the compiler doesn't like that. If you simply move the line into the constructor, it'll work.

You'll still have to define the property as a private field. You can do this with:

private KingPiece kingPiece;

Do this at the current location of the first line. The end result will be exactly like what you now heave, except that it will compile.

第七度阳光i 2024-10-08 02:54:16

该错误是自我解释的。 siyahsah1 是 KingPiece 的非静态私有属性,在调用构造函数时并未初始化。

The error is self explaining. siyahsah1 is a non-static private property of KingPiece and not is not initialized at the moment constructor is called.

雪若未夕 2024-10-08 02:54:16

正如其他人所说,siyahsah1 是一个非静态私有字段,不能用于初始化其他字段。
但这里还有另一个问题。由于 KingPiece 类只有一个构造函数,因此您无法在构造函数本身中创建该类的新实例 - 它会产生 StackOverflowException。解决方法是创建另一个私有构造函数,该构造函数只能从 KingPiece 类中调用。但更好的是,也许您可​​以准确地告诉我们您想做什么,我们可以帮助您。

更新:考虑到评论,我猜测 arash 想要指定与 KingPiece 类一起使用的图像。如果只使用一张图像,而与该部分的颜色无关,那么您可以将该参数传递给基本构造函数。

public class KingPiece: ChessPiece {
  public KingPiece(ChessColor color):
    // Pass the image to the base class. All king pieces will use this image.
    base(chess6.Properties.Resources.siyahsah1, color) {
    ..
  }
}

但是,如果您希望每种颜色都有不同的图像,那么应该在其他地方定义它,或者可能作为静态字段/属性。

public class KingPiece: ChessPiece {
  public static readonly BlackKing = new KingPiece(/* image of the black king here */, ChessColor.Black);
  public static readonly WhiteKing = new KingPiece(/* image of the white king here */, ChessColor.White);

  // Constructor could also be made private since you probably don't need other instances beside black and white.
  public KingPiece(Image image, ChessColor color): base(image, color) {
    ...
  }
}

希望这有帮助。

As others have said, siyahsah1 is a non-static private field, which cannot be used to initialize other fields.
But you have another problem here. Because the KingPiece class has only one constructor, you cannot create a new instance of that class in the constructor itself - it will produce a StackOverflowException. Workaround would be to create another, private constructor which could only be called from within the KingPiece class. But better yet, maybe you could tell us exactly what you want to do and we can help you with that.

Update: Considering the comments I'm guessing that arash wants to specify the image to be used with the KingPiece class. If only one image is used independent of the piece's color, then you can just pass that argument to the base constructor.

public class KingPiece: ChessPiece {
  public KingPiece(ChessColor color):
    // Pass the image to the base class. All king pieces will use this image.
    base(chess6.Properties.Resources.siyahsah1, color) {
    ..
  }
}

But if you want to have different images for each color then that should be defined elsewhere or maybe as a static field/property.

public class KingPiece: ChessPiece {
  public static readonly BlackKing = new KingPiece(/* image of the black king here */, ChessColor.Black);
  public static readonly WhiteKing = new KingPiece(/* image of the white king here */, ChessColor.White);

  // Constructor could also be made private since you probably don't need other instances beside black and white.
  public KingPiece(Image image, ChessColor color): base(image, color) {
    ...
  }
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文