簡體   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