actionscript java_从Java到ActionScript的类设计挫折

AS3没有缺少特性,您不能将其定义为“欠发达”。

首先针对你的问题-有很多方法可以绕过抽象类方法论。

对于抽象类字符-您可以设置它,这样用户开发人员在尝试实例化它时会收到一个错误。

package com.strangemother.database.abstract

{

public class CentralDispatch extends EventDispatcher

{

private static var _centralDispatch:CentralDispatch;

public static function getInstance():CentralDispatch

{

if(!_centralDispatch)

_centralDispatch = new CentralDispatch(SingletonLock);

return _centralDispatch;

}

public function CentralDispatch(lock:Class)

{

if(!lock is SingletonLock)

{

throw new Error("CentralDispatch is a singleton. Use CentralDispatch.getInstance() to use.");

}

}

}

}

internal class SingletonLock{}

如您所见-这必须由“.getInstance方法”使用-但是要扩展它,只有这个类可以使自己成为一个新的实例,因为它的唯一类可以看到内部类“SingletonLock{}”。

出于您的目的-您可以删除“getInstance()”方法,并强制用户以另一种方式接收此类的实例。

这还应该显示生成内部类的能力。其他任何类都看不到这些,只有这个包和父类CentralDispatch可以使用它。

另一种使用抽象函数方法的方法是将其写入

interface

package com.strangemother.database.engines

{

import com.strangemother.database.Model;

import com.strangemother.database.ModelSchema;

import com.strangemother.database.events.ModelEvent;

public interface IEngine

{

/**

* Reads modelSchema and calls generateModel upon

* each model found.

* */

function generateModelSchema(modelSchema:ModelSchema=null):String

/**

* Generate your CREATE syntax model to output to your SQL

*

* If you are extending the framework with your own database

* engine, you must override this with your own model generation

* format.

* */

function generateModel(model:Model):String

}

}

然后在任何时候使用它,您都可以在类级别实现它

public class SQLite3 extends EngineBase implements IEngine

{

现在我的SQLite3类

必须

方法定义在

IEngine

我更喜欢编写定义了函数的类,这些函数在实现时被重写。

抽象库.as

/**

* connect to the database. This is not usually

* required as the abstraction layer should

* automatically connect when required.

* */

public function connect(onComplete:Function=null):void

{

SQLite3

其中包括

抽象库

在某个时刻

overide public function connect(onComplete:Function=null):void

现在驳斥@Allan关于它不够发达的评论(抱歉,老兄)

没有运算符重载-这是正确的,但Java也没有。它不是用来确保AS3是可读的。

函数重载-不能硬输入,但可以

function makeTea(...args)

根据您的需要传入尽可能多或尽可能少的数据。你也有getter/setter。

对于内联函数,可以创建匿名函数。

var myFunction:Function = Function(name:String):String{ return name + ' - rocks!'; }

有动态类,因此类级重载-

真正的代码的一个很好的例子是Flex Lib-it开源,您可以阅读如何通过逐渐遍历代码来管理所有这些元素。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值