`

AS3 dynamic class

    博客分类:
  • Flex
阅读更多
动态类
“动态”类定义在运行时可通过添加/ 更改属性和方法来改变的对象。 非动态类(如 String类)是“密封”类。您不能在运行时向密封类中添加属性或方法。

在声明类时,可以通过使用 dynamic 属性来创建动态类 。例如,下面的代码创建一个名为
Protean 的动态类:

dynamic class Protean
{
  private var privateGreeting:String = "hi";
  public var publicGreeting:String = "hello";
  function Protean()
  {
     trace("Protean instance created");
   }
}


如果要在以后实例化 Protean 类的实例,则可以在类定义的外部向该类中添加属性或方法 。
例如,下面的代码创建 Protean 类的一个实例,并向该实例中添加两个名称分别为 aString和 aNumber 的属性:

  

 var myProtean:Protean = new Protean();
 myProtean.aString = "testing";
 myProtean.aNumber = 3;
 trace(myProtean.aString, myProtean.aNumber); // 测试 3


添加到动态类实例中的属性是运行时实体,因此会在运行时完成所有类型检查。不能向以这种方式添加的属性中添加类型注释。

您还可以定义一个函数并将该函数附加到 myProtean 实例的某个属性,从而向 myProtean实例中添加方法。下面的代码将 trace 语句移到一个名为 traceProtean() 的方法中:

 var myProtean:Protean = new Protean();
 myProtean.aString = "testing";
 myProtean.aNumber = 3;
 myProtean.traceProtean = function ()
 {
    trace(this.aString, this.aNumber);
  };
 myProtean.traceProtean(); // 测试 3


但是,以这种方式创建的方法对于 Protean 类的任何私有属性或方法都不具有访问权限。而且,即使对 Protean 类的公共属性或方法的引用也必须用 this 关键字或类名进行限定。 下面的示例说明了 traceProtean() 方法,该方法尝试访问 Protean 类的私有变量和公共变量。

 myProtean.traceProtean = function ()
 {
    trace(myProtean.privateGreeting); // undefined
    trace(myProtean.publicGreeting); // hello
  };
 myProtean.traceProtean();


这其实和javascript中的机制类似,不同的是在javascript中,可以对任意对象,在任何时候,任意的添加属性和方法。而as3对此做了很大的限制,它们本来也是基于同一标准实现的。但as3已经有些接近强类型语言了。
 
分享到:
评论

相关推荐

    FLASH As3_basic 入门源码

    【标题】"FLASH As3_basic 入门源码"揭示了这个压缩包是关于Adobe Flash平台中ActionScript 3.0(简称AS3)的基础学习资源。ActionScript是一种面向对象的编程语言,广泛用于创建交互式的动画、游戏以及富互联网应用...

    Dynamic Positioning Systems

    A positioning system, such as dynamic positioning (DP) and joystick, is the ‘enduser’ of other equipment on a vessel and plays also the role as an integrating factor for the vessel operation. Rolls-...

    Dynamic Programming in a Generalized Decision Mode

    We present two dynamic programming strategies for a general class of decision processes. Each of these algorithms includes among others the following graph theoretic optimization algorithms as special...

    由static_cast和dynamic_cast到C++对象占用内存的全面分析

    static_cast和dynamic_cast是C++的类型转换操作符。编译器隐式执行的任何类型转换都可以由static_cast显式完成,即父类和子类之间也可以利用static_cast进行转换。而dynamic_cast只能用于类之间的转换。那么dynamic_...

    Best practices for a Hyper-V Dynamic Data Center

    This document provides a general reference for a proof of concept ... This document should be used as a general reference only for HP BladeSystem and Microsoft Hyper-V in a dynamic data center scenario.

    Pro+Spring+Dynamic+Modules+for+OSGi+Service+Platforms

    This book is about OSGi’s role in enterprise Java and how the Spring Dynamic Modules for OSGi (Spring-DM) fulfills this role. I first learned about OSGi when the Eclipse IDE started using it in its ...

    Combinatorial data analysis : Optimization by Dynamic Programming

    Combinatorial data analysis (CDA) refers to a wide class of methods for the study of relevant data sets in which the arrangement of a collection of objects is absolutely central. The focus of this ...

    Creating.Dynamic.UI.with.Android.Fragments.2nd.Edition.1785889

    Dynamically manage fragments using the FragmentTransaction class Learn the application design for communicating between fragments Leverage fragments when implementing applications that take advantage ...

    Fluent.Python.1491946008

    Functions as objects: view Python functions as first-class objects, and understand how this affects popular design patterns Object-oriented idioms: build classes by learning about references, ...

    ActionScript3.0教程

    - AS3中更加严格地遵循了面向对象编程的原则,例如类(`class`)、接口(`interface`)、封装(`public`、`private`等访问修饰符)、继承(`extends`)和多态性等概念得到了加强。 - 类定义格式发生了变化,比如...

    Vue.js绑定HTML class数组语法错误的原因分析

    这里的`dynamicClass`是一个字符串,而`{ isActive: isActiveFlag }`是一个对象,其中`isActiveFlag`是Vue实例中的一个布尔值。Vue.js会将`dynamicClass`字符串作为一个类添加,并根据`isActiveFlag`的值来决定是否...

    as_extensions_flash_lite_digital_home

    3. **ASObject**: This represents a custom object that can contain properties and methods. It is useful for creating complex data structures and behavior patterns within the extension. #### ...

    Research on dynamic update transaction for Java classes

    Dynamic software updating is critical for many systems that must provide continuous service. In addition, the Java language is gaining increasing popularity in developing distributed systems. Most ...

    AS3.0的关键字和保留字

    5. **class**:用于定义类。 6. **const**:用于声明常量。 7. **continue**:用于跳过当前循环迭代中的剩余部分并继续下一次迭代。 8. **default**:在switch语句中表示默认情况。 9. **delete**:用于删除对象属性...

    Dynamic T-SQL

    public class SearchOrders { public void Execute() { string sql = "SELECT * FROM Orders WHERE OrderDate >= @startDate AND OrderDate ; using (SqlConnection connection = new SqlConnection("Data ...

    JAVA源码文本-dynamic-java-compiler:DynamicJavaCompiler-根据需要从文本源编译Java代码段

    JAVA原始码什么是dynamic-java-compiler? Dynamic-java-compiler是一个库,该库允许用户动态编译和执行任何Java源代码。...as String Class< Runnable > clazz = compiler . compile( null , " Test01

    vb.net 调用DLL

    Dim classtemp As Type = asm.GetType("ClassDll.TestClass") Dim obj As Object = asm.CreateInstance(classtemp.FullName) Dim me_Info As System.Reflection.MethodInfo = classtemp.GetMethod("Add") Dim ...

Global site tag (gtag.js) - Google Analytics