输入参数和向下转换 [英] Type parameters and downcasting

查看:55
本文介绍了输入参数和向下转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有委托属性的类,从它继承的类需要使用它并仍然访问它们自己的属性。以下是我到目前为止的内容:

I have a class with a delegate property and the classes inheriting from it need to use that and still access their own properties. Here's what I have so far:

public class GameElement:FrameworkElement
    {
        public int ZOrder = 0;
        public double Rotation = 1;
        public double TranslateX = 0;
        public double TranslateY = 0;
        public Vector Velocity = new Vector(0, 0);
        //this is the delegate
        delegate void Behavior(GameElement Owner, Type T);
        public Behavior Behaviors;
    }

//and I have several other classes inheriting from gameobject:
public class TextObject : GameElement
    {
        public string Text = "";
        public float Size = 0;
        public Brush Color = Brushes.Black;
        public TextObject(string name, string text, float size, Brush color)
        {
            Name = name;
            Text = text;
            Size = size;
            Color = color;
            Game.NewItemsThisFrame.Add(this);
        }
    }
//ideally, each frame would call to the delegate:
//if(item is TextObject)
//{
//    if(item.behaviors!=null)
//        item.behaviors(item,typeof(TextObject))
//}
//and in my delegate:
//public static void Behavior1(GameElement Owner, Type T)
//        {
             //var This = ??? Owner as T doesn't work
//        }



我是以正确的方式接近这个还是有更好的方法?


Am I approaching this the right way or is there a better way?

推荐答案

这不是你的做法不好或者好的(好吧,我能看到的并不是很好);问题不同:你在问题中歪曲了你的问题。所以,你应该一步一步,准确定义你想要实现的目标。



首先,你真的没有委托财产 。首先,您有一个委托类型行为。此外,您还具有基于该委托类型的委托实例类型的字段 行为。没有属性。



更糟糕的是,它与继承它的类需要使用它并仍然访问它们自己的属性完全无关,这只是关于继承一般情况下,无论进一步的细节是什么。



而且塞子是:你没有真正解释你的想法;而且我怀疑它是否有意义。您是否认为在重载中忽略了虚拟方法这样的基本OOP?想象一下,你只有一个行为,但在每个类中它可以与继承或不同相同。由于您没有显示每个类实例多于一个行为的情况,因此可能是可行的选项。而这个中心OOP机制已经关注多态this类型。考虑一下:

It's not that your approach is bad or good (well, what I can see is not very good); the problem is different: you misrepresent your problem in your question. So, you should make one step at a time and accurately define what you want to achieve first.

First of all, you don't really have a "delegate property". First, you have a delegate type Behavior. Also, you have the field Behaviors of the delegate instance type based on that delegate type. No properties.

Worse, it has absolutely nothing to do with "classes inheriting from it need to use that and still access their own properties", which is just about inheritance in general, no matter what further details are.

And the stopper is: you don't really explain your idea; and I doubt it makes sense at all. Don't you think that you are ignoring such a basic OOP thing as virtual methods in their overloading? Imagine you have only one behavior, but in each class it can be the same as inherited or different. As you are not showing the cases of more then one behavior per instance of the class, if could be the viable option. And this central OOP mechanism already takes care about polymorphous "this" type. Consider this:
public abstract class GameElement : FrameworkElement {
   protected abstract void Behavior(GameElement Owner);
   public void SomeBaseMethod() { Behavior(MyOwner); } // will only be called in some derived class
   //... 
}

public class TextObject : GameElement {
    protected override void Behavior(GameElement Owner) {
        if (someNewlyIntroducedField) { // this is possible, because "this" has the type TextObject
                                        // despite of the fact that Behavior was inherited from
                                        // GameElement which is "unaware of" the type TextObject
            //...
        }
    }
    bool someNewlyIntroducedField;
} //class TextObject





如果您需要类似多播代理的东西,您应该解释它的用法。无论如何,你的方法毫无意义,真的。您的 T 类型过于笼统,您无法使用它的任何知识。即使您可以通过反射和调用来探索它,也无法保证正确设置此类型。怎么样?不,这是一个死胡同。



对于完全不同的方法,请考虑使用泛型。除非你清楚地解释你的目标,否则我不能告诉你更多。



-SA



If you need something like a multicast delegate, you should explain its use. Anyway, your approach makes no sense, really. Your T type is too general, you cannot use any knowledge of it. Even though you can explore it through reflection and do invocation, you cannot guarantee that this type is set correctly. How? No, this is a dead end.

For a completely different approach, think about the use of generic. I cannot tell you more unless you explain your goals clearly enough.

—SA


这篇关于输入参数和向下转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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