[AS3]在反射中使用静态方法
Posted 弃天笑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AS3]在反射中使用静态方法相关的知识,希望对你有一定的参考价值。
个人比较喜欢用xml配置一些flash中使用的信息,尤其是利用反射(java感染的习惯的),主要是因为比较多是做架构,讲究的通用性
在AS3中,类的实例是不能使用该类中的静态方法的。只能通过类名来调用静态方法。所以利用反射使用一个类的静态方法时候,就可以少一步啦,直接反射出来的class,再调用静态方法就可以了。
在程序中直接输出类名,得出的是class对象。下面还是看看代码吧
import
flash.display.Sprite;
import
org.sujun.StaticClass;
![](https://image.cha138.com/20210514/22bac210bf194cd0acd3a3b971a24d3d.jpg)
public
class
Main
extends
Sprite
{
private var str:StaticClass;
public function Main()
{
var st:Class = getDefinitionByName("org.sujun.StaticClass") as Class;
trace("==" + st);
trace(st.name);
st["staticTest"]();
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
}
}
下面是静态属性的方法和类
/**
* @(#)StaticClass.as
*
* @author soda.C E-mail:sujun10@21cn.com
* @version 1.0
* <br>Copyright (C), 2007 soda.C
* <br>This program is protected by copyright laws.
* <br>Program Name:Soda.API
* <br>Date:2008-4-24
*/
package org.sujun
{
public class StaticClass
{
public static var name:String = "soda";
public static function staticTest():void
{
trace("staticTest");
}
public function test():void
{
trace("test");
}
}
}
你复制那里代码就可以,很简单,呵呵
代码下载
在AS3中,类的实例是不能使用该类中的静态方法的。只能通过类名来调用静态方法。所以利用反射使用一个类的静态方法时候,就可以少一步啦,直接反射出来的class,再调用静态方法就可以了。
在程序中直接输出类名,得出的是class对象。下面还是看看代码吧
![](https://image.cha138.com/20210514/22bac210bf194cd0acd3a3b971a24d3d.jpg)
![](https://image.cha138.com/20210514/22bac210bf194cd0acd3a3b971a24d3d.jpg)
![](https://image.cha138.com/20210514/22bac210bf194cd0acd3a3b971a24d3d.jpg)
![](https://image.cha138.com/20210514/22bac210bf194cd0acd3a3b971a24d3d.jpg)
![](https://image.cha138.com/20210514/4cfeea9d52e84890be435ed84a716b36.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/652c4450e87c4b58bdc1a6de68e2bc6a.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/6176027ddb6b4f6dbdc238d2cda983b4.jpg)
![](https://image.cha138.com/20210514/ce4bbcd150574fdb8b62e63dfa640841.jpg)
下面是静态属性的方法和类
![](https://image.cha138.com/20210514/4cfeea9d52e84890be435ed84a716b36.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/ce4bbcd150574fdb8b62e63dfa640841.jpg)
![](https://image.cha138.com/20210514/22bac210bf194cd0acd3a3b971a24d3d.jpg)
![](https://image.cha138.com/20210514/4cfeea9d52e84890be435ed84a716b36.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/652c4450e87c4b58bdc1a6de68e2bc6a.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/652c4450e87c4b58bdc1a6de68e2bc6a.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/6176027ddb6b4f6dbdc238d2cda983b4.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/652c4450e87c4b58bdc1a6de68e2bc6a.jpg)
![](https://image.cha138.com/20210514/200a451c07564abe90bbd923880ba7c8.jpg)
![](https://image.cha138.com/20210514/6176027ddb6b4f6dbdc238d2cda983b4.jpg)
![](https://image.cha138.com/20210514/6176027ddb6b4f6dbdc238d2cda983b4.jpg)
![](https://image.cha138.com/20210514/ce4bbcd150574fdb8b62e63dfa640841.jpg)
![](https://image.cha138.com/20210514/22bac210bf194cd0acd3a3b971a24d3d.jpg)
你复制那里代码就可以,很简单,呵呵
![下载文件](http://www.sujun.org/images/download.gif)
以上是关于[AS3]在反射中使用静态方法的主要内容,如果未能解决你的问题,请参考以下文章