OOP的问题:扩展类,覆盖功能和jQuery的语法语法、功能、问题、OOP

2023-09-09 21:45:51 作者:萌萌滴

我有一个面向对象与Flash相关的问题,动作3,这是一个个人项目,我要寻找一个设计模式或解决这一问题,而我的目标是去学习新的东西。

I have an OOP related problem with Flash, actionscript 3. It's a personal project, and I am looking for a design pattern or workaround for this problem, and my goal is to learn new things.

我已经创建了一个名为链。我创造了这个UTIL级,使延迟函数调用方便。你可以做一个链的功能,通过在毫秒的延迟将它们添加。此链可被多次执行,即使以相反的顺序。这个类的功能,它返回本身。这使得它可以有一个这样的jQuery的风格的语法:

I have created a class called Chain. I created this util-class to make delayed function calling easy. You can make a chain of functions, by adding them with a delay in milliseconds. This chain can be executed multiple times, even in reversed order. This class has functions which returns itself. That makes it possible to have a jQuery styled syntax like this:

var chain:Chain = new Chain(); 
chain.wait(100).add(myFunction1,300).wait(300).add(myFunction2,100);
// etc..

有关的例子,我已经离开了很多的功能,只是为了说明问题。该连锁类大多是纯粹的添加功能和启动/停止链。

For the example I have left lots of functions just to demonstrate the problem. The Chain class is mostly pure for adding functions and start/stopping the chain.

public class Chain 
{  
 function wait(delay:int = 0):Chain
 {
   // do stuff
   return this;
 }

 public function add(func:Function, delay:Number = 0):Chain
 {
      list.push( new ChainItem(func, delay) );
      return this;
 }
}

现在,我有另一个类叫做ChainTween。我想拆分开来,以保持链与一些核心功能,并ChainTween做一些动画的技巧。我有想法创建基于连锁类的小tweenengine。目前,它延伸链。它使用大量的连锁类保护变量,也覆盖了一些核心的功能链添加链的进程中补间功能。

Now, I have a another class called ChainTween. I am trying to split things up to keep the Chain with some core functions and have ChainTween do some animating tricks. I had the idea to create a little tweenengine based on the Chain class. Currently it extends Chain. It uses lots of protected variables from the Chain class and overrides also some core functions for Chain to add the tween functions inside the process of Chain.

public class ChainTween extends Chain
{  
 function animate(properties:Object = null, duration:Number = 0, easing:Function = null):ChainTween
 {
   // do stuff
   return this;
 }
}

现在这就是问题所在:我要保留链接语法,但等待()返回一个链实例,链条不具有动画的功能

Now this is the problem: I want to keep the chaining syntax, but wait() returns a Chain instance and Chain has no animate function.

var chain:ChainTween = new ChainTween();
chain.wait(100).animate({x:200}, 100).wait(250);

我试图覆盖wait()和add()函数在ChainTween类,但这会导致不兼容的替代。

I have tried to override the wait() and add() function in the ChainTween class but this causes an incompatible override.

我会投chain.wait(100)ChainTween,但这是非常丑陋的,而不是有用的,当我链接他们很多。现在,我不希望添加任何的ChainTween功能链(无虚函数太),我想保持完成所有功能,所以返回的对象是不是一种选择了。我试图用一个接口,但是这给了同样的问题,因为一个接口的功能应该在实现它的类来实现。

I could cast chain.wait(100) as ChainTween, but this is very ugly and not useful when I am chaining lots of them. Now I don't want to add any of the ChainTween functions to Chain (no dummy functions too), and I want to keep completion to all functions, so returning Object is not an option too. I tried to use an interface, but this gives the same problem, since the functions of an interface should be implemented in the class that implements it.

现在我也想过创造链中的ChainTween的一个实例,但是这并没有让我重写的功能,那么我应该做大量的公共属性,而不是受保护的,这是不是pferred太$ P $。

Now I have thought about creating an instance of Chain inside ChainTween, but this does not allow me to override functions, and then I should make lots of properties public instead of protected, which is not preferred too.

这是可能的,并没有任何人有一个很好的解决方案?

Is this possible and does anyone has a great solution for this?

推荐答案

这个问题是很常见的。您正在使用的设计模式被称为流利的接口,如果​​你谷歌流利接口继承,你会发现很多的问题和极少数的答案。

This problem is quite common. The design pattern you are using is called Fluent Interface and if you Google "Fluent Interface Inheritance", you'll find lots of questions and very few answers.

一个共同的方式来解决它在C#,Java和 C ++是使用模板。但是,我不知道如何实现在AS3一样,我发现这个话题的可以帮助你。

A common way to solve it in C#, Java and C++ is to use templates. However, I cannot tell how to implement the same in AS3, I found this topic that might help you.