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

C# – 由于其保护级别而无法访问,第1张

概述我无法弄清楚为什么会这样.我正在从Game类创建一个View类,然后我尝试从View in Game中调用一个方法,并将整个游戏对象作为参数发送给它.如果我发送单个变量作为参数然后它没有问题,但我想发送整个Game对象,以便只传递一件事. 游戏课 using System;using System.Collections.Generic;using System.Linq;using Mic 我无法弄清楚为什么会这样.我正在从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和从中派生的任何类都可以访问这些受保护的成员而没有其他人可以访问私有.类型VIEw是完全独立的类型,因此无法访问受保护或私有的任何成员.

要公开私有字段,需要将它们标记为内部或@R_301_6470@.

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

这些成员中的大多数也被标记为覆盖,因此您将被锁定为受保护.但是您可以使用另一个不受保护的成员来调用受保护的成员.从样本中不清楚是否要调用这些方法.

总结

以上是内存溢出为你收集整理的C# – 由于其保护级别而无法访问全部内容,希望文章能够帮你解决C# – 由于其保护级别而无法访问所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1217339.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存