AS3 内部和自定义命名空间

发布于 2024-12-02 19:26:05 字数 856 浏览 2 评论 0原文

我有以下软件包:

spark
    spark.engine

spark 中,我有一个类 SeCore;在 spark.engine 中,我有 SeStepperSeKeyboard

我想要实现的目标是让 SeCore 成为唯一可以创建 SeStepperSeKeyboard 实例的类。这可以通过将 SeCore 移动到 spark.engine 包并将其他两个类设置为 internal 来实现,但我想要 <如果可能的话,将 code>SeCore 放在 spark 包中。

我尝试创建自己的名称空间来处理此问题,如下所示:

package spark.engine
{
    import spark.namespaces.spark_core;

    use namespace spark_core;

    spark_core class SeStepper extends SeObject
    {
        //
    }
}

但是我收到错误:

1116:用户定义的命名空间属性只能在顶部使用 类定义的级别。

我还可以采取其他方法来实现我所追求的目标吗?

I have the following packages:

spark
    spark.engine

Within spark I have a class SeCore; and within spark.engine I have SeStepper and SeKeyboard.

What I'm trying to achieve is have SeCore as being the only class that can create an instance of SeStepper or SeKeyboard. This can be achieved by moving SeCore into the spark.engine package and making the other two classes internal, but I'd like to have SeCore in the spark package if possible.

I've tried making my own namespace to handle this, like so:

package spark.engine
{
    import spark.namespaces.spark_core;

    use namespace spark_core;

    spark_core class SeStepper extends SeObject
    {
        //
    }
}

However I get the error:

1116: A user-defined namespace attribute can only be used at the top
level of a class definition.

Are there any other approaches I can take to achieve what I'm after?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2024-12-09 19:26:05

99% 的情况下,将任何内容标记为“内部”都是一个坏主意。最好对“禁区”类和成员有一个命名约定,并允许开发人员自行承担风险。将事物标记为“内部”或“私人”是应该很少做的事情,并且应该深思熟虑。

但是,您可以在运行时通过使用 SeCore 中的只读属性并从 SeStepper 和 SeKeyboard 检查其值来强制执行此行为。

以下是伪代码,有一段时间没有使用 AS3 了。

在 SeCore 中

private var _createAuthorized = false;
public function get CreateAuthorized():boolean {return _createAuthorized;}

private function createSeStepper(){
  _createAuthorized = true;
  var obj = new SeStepper(this)
  _createAuthorized = false;
  return obj;
}

在 SeStepper 中

public function SeStepper(core:SeCore){
  if (!core.CreateAuthorized) throw new Error("Only SeCore can do this");
}

99% of the time, marking anything as 'internal' is a bad idea. It's better to have a naming convention for 'off-limits' classes and members, and allow developers to go there at their own risk. Marking things as 'internal' or 'private' is something that should only be done rarely, and with great forethought.

However, you could enforce this behavior at run time by using a read-only property in SeCore and checking its value from SeStepper and SeKeyboard.

Following is pseudocode, haven't used AS3 in a while.

In SeCore

private var _createAuthorized = false;
public function get CreateAuthorized():boolean {return _createAuthorized;}

private function createSeStepper(){
  _createAuthorized = true;
  var obj = new SeStepper(this)
  _createAuthorized = false;
  return obj;
}

in SeStepper

public function SeStepper(core:SeCore){
  if (!core.CreateAuthorized) throw new Error("Only SeCore can do this");
}
比忠 2024-12-09 19:26:05

我不能同意这个答案,我的意思是公开事情是邀请黑客的一种方式。我可以在我想要的任何上下文中在计算机上运行的任何闪存中执行任何公共函数,我什至可以覆盖它们在内存中的执行,因为它们很容易找到,而使用私有/内部函数执行类似的操作几乎是不可能的。

I can't agree with the answer, i mean making things public is way to invite hackers. I can execute any public functions in any flash running on my computer in any context i want, i can even override their execution in memory since they are easy to find, whereas doing something like that with private/internal functions is almost impossible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文