繁体   English   中英

C# - 由于其保护级别而无法访问

[英]C# - is inaccessible due to its protection level

我无法弄清楚为什么会这样。 我正在从Game类创建一个View类,然后我尝试从View in Game中调用一个方法,并将整个游戏对象作为参数发送给它。 如果我发送单个变量作为参数然后它没有问题,但我想发送整个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中的所有成员都被标记为protected或根本没有标记为默认为private 这意味着只有Game和从中派生的任何类都可以访问这些protected成员而没有其他人可以访问private 类型View是完全独立的类型,因此无法访问protectedprivate任何成员。

要公开private字段,需要将它们标记为internalpublic字段。

public class Game : Microsoft.Xna.Framework.Game
{
  ...
  public SpriteBatch spriteBatch;
}

这些成员中的大多数也被标记为override因此您将被锁定为protected 但是你可以使用其他non-protected成员打电话到protected 从样本中不清楚是否要调用这些方法。

所有这些都是隐含的private

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

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

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

    // etc... for your other fields

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM