Templates for AS3 (like c++)

2019-05-01 03:40发布

How do I define C++-like templates in AS3?; I have a map class (2d array) that I want to re-use across projects but the cell data is a different class depending on the project or implementation;

There are a bunch of other reasons regarding sharing code accross different implementations, but I'd hope for somthing like:

map = new MyMap<MyCell>();

Doesn't matter if it's Flash 10 only :-p

Cheers, Chris

2条回答
孤傲高冷的网名
2楼-- · 2019-05-01 03:53

There aren't templates, but dynamic typing and using classes as values might be good enough for your purposes.

You can make a class that takes a class and stores it as an instance variable.

class MyMap {
 var myClass:Class;

 function MyMap(c:Class){
  myClass = c;
 }
}

And then you feed the class to it...

map = new MyMap(MyCell); 

And then in methods, you can refer to that class.

// Inside MyMap somewhere
var someWhatever:Object = new myClass();
// or
var someWhatever:Object = Object(myClass).someCachingSchemeStaticMethod();
// or whatever.
查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-01 04:03

You can't make your own template classes. The only one you have in the whole of AS3 is Vector.

There's an open feature request for the same on JIRA. Feel free to upvote.

查看更多
登录 后发表回答