开发者

Is it possible to have a variable with specific class reference type in AS3?

开发者 https://www.devze.com 2023-02-11 10:07 出处:网络
Coming from a Delphi background, I am used to be able to have class references/pointers of a specific superclass, example:

Coming from a Delphi background, I am used to be able to have class references/pointers of a specific superclass, example:

#!pas
var
  niceVar: class of TClassOne; // Delphi style
  badVar: class; // Only? allowed AS3 style to the best of my knowledge

begin
  niceVar := x;
  badVar := x;

  niceVar.staticMethodSpecificToTClassOne;
  TClassOne(badVar).staticMethodSpecificToTClassOne;
end;

What this means is that I don't have to cast my variables to a specific class; they are of the correct Class beforehand. This also means that compile-time checking can be performed to make sure proper members are being accessed and I don't have to check that niceVar is in fact of class TClassOne if niceVar was passed to a method.

#!pas
procedure test(var x: class of TClassOne);
beg开发者_Python百科in
  x.someStaticMethod(true);
end;

// An entry point
var
  niceVar: TClassTwo; // Does not inherit from TClassOne

begin
  test(niceVar); // Error - niceVar does belong to the TClassOne "family"
end;

So just like a variable storing an object can be for a specific type and only objects of that class or subclasses of it are accepted, so to does "class of AClass" allow for variables of a specfic class to be limited to references to a certain class or those inherited from it.

I hope that makes sense somehow; I don't know of specific nomenclature for the whole "class of SuperClass" thing.

So I'd like to do the same in AS3 as having variables/properties/parameters of type Class does not cut the mustard; its kind of like having all object variables/properties/parameters simply being Object instead of their proper, specific type.


Edit #1 - 2011-02-14 13:34 Syntax highlighting is messed up here; I want the code to be recognized as Object Pascal. Looking forward to this.


Edit #2 - 2011-02-14 15:11 Here is an example of what I would like to achieve with this in AS3.

Current code

public function set recordClass(aRecordClass: Class): void 
{
  if (!extendsClass(aRecordClass, TRecord)) 
  {
    throw new Error("TDBTable - Invalid record class passed.");
      return;
  }
  _recordInstance = new aRecordClass(this); // Compiler has no idea of the classes constructor signature, but allows this regardless.
}

What I'd like to be able to do

public function set recordClass(aRecordClass: TRecordClass): void 
{
  _recordInstance = new aRecordClass(this); // Compiler will know that I am creating a TRecord
}


AFAIK, you have to stay with Class type. This is one of ActionScript limitations, same as Function is reference for all function types. AS 3.0 quite often forces run-time checks where in other languages were static checks - static type checking is not its strongest point.


I guess you could use getDefinitionByName

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号