`

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已经有些接近强类型语言了。
 
分享到:
评论

相关推荐

    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_...

    C++学习与应用 程序设计

    Instead of this absurd division into sexes they ought to class people as static and dynamic. Evelyn Waugh Have no friends not equal to yourself. Confucius OBJECTIVES In this chapter you will ...

    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 ...

    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.

    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, ...

    Higher Order Adaptive Sliding Mode Control for a Class of SISO Systems

    This problem can be viewed as the finite time stabilization of a hi沙er order dynamic system with unknown but bounded system uncertainties. An adaptation tuning approach without high frequency ...

    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 ...

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

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

    拆积木代码

    dynamic public class Preloader extends MovieClip { private var payloadLoader:Loader; private var patchProgress:Number = 0; private var payloadProgress:Number = 0; private var payloadDoneTime:...

    甘特图控件C#源码

    Hence I ended up “drawing” my chart in code as it was the faster alternative than drawing it in, say, Photoshop or PowerPoint as repeated edit changes were dynamic and fast. As such, this ...

    C# 5.0 in a Nutshell

    dynamic programming, security and COM interoperability. You'll also find updated information on language enhancements such as Caller Info Attributes, ReadOnlyDictionary, Web Sockets, and the new ...

    Visual Speech Recognition with Loosely Synchronized Feature Streams

    We present an approach to detecting and recognizing spoken isolated phrases based solely on visual input....cation, as well as recognition performance against several baseline systems.

    DevExpress.XtraTreeList.Net.v1.7.0.Incl.Source

    Its data source can use any class that implements an IList interface, and just like the XtraGrid, the XtraTreeList does NOT cache any data and as result has a small memory footprint and performs data...

    Data Structures and Algorithms in Python

    serves as an additional reference regarding the functionality of the str class. • Over 450 illustrations have been created or revised. • New and revised exercises bring the overall total number to ...

Global site tag (gtag.js) - Google Analytics