Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用继承时,访问非静态字段、成员或属性需要对象引用_C#_Inheritance_Xna 4.0 - Fatal编程技术网

C# 使用继承时,访问非静态字段、成员或属性需要对象引用

C# 使用继承时,访问非静态字段、成员或属性需要对象引用,c#,inheritance,xna-4.0,C#,Inheritance,Xna 4.0,您好,我在Visual Studio C 2010 Express中遇到一些奇怪的问题。我试图让一个对象从另一个对象继承,但我一直得到相同的错误 错误1非静态字段需要对象引用, 方法或属性 “PurpleThing.BasicSprite.texture”C:\Users\HAL-9000\Desktop\Examples\PurpleThing\PurpleThing\AnimateSprite.cs 13 39 PurpleThing 这是我希望另一个类从中继承的父类: namesp

您好,我在Visual Studio C 2010 Express中遇到一些奇怪的问题。我试图让一个对象从另一个对象继承,但我一直得到相同的错误

错误1非静态字段需要对象引用, 方法或属性 “PurpleThing.BasicSprite.texture”C:\Users\HAL-9000\Desktop\Examples\PurpleThing\PurpleThing\AnimateSprite.cs 13 39 PurpleThing

这是我希望另一个类从中继承的父类:

    namespace PurpleThing
{
    class BasicSprite
    {
        //Variable Drawing parameters
        protected Texture2D texture;
        protected Color tintColor;
        protected Vector2 spritePosition;

        public BasicSprite(Texture2D texture, Color tintColor)
        {
            //Saving external data into private/protected data
            this.texture = texture;
            this.tintColor = tintColor;
            //Adding a default point to draw the sprite at
            spritePosition = Vector2.Zero;
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, spritePosition, tintColor);
        }

        //Number of public outlets for information and remote changes
        public Texture2D Texture
        {
            get { return (texture); }
            set { texture = value; }
        }

        public Vector2 SpritePosition
        {
            get { return (spritePosition); }
            set { spritePosition = value; }
        }

        public Color TintColor
        {
            get { return (tintColor);}
            set { tintColor = value;}
        }
    }
}
这是我想从上面的类继承的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PurpleThing
{
    class AnimateSprite : BasicSprite
    {

        public AnimateSprite() : base(texture, tintColor)
        {

        }

    }
}

我是一个非常新的程序员,所以这个程序不是很复杂,但我不能解决这个问题。提前谢谢你

听起来像是在尝试将参数从构造函数传递给基构造函数

但是您的构造函数实际上没有任何参数,因此派生类中的纹理没有任何意义


您需要将参数添加到派生类型的构造函数中。

@chomba所以我需要在新构造函数中使用父参数的副本?这样地?public AnimateSpriteTexture2D纹理,Color tintColor:basetexture,tintColor{}非常感谢您的帮助。@Wolfspaine是的,构造函数不是继承的,因此每次创建派生类型的新实例时,您必须指定在基类中调用哪个构造函数。您可以通过在基类中定义无参数构造函数来避免这种需要。