C# - 因保护级别而无法访问。

3
我不明白为什么会出现这种情况。我正在从Game类创建一个View类,然后尝试在Game中调用View的方法,并将整个游戏对象作为参数发送。如果我将单个变量作为参数发送,则没有问题,但我想发送整个Game对象,以便只传递一件事物。
游戏类
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        // Device Objects
        GraphicsDevice device = null;
        GraphicsDeviceManager graphics = null;
        MouseState mouse;

        // Game Objects
        SpriteBatch spriteBatch;
        Texture2D b = null;
        View view = null;

        // Arrays
        Buoy [] buoy = new Buoy[3];
        Plane [] plane = new Plane[3];

        // Variables
        bool selected = false;
        int index = 0;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
        }

        protected override void Initialize()
        {
            Content.RootDirectory = "Content"; 
            this.IsMouseVisible = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.ApplyChanges();
            base.Initialize();

            view = new View();

            for (index = 0; index < buoy.Length; index++)
            {
                buoy[index] = new Buoy();
                plane[index] = buoy[index].CreatePlane();
            }
        }

        protected override void LoadContent()
        {
            device = graphics.GraphicsDevice;
            spriteBatch = new SpriteBatch(device);
            b = Content.Load<Texture2D>("buoy");
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            mouse = Mouse.GetState();

            if (selected == true || mouse.X >= buoy[0].position.X - 3 && mouse.X <= buoy[0].position.X + 19 && mouse.Y >= buoy[0].position.Y - 3 && mouse.Y <= buoy[0].position.Y + 19)
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    selected = true;
                    buoy[0].position= new Vector2(mouse.X - 8, mouse.Y - 8);
                }

                else
                {
                    selected = false;
                }
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            view.DrawScreen(this);
        }
    }
}

查看类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    class View
    {
        public View()
        {

        }

        public void DrawScreen(Game game)
        {
            game.device.Clear(Color.CornflowerBlue);

            game.spriteBatch.Begin();
            DrawBuoys(game);
            game.spriteBatch.End();
        }

        public void DrawBuoys(Game game)
        {
            for (int x = 0; x < game.buoy.Length; x++)
            {
                game.buoy[x].DrawBuoy(game.spriteBatch, game.b);
            }
        }
    }
}

基本上,每次我尝试使用Game中的某些内容时都会出现错误。

传递引用有帮助吗? - Tim
3个回答

5
问题在于,Game内的所有成员都被标记为protected或未标记,默认为private。这意味着只有Game和任何派生自它的类才能访问这些protected成员,而没有人能访问private成员。类型View是一个完全独立的类型,因此无法访问任何被标记为protectedprivate的成员。
要公开private字段,需要将它们标记为internalpublic
public class Game : Microsoft.Xna.Framework.Game
{
  ...
  public SpriteBatch spriteBatch;
}

大多数这些成员也被标记为override,因此您被锁定到protected。但是您可以使用另一个非保护成员来调用protected。从示例中不清楚是否要调用这些方法。

2
所有这些都是隐式的私有的:
// Device Objects
GraphicsDevice device = null;
GraphicsDeviceManager graphics = null;
MouseState mouse;

// Game Objects
SpriteBatch spriteBatch;
Texture2D b = null;
View view = null;

// Arrays
Buoy [] buoy = new Buoy[3];
Plane [] plane = new Plane[3];

// Variables
bool selected = false;
int index = 0;

您需要将它们更改为public,这对成员变量来说是一个不好的主意。或者,您可以创建属性来访问成员变量:

public SpriteBatch SpriteBatch {
    get { return this.spriteBatch; }
    protected set { this.spriteBatch = value; }
}

它基本上是在告诉你这些变量不是公共的,而你却试图像访问公共变量一样去访问它们。 - Yatrix
1
如果你要这样做的话,考虑省略后备字段。public SpriteBatch SpriteBatch { get; set; } 更短更容易;让编译器为你生成后备字段。 - Eric Lippert

0

在C#中,默认情况下,任何没有明确定义访问级别的字段都被视为私有的。因此,在您的Game类中,您需要执行以下操作:

    // Device Objects
    public GraphicsDevice device = null;
    public GraphicsDeviceManager graphics = null;
    public MouseState mouse;

    // etc... for your other fields

其次,值得注意的是,您的Game类的所有方法都具有protected可访问性,这意味着只有继承自Game的类才能调用这些方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接