一般编程,抽象类调用它不“知道”的方法。关于-AS3
我在 AS3 中工作。我们有一个抽象类,称之为 MyAbstractClass (我不认为它是真正的抽象,但我们只是扩展它)。扩展此类的类也实现一个接口,称为 IMyInterface。 IMyInterface 有一个名为 updateView 的函数。
我希望 MyAbstractClass 中的函数之一调用 updateView,但是当然 MyAbstractClass 没有名为 updateView 的函数,因此它无法编译。我是否在尝试某种不良做法?有没有一种简单的方法可以做我想做的事?
我应该将 updateView 移至 MyAbstractClass,然后在需要的地方重写它吗?谢谢
I am working in AS3. We have an abstract class, call it MyAbstractClass (I don't think it is truly abstract, but we are ONLY extending it). The classes that extend this class also implement an interface, call it IMyInterface. IMyInterface has a function called updateView.
I want one of my functions in MyAbstractClass to call updateView, but of course MyAbstractClass doesn't have a function called updateView so it won't compile. Am I attempting a bad-practice type of thing? Is there an easy way to do what I want?
Should I move updateView to MyAbstractClass, and just override it everywhere I need to? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,听起来这绝对应该是 MyAbstractClass 中的抽象方法。如果所有实现也实现了
IMyInterface
,那么听起来您应该声明MyAbstractClass
也声明了它。当然,这些都要求您正确地抽象MyAbstractClass
。基本上,您需要问自己:从逻辑上讲,您是否应该能够将
MyAbstractClass
的每个实例视为IMyInterface
?他们都应该有updateView
吗?这对于 MyAbstractClass 有意义吗?或者它们在逻辑上是独立的吗?Yes, it absolutely sounds like this should be an abstract method within
MyAbstractClass
. If all the implementations also implementIMyInterface
, it sounds like you should possibly declare thatMyAbstractClass
declares it too. Of course, these will both require that you makeMyAbstractClass
properly abstract.Basically, you need to ask yourself: logically, should you be able to treat every instace of
MyAbstractClass
as anIMyInterface
? Should they all haveupdateView
? Does that make sense forMyAbstractClass
, or are they logically separate?从声音来看,所有类(包括抽象类)都需要实现 IMyInterface,因此它应该位于层次结构的顶部。当然,那些扩展抽象类的类不需要显式声明它们实现 IMyInterface,但从概念上讲它们是这样做的。
From the sounds of things, all of your classes, including the abstract class need to implement IMyInterface, so it should be at the top of the heirarchy. Of course, those classes that extend the abstract class don't need to explicitly declare that they implement IMyInterface, but conceptually they do.
当您想调用“抽象”类中的接口方法时,您可以执行以下操作:
它可以工作,但它并不真正漂亮或优雅。您可能应该重新考虑您的层次结构,就像您正在考虑的那样。
When you want to invoke the interface method in your "abstract" class you could do the following:
It'll work, but its not really pretty or elegant. You should probably rethink your hierarchy, like you are considering.