AS3覆盖的addChild(),试作型addChild

2023-09-08 15:30:30 作者:恶鬼

这对我的作品,但它看起来正确的AS3最佳实践方面。它是正确的,如果它失败,返回null?

 重写公共职能的addChild(子:的DisplayObject):的DisplayObject {
        如果(孩子是屏幕){
            super.addChild(子);
            返回的孩子;
        }

        返回null;
    }
 

解决方案

我看到一对夫妇的选择,没有感觉完全正确的。

最好的解决办法,在我看来,是不可能在Actionscript中(我认为这是允许的其他语言)。也就是说,声明,你会接受屏只有实例:

 重写公共职能的addChild(子:屏幕):屏幕{
    返回super.addChild(子);
}
 
不分癌种 抗癌药恩曲替尼上市,癌症治疗新时代已经到来

由于这是不可能的,其他的选择,我能想到的是:

1)抛出一个错误。

我一般不是很喜欢扔错误的,但在这里亲的是,它的扩展类的行为相匹配。如果你传递一个非有效的显示对象的addChild,它会抛出。在另一方面,这是有效鉴于方法签名会告诉你,你必须通过一个DisplayObject。在这种情况下,你的方法是说谎,在某种程度上,因为它说,它接受的DisplayObject但它确实只接受屏幕的实例。所以,你是依靠在文档(希望),而在类型系统来告诉用户code如何它应该使用的功能。

2)添加了调用的addChild的addScreen方法。

 公共职能addScreen(子:屏幕):屏幕{
    返回super.addChild(孩子)的画面;
}
 

这是较为类型安全的,但你失去了一些多态的advatanges(也许这是不可能的/可行的code)条。也许,如果我们有方法重载......但我们不这样做的话,再次,我想这是一个权衡。

3)运行时类型检查和故障返回null。

这是你的code一样。这可能只是罚款。我不喜欢它就是我上面提到的:你的方法是一种说谎的。我不认为一个方法,需要一个DisplayObject失败,因为我通过一个DisplayObject。但同时,有时候,这不是什么大不了的事。

我怕我真的不能给这个一个明确的答案。如果可能的话,我想我可能会使用选项2)走了,但所有这些选择似乎同样有效。在某些时候,你不得不接受一个更有意义,为您和您的项目,我想。

this works for me, but does it look correct in terms of as3 best practices. is it correct to return null if it fails?

        override public function addChild(child:DisplayObject):DisplayObject {
        if(child is Screen) {
            super.addChild(child);
            return child;
        }

        return null;
    }

解决方案

I see a couple of options and none feels completely "right".

The best solution, as I see it, is not possible in Actionscript (I think this is allowed in other languages). That is, declare that you will accept only instances of Screen:

override public function addChild(child:Screen):Screen {
    return super.addChild(child);
}

Since that's not possible, the other options I can think of would be:

1) Throwing an Error.

I'm generally not very fond of throwing Errors, but a pro here is that it matches the behaviour of the extended class. If you pass a non valid display object to addChild, it will throw. On the other hand, this is valid given that the method signature tells you that you have to pass a DisplayObject. In this case, your method is "lying", in a way, because it says it accepts DisplayObjects but it really only accepts instances of Screen. So you are relying in documentation (hopefully) rather on the type system to tell the user code how it's supposed to use your function.

2) Adding an addScreen method that calls addChild.

public function addScreen(child:Screen):Screen {
    return super.addChild(child) as Screen;
}

This is somewhat more typesafe, but you lose some of the advatanges of polymorphism (and perhaps it's not possible / feasible in your code). Maybe if we had method overloading... but we don't so, again, I guess it's a tradeoff.

3) Runtime type checking and returning null on failure.

This is what your code does. It could be just fine. What I don't like about it is what I mentioned above: that your method is kind of "lying". I don't expect a method that takes a DisplayObject to fail because I passed a DisplayObject. But well, sometimes, this is not a big deal.

I'm afraid I can't really give a definite answer on this. If possible, I think I'd probably go with option 2), but all these options seem equally valid. At some point you have to settle for the one that makes more sense for you and your project, I guess.