梦想从C#中的继承结构 [英] The dream to inherit from a struct in c#

查看:263
本文介绍了梦想从C#中的继承结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有我正在使用C#4.0 XNA 2D游戏,并在我的又一次一个小烦恼运行;在长方形。对于使用基本碰撞那些,这几乎是必要的。对于几乎所有的游戏对象创建的,你需要有一个矩形。然后我去改变X,检查碰撞,或其他任何东西。在那里我开始 objectName.Rectangle.Whatever 的永无休止的战斗。为了解决这个问题我当然给类对象名的属性/访问这些对我来说方法。

There I am making a 2D game in C# XNA 4.0, and run across yet again a petty annoyance of mine; the Rectangle. For those using basic collision, this is almost a necessity. For almost any game object created you need to have a rectangle. Then I go to change the X, check collision, or anything else. And there I begin the never-ending battle of objectName.Rectangle.Whatever. To get around this I of course give the class of objectName properties/methods that access these for me.

然后我敢于梦想。我有宏伟的设计,使一个基本的游戏对象类,一切都将绘制继承,将允许养育,精确的局部坐标系(花车),并保持纹理/ spritebatch。为了使这一完整的我正准备从矩形继承,使用所有的方法和属性,它持有。哎呀,不管什么时候需要一个Rectangle,我可以这么懒的说了对象名,而不是objectName.Rectangle。

Then I dared to dream. I had grand designs to make a basic game object class that everything drawable would inherit from, that would allow parenting, precise local coordinates (floats), and hold the texture/spritebatch. To make this complete I was ready to inherit from Rectangle, to use all the methods and attributes that it holds. Heck, whenever something required a Rectangle, I could be so lazy as to say objectName, not objectName.Rectangle.

这时我才意识到,没有一个机会。我开始了郁闷我的哦,所以,聪明的想法被砸成碎片。从那时起,我的完美的小类包含一个矩形,用各种方法,并根据需要访问它的属性。我也注意到有它从XNA DrawableGameComponent继承的机会。而从长远来看,这一直是比较实用的,我每次观看一场平​​局或更新方法,并查看通话时间为矩形我常常在想,是有过一个希望做什么,我本来想?有没有一些聪明的变通,我可以做?或者是从一个矩形继承从我掌握真正的封?

Then I realized, not a chance. I started out depressed as my oh-so-clever idea was smashed to bits. Since then my perfect little class holds a rectangle, with various methods and properties accessing it as needed. I have also taken the chance to have it inherit from the XNA DrawableGameComponent. While in the long run this has been more practical, every time I view a draw or update method and see the call to rectangle I often wonder, was there ever a hope to do what I had wanted? Was there some clever work around that I could have done? Or was inheriting from a Rectangle truly sealed from my grasp?

虽然使用XNA提供的DrawableGameComponent类允许大多数游戏对象相关的操作发生的类更新()方法内一类我需要不能引用到一个矩形的财产以外的每一次,而是矩形本身,我略微恼怒考虑到在真正各种方式我的目标是,事实上,和各式的矩形。然后,我再一次能发现自己问:

While using the DrawableGameComponent class provided in XNA allows most game-object related actions happen inside the classes Update() method, every time outside of a class I need to reference not to property of a Rectangle, but rather the Rectangle itself, I am slightly peeved considering that in really every way my object is, in fact, and souped-up Rectangle. And then once again I can find myself asking:

有没有办法从一个预先定义的结构继承,或给予项目的印象,你是(一个变通办法)?

推荐答案

继承没有,但你可以有很多的默认功能添加到矩形对象,扩展方法。
为例

Inherit no, but you can add a lot of 'default' functionality to the Rectangle object with extension methods. For example

  //(inside a static class)
  public static int GetSurface(this Rectangle rect){return rect.Width * rect.Height;}

  //calling
  Rectangle rect;
  var s = rect.GetSurface();






这是说,我通常做的是封装说结构。使用类作为基础对象,并添加一个操作者,以便它可以被隐式转换为矩形。这样,它可以被传递到需要一个矩形没有铸造的方法。


That said, what I normally do is encapsulate said struct. Use that class as the base object, and add an operator so that it can be implicitly cast to a Rectangle. That way it can be passed to a method that needs a rectangle without casting.

    public class MyRect //class so you can inherit from it, but you could make your own struct as well
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public int Right { get { return X + Width; } }
        public int Bottom{ get { return Y + Height; } }

        public static implicit operator Rectangle(MyRect rect)
        {
            return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
        }

        public static implicit operator MyRect(Rectangle rect)
        {
            return new MyRect { X = rect.X, Y = rect.Y, Width = rect.Width, Height = rect.Height };
        }

    }       
 }

现在你可以手动也可以创建自己的矩形现有的:

Now you can create your own rect manually or from an existing one:

  MyRect rect = ARectangleVar

和您可以在期待一个矩形无铸造

And you can use it in legacy methods that expect a Rectangle without casting

这篇关于梦想从C#中的继承结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆