ActionScript - 确定调用者的类?

发布于 2024-11-02 16:58:23 字数 1539 浏览 1 评论 0原文

因为 MXMLC 编译器不支持 setters/getters 的混合访问修饰符< /a>,我想知道公共 setter 函数是从 setter 类的内部还是外部调用的。这种方法应该允许我维护公共财产,但具有更高的安全性,例如公共/私人混合访问。

是否可以使用参数的被调用者属性或其他属性来确定 setter 是从 setter 的类内部设置的还是外部设置的?

这是我的意思的一个例子:

package
{
//Class
public class MyLocation
    {
    //Constants
    public static const LOCATION_LEFT:String = "locationLeft";
    public static const LOCATION_RIGHT:String = "locationRight";

    //Properties
    private var sideLocationProperty:String;

    //Constructor
    public function MyLocation(sideLocation:String = MyLocation.LOCATION_LEFT)
        {
        this.sideLocation = sideLocation;

        trace(sideLocation);
        }

    //Texture Panel Is Collapsed Setter
    public function set sideLocation(value:String):void
        {
        if  (value != LOCATION_LEFT && value != LOCATION_RIGHT && value != LOCATION_AUTO)
            throw new ArgumentError("\"MyLocation set sideLocation(value:String)\" – value parameter is not supported.");

        sideLocationProperty = value;

        //pseudo code
        if  (arguments.callee is from MyLocation)
            trace("sideLocation was set internally");
            else
            trace("sideLocation was set externally");
        }

    //Texture Panel Is Collapsed Getter
    public function get sideLocation():String
        {
        return sideLocationProperty;
        }
    }
}

since the MXMLC compiler doesn't support mixed access modifiers for setters/getters, i would like to know whether a public setter function is being called from inside or outside of the setter's class. this approach should allow me to maintain a public property but with more security, something like public/private hybrid access.

is it possible to use arguments' callee property, or perhaps something else, to determine whether the setter was set internally from the setter's class or if it was set externally?

here's an example of what i mean:

package
{
//Class
public class MyLocation
    {
    //Constants
    public static const LOCATION_LEFT:String = "locationLeft";
    public static const LOCATION_RIGHT:String = "locationRight";

    //Properties
    private var sideLocationProperty:String;

    //Constructor
    public function MyLocation(sideLocation:String = MyLocation.LOCATION_LEFT)
        {
        this.sideLocation = sideLocation;

        trace(sideLocation);
        }

    //Texture Panel Is Collapsed Setter
    public function set sideLocation(value:String):void
        {
        if  (value != LOCATION_LEFT && value != LOCATION_RIGHT && value != LOCATION_AUTO)
            throw new ArgumentError("\"MyLocation set sideLocation(value:String)\" – value parameter is not supported.");

        sideLocationProperty = value;

        //pseudo code
        if  (arguments.callee is from MyLocation)
            trace("sideLocation was set internally");
            else
            trace("sideLocation was set externally");
        }

    //Texture Panel Is Collapsed Getter
    public function get sideLocation():String
        {
        return sideLocationProperty;
        }
    }
}

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

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

发布评论

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

评论(4

梦回旧景 2024-11-09 16:58:23

您很可能无法做到这一点。

你为什么不这样写,

public function setSideLocation(value:String, caller:* = null):void

然后你就可以用它做任何你想做的事情,比如

trace(caller is MovieClip);

caller.hasOwnProperty(something)

等。

通过将其设置为 null 作为默认值,你甚至不需要需要使用它。只是可选的。

因此,要从另一个对象调用它,请执行以下操作:

object.setSideLocation("something", this);

另外,为了保持一致,请编写

public function getSideLocation():String

Most likely you can't do that.

Why don't you write

public function setSideLocation(value:String, caller:* = null):void

then you can do whatever you want to do with it, like

trace(caller is MovieClip);

or caller.hasOwnProperty(something)

etc.

By setting it to null as a default value, you don't even need to use it. Just optionally.

So to call it from another object do:

object.setSideLocation("something", this);

Also to keep it consistent write

public function getSideLocation():String.

孤云独去闲 2024-11-09 16:58:23

不,不可能。

而且,这是一个非常糟糕的主意。尽量让你的代码没有副作用,即。您不应该编写因调用者的类而故意或意外执行不同的代码。想象一下尝试对这样的代码进行单元测试 - 噩梦。

No, not possible.

Also, it's a really bad idea. try to keep your code free of side-effects, ie. you shouldn't write code that deliberately or accidentally performs differently because of the Class of the caller. Imagine trying to unit-test such code - nightmare.

雨后彩虹 2024-11-09 16:58:23

是的,AS3 getter 和 setter 已经足够灵活,可以通过产生意想不到的副作用来轻松编写难以遵循的代码,而这个想法只会让它变得更加混乱。 getter 或 setter 的全部目的是提供受控的公共访问点——无需从有权访问私有成员的类中调用 getter 或 setter。

Yeah, AS3 getters and setters are already flexible enough to make it easy to write difficult-to-follow code by making them unexpected side effects, and this idea would just make it even more confusing. The whole point of a getter or setter is to provide a controlled public access point--there is no need to call a getter or setter from within the class that has access to the private members.

静若繁花 2024-11-09 16:58:23

这是一个老问题,但如果其他人正在寻找......请尝试以下操作并解析堆栈跟踪以获取调用者

跟踪(新Error().getStackTrace())

This is an old questions but if someone else is looking... try the following and parse the stack trace to get the caller

trace( new Error().getStackTrace() )

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