简体   繁体   English

如何从C#中的所有基类继承所有属性

[英]How can I inherit all properties from all Base classes in C#

I have XNA game and it contains these classes 我有XNA游戏,其中包含这些类

public partial class Bird : Microsoft.Xna.Framework.GameComponent
{
    private Vector2 velocity;
    private Vector2 position;
    ..........................
    public Vector2 Velocity
    {
        get { return velocity; }
        set { velocity = value; }
    }
    public Vector2 Position
    {
        get { return position; }
        set { position = value; }
    }

}
public class BigRedBird : Microsoft.Xna.Framework.GameComponent,Bird
{

    public BigRedBird(Game game ,Rectangle area,Texture2D image)
        : base(game)
    {
        // TODO: Construct any child components here

    }
    .....................
}

How can I access Position and velocity from Bird Class and use it in BigRedBird Class in the constructor. 如何从Bird类访问位置和速度,并在构造函数的BigRedBird类中使用它。

Thanks 谢谢

To start with you are inheriting from two classes which would be illegal. 首先,您要从两个非法类继承。

As Bird already inherits from GameComponent its not a problem that you don't mention it in BigRedBird it's already inherited through bird! 因为Bird已经从GameComponent继承了,所以您在BigRedBird中没有提到它不是问题,它已经通过bird继承了!

As BigRedBird inherits from Bird it will have all its properties so you just need to do 由于BigRedBird从Bird继承而来,它将具有其所有属性,因此您只需要

public class BigRedBird : Bird 
{ 

    public BigRedBird(Game game ,Rectangle area,Texture2D image) 
        : base(game) 
    { 
        // TODO: Construct any child components here 
        this.Position= ....

    } 
    ..................... 
} 

C# doesn't support multiple inheritance, so the answer to the question in your title is - you can't. C#不支持多重继承,因此标题中问题的答案是-您不能。 But I don't think that's what you were trying to achieve. 但是我认为这不是您要实现的目标。

Add suitable constructors to your Bird class: 将适当的构造函数添加到Bird类:

public partial class Bird : Microsoft.Xna.Framework.GameComponent
{
   public Bird( Game game ) : base(game)
   {
   }

   public Bird( Game game, Vector2 velocity, Vector2 position ) : base(game)
   {
       Velocity = velocity;
       ...
   }
}

And then call the base class constructor in your derived class 然后在派生类中调用基类构造函数

public class BigRedBird : Bird
{
    public BigRedBird( Game game, ... ) : base(game, ... )
    {
    }
}

Or 要么

public class BigRedBird : Bird
{
    public BigRedBird( Game game, ... ) : base(game)
    {
        base.Velocity = ...;  // note: base. not strictly required
        ...
    }
}

Inherit BigRedBird from Bird only. BigRedBird Bird继承BigRedBird By doing that, you will still be able to access stuff from GameComponent , since Bird inherits from it. 这样,由于Bird继承自GameComponent ,因此您仍然可以从GameComponent进行访问。

By the way, inheriting from multiple classes is not possible in C#. 顺便说一下,在C#中不可能从多个类继承。

暂无
暂无

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

相关问题 如果这些属性从某个基类继承,我怎么能递归搜索C#中的属性? - How can I recursively search properties in C# only if those properties inherit from some base class? 如何编写通用方法来复制均从同一抽象基类继承的类的实例? - How do I write a general method to copy instances of classes that all inherit from the same abstract base class? 如何组织相互继承但又具有相互继承的属性的C#类? - How do I organize C# classes that inherit from one another, but also have properties that inherit from one another? 您如何使用反射获得一个类及其基类(在层次结构中)的所有属性? (C#) - How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#) 你可以在C#中继承多少个类? - How many classes can you inherit from in C#? Selenium C#如何从另一个类调用方法,两个类都从基类继承 - Selenium C# How do I call a method from another class both classes inherit from a base class 如何继承所有属性和控件以及先前创建的表单中的代码? - How can I inherit all of the properties and controls along with the code from a previously created form? 在基类上向构造函数添加新参数意味着我必须重构所有从其继承的类? - Adding a new parameter to constructor on the base class means I have to refactor all of the classes that inherit from it? c#从项目中的所有类访问数据表(使用属性) - c# Access a DataTable from all classes in project (using properties) UNITY,我如何从另一个 C# class 获取所有属性并将它们放入枚举 - UNITY, How can I get all properties from another C# class and put them into an enum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM