AS3 analog anonymous class (with type)

2010-04-15  来源:本站原创  分类:Flash  人气:229 

Java anonymous classes to use them in a very flexible, it can effectively reduce the number of classes, AS3 not directly support the anonymous class implements, but it supports the closure, can make use of closure to simulate Java's anonymous class implementation:

The interface to achieve the anonymous class

package zzx.test.flex
{
        public interface Interface
        {
                function i1():void;

                function i2(name:String):void;

                function i3(value:int):int;

                function i4():void;

        }
}

Write a helper class that implements the interface

package zzx.test.flex {

        public class InterfaceAnonymouse implements Interface {

                private var _i1:Function;
                private var _i2:Function;
                private var _i3:Function;
                private var _i4:Function;

                private function anyCall(func:Function, ...params):*{
                        if(func == null) return null;
                        return func.call(this, params);
                }

                public function i1():void {
                        anyCall(_i1, arguments);
                }

                public function i2(name:String):void {
                        anyCall(_i2, arguments);
                }

                public function i3(value:int):int {
                        return anyCall(_i3, arguments);
                }

                public function i4():void {
                        anyCall(_i4, arguments);
                }

                public function InterfaceAnonymouse(i1:Function, i2:Function, i3:Function, i4:Function) {
                        this._i1=i1;
                        this._i2=i2;
                        this._i3=i3;
                        this._i4=i4;
                }

        }
}

Anonymous class analog

public function testInterface():void {

        var obj:Object = new Object();
        obj.name = "mysh";

        var inter:Interface=new InterfaceAnonymouse(function():void {
                        Alert.show("i1");
                }, function(name:String):void {
                        Alert.show("hello, "+name);
                }, function(value:int):int{
                        return (value+100);
                }, null);

        inter.i1();
        inter.i2(obj.name);
        Alert.show(inter.i3(200).toString());
        inter.i4();
}

Summary

Advantage

  • Style and Java code to achieve a similar way

Shortcoming

  • Interfaces need to write a helper class corresponding
  • Closure method does not name
  • Method closures lack of integrity constraints, such as the closure method parameter declaration error can be found at run time

To use a more flexible way, you can look at AS3 anonymous class analog (no type)
If there is a better way which my dear friend, welcome Paizhuan

相关文章
  • AS3 analog anonymous class (no type) 2010-04-16

    Before writing the article, describes the realization of anonymous classes AS3 simulation method (AS3 anonymous class analog (with type)), which despite some of the advantages of static languages, where the type, style, fairly elegant, but it is a cl

  • AS3 analog anonymous class (with type) 2010-04-15

    Java anonymous classes to use them in a very flexible, it can effectively reduce the number of classes, AS3 not directly support the anonymous class implements, but it supports the closure, can make use of closure to simulate Java's anonymous class i

  • AS3 analog anonymous class 2010-04-15

    Java anonymous classes to use them in a very flexible, it can effectively reduce the number of classes, AS3 not directly support the anonymous class implements, but it supports the closure, can make use of closure to simulate Java's anonymous class i

  • as3 how to limit the type of text input? 2010-08-20

    How to limit the type of text input ? We can also use the TextField property of their input limit (TextField.restrict) To restrict the user's input type. And where this property is very flexible , It can accept regular expressions, so it can be done

  • anonymous class 2010-03-29

    anonymous classes A type of inner class that has no name that you define right in the middle of a method (where static init blocks and instance init blocks count as methods). You define it, and create an object of that type as a parameter all in one

  • JUnit framework for design and use of design patterns 2009-10-27

    JUnit framework for design and use of design patterns Translation: Yong-Jun Hu hu.yong.jun @ ctgpc.com.cn be added 〔〕 Original: JUnit A Cook's Tour, see www.junit.org JUnit framework for design and use of the Mode 1 Translation: Yong-Jun Hu hu.yong.j

  • Java 7 of the closure and the merits of Lambda Expressions 2010-06-18

    Oracle released a few days before the official closure of Java 7 and the first realization of Lambda expressions, which are basically the final version in the style of the official. After reading this realization, my first feeling is "ugly" and,

  • prototype.js1.4 Edition Developer Guide 2010-08-21

    Some useful function in this library with many predefined objects and utility functions, such stuff was clearly aimed at you from some repetitive typing freed. Using $ () method $ () Method is used too frequently DOM document.getElementById () method

  • php class transcoding 2010-10-15

    Charset::convert(string input,string incharset,string outcharset) Charset::unescape(string escaped,string outcharset) Charset::escape(string string,string incharset) Charset::jsondecode(string encoded,string outcharset) Charset::jsonencode(mix value,

  • [Transfer] Why can not order through the array for all the elements? (Array design principle) 2011-01-11

    http://sban.biz/95 The subject a little plain, but easy to talk to understand. If I tell you, we can not guarantee that any code in order to traverse all the elements of an array, you will certainly refute me, because obviously to be used for ah! But

  • 我如何发现Facebook服务器中的远程代码执行漏洞:OpenID中的XML外部实体扩展漏洞 2014-01-03

    大家好!首先我做一下自我介绍.我叫ReginaldoSilva,是一名巴西籍的计算机工程师.最近我的工作与信息安全有关,尤其是在Web应用程序安全性的方面.如果可以的话,我很乐意给大家演示如何入侵网站和应用程序.我的主页上有一些相关信息,欢迎大家浏览. 今天,我想讲一下我发现一个影响Facebook的远程漏洞代码的过程.正如一般的故事开头那样,这一过程也是在很长时间以前就开始了(实际上仅仅是一年多,但我一然感觉很漫长).如果你觉得这个话题很有趣,或者想让我帮你在你的(或你公司的)代码中做一些有关

  • C#匿名方法与Delegate类型转换错误分析 2014-08-21

    这篇文章主要介绍了C#匿名方法与Delegate类型转换错误,较为详细的分析了C#匿名方法的用法及Delegate类型转换错误问题解决方法,具有一定的实用价值,需要的朋友可以参考下 本文实例分析了C#匿名方法与Delegate类型转换错误.分享给大家供大家参考.具体分析如下: 问题描述 C#2.0出现了匿名方法, 这在一定程度上节省了我们维护代码上下文的精力, 也不需要思考为某个方法取什么名字比较合适. 在FCL的一些方法中要求传入一个Delegate类型的参数, 比如Control.Invok

  • 2000个软件开发领域的高频特殊词及精选例句(七) 2015-03-20

    superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1701.单词 jpql 的匹配文本: You will learn how to implement the various database operations using JPA's EntityManager interface and JPQL (which is similar to HQL) Pro Spring 3 In another situat

  • as3: type conversion. literals. constants. operator. statements (for in and for each in) 2010-11-09

    Let me talk about data types : Primitive data types include Boolean.int.Null.Number.String.uint and void.ActionScript Core classes also define the following complex data types :Object.Array.Date.Error.Function.RegExp. XML And XMLList. 1. Type convers

  • The type of reflection 0 AS3.0 2011-01-13

    The type of reflection 0 AS3.0 Author: dmh2002 Posted on :2008-7-22 Category: AS3 experience / skills / game Views: 263 Get the class name: pass into an object, returns the full name of the class var loader: URLLoader = new URLLoader (); var classNam

  • AS3 event type (as3 development technology Daquan reading notes) 2011-05-10

    Event Event Type: ACTIVATE Activate event DEACTIVATE event object Sleep ADDED visual objects to add an event ADDED_TO_STAGE event added to the stage of visual objects REMOVED remove events visual objects REMOVED_FROM_STAGE visual objects removed from

  • AS3 with javascript interaction 2009-03-25

    Also diverted to the HA, the longer the front of the shop more, hoping to calm down and put points trenches deepened. At AS2, the access to external variables passed to the flash can be accessed only through _root.value that simultaneously simply thr

  • ajax Analog comet 2009-05-27

    Analog data server push, nothing to add to that look at the code I wrote is very easy to understand, please write a lot of bad advice servlet: package com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException;

  • Anonymous classes on Java, Some Thoughts on 2010-02-04

    2009-12-04 Anonymous classes on Java, Some Thoughts on Keywords: right java anonymous classes in Some Thoughts Introduction Java's anonymous classes are more interesting as a programming approach, in the swing on the registration of listeners, we oft

  • Anonymous inner classes 2010-03-29

    1, interface method Anonymous inner class is the abstract classes and interfaces based on the concept developed. The main objective is to reduce the class definition, as the code shown below: Do not use anonymous inner classes are now complete: inter