AS2:是否将在类中声明的函数存储为单独的实例,从而在实例化的每个对象上占用更多内存? [英] AS2: Are functions declared in a class stored as separate instances taking up more memory on each object instantiated?
问题描述
我的问题是有关动作脚本2中的内存使用和对象的信息.如果我有一个类定义:
My question is about memory use and objects in actionscript 2. If I have a class definition:
class test1{
public function one(){trace("Hello");}
public function two(){trace("World");}
}
还有第二个类的定义:
class test2{
static public function one(){trace("Hello");}
static public function two(){trace("World");}
}
然后我执行以下操作:
var object1a =新的test1();
var object1b = new test1();
var object1a = new test1();
var object1b = new test1();
var object2a = new test2();
var object2b = new test2();
var object2a = new test2();
var object2b = new test2();
由于函数不是静态的(可能会复制到每个对象实例中),所以object1a + object1b的大小是否大于object2a + object2b的大小?我没有Actionscript 3来检测内存使用情况,如果很难在AS 2中确定,也许有人可以检查一下AS 3中的使用情况.
Is the size of object1a + object1b greater than the size of object2a + object2b because of the functions not being static (and possibly being copied into each object instantiation)? I do not have Actionscript 3 to detect memory use, maybe someone can check how this behaves in AS 3 if it is difficult to determine in AS 2.
我只是想知道非静态成员函数是否全部都是对单个原型定义的引用,或者是否将它们全部复制到每个函数中,从而有效地使test1与test2的内存使用量增加了一倍.我想象它们被视为引用,然后覆盖它们只是将引用更改为内存中的其他函数,但是我不确定,并且需要澄清一下.
I'm just wondering if non-static member functions are all references to the single prototype definitions, or if they are copied wholesale into each function effectively doubling memory use for test1 vs test2. I imagine they are treated as references and then overriding them simply changes the reference to a different function in memory, but I am not sure and would like a bit of clarification.
谢谢!
推荐答案
非静态属性(别名字段,别名成员变量)对于类的每个对象实例都有其自己的副本.
Non static properties (alias fields, alias member variables) have their own copy for each object instance of a class.
方法,无论是静态的还是静态的,对于每个类仅存在一个副本中.
Methods, whether static or not, only exist in one copy for each class.
如果您考虑一下,那是有道理的:没有理由复制不变的行为.仅状态(变量)会更改.
That makes sense, if you think about it: there's no reason to copy a behaviuor that doesn't change. Only the status (variables) do change.
我能想到的静态方法和非静态方法之间的唯一区别是可见性级别,即非静态方法看到"对象状态,而静态方法则不能,因为它在类级别上起作用.
The only difference I can think about between static and non-static methods is the visibility level, that is a non-static method "sees" the object status, while a static method can't because it works on a class level.
编辑(证明):
AClassStatic.as
class AClassStatic
{
public static function f():Void { return ; }
public static function g():Void { return ; }
public static function h():Void { return ; }
}
AClass.as
class AClass
{
public function f():Void { return ; }
public function g():Void { return ; }
public function h():Void { return ; }
}
test.fla
import AClass
import AClassStatic
var obj1:AClass = new AClass ();
var obj2:AClassStatic = new AClassStatic ();
if ( sizeof(obj1) == sizeof(obj2) )
trace("equal");
我创建了10,000个AClass对象,并测量该可执行文件占用6304 KB,而创建10,000个AClassStatic对象则需要6284 KB.这是不同的,但无关紧要.
I have created 10,000 objects of AClass and measured that the executable occupies 6304 Kbyte, while creating 10,000 AClassStatic objects needs 6284 KB. It is different, but irrelevant.
这篇关于AS2:是否将在类中声明的函数存储为单独的实例,从而在实例化的每个对象上占用更多内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!