2011-02-14 43 views
0

從德爾福背景的,我習慣能夠有類引用/特定的超類的指針,例如:是否可以在AS3中使用特定類引用類型的變量?

#!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; 

這意味着,我沒有投我的變量到具體的班級;他們事先是正確的。這也意味着可以執行編譯時檢查以確保訪問正確的成員,並且如果niceVar傳遞給方法,我不必檢查niceVar實際上是類TClassOne。

#!pas 
procedure test(var x: class of TClassOne); 
begin 
    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; 

所以就像存儲對象可以是用於特定類型和類或它的子類的唯一對象的變量被接受,所以確實「類ACLASS的」允許用於向specfic類的變量限於引用某個類或從中繼承的類。

我希望是有道理不知何故;我不知道整個「SuperClass類」的具體命名。

所以我想要做在AS3一樣具有變量/屬性/參數類型爲Class不切割芥;它就像所有對象變量/屬性/參數都只是Object而不是它們的適當的特定類型。


編輯#1 - 2011-02-14 13:34 語法高亮這裏搞砸;我希望代碼被識別爲Object Pascal。期待this


編輯#2 - 2011-02-14 15:11 這裏是我想在AS3與此達到什麼樣的一個例子。

當前代碼

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

我想怎麼能夠做到

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

有沒有使用`VAR MYVARIABLE任何理由:MyClass的;`? – weltraumpirat 2011-02-14 11:24:50

+0

這是對象,而不是類引用afaik。 – Atorian 2011-02-14 11:36:29

+0

我修正了代碼示例;他們讓它看起來像我需要對象引用,但我希望能夠存儲* class *引用,並將它們限制爲超類X例如。 – Atorian 2011-02-14 11:43:32

回答

0

據我所知,你必須留與Class類型。這是ActionScript限制之一,與Function相同,是所有函數類型的參考。 AS 3.0經常會強制運行時檢查其他語言中的靜態檢查 - 靜態類型檢查並不是最重要的一點。

相關問題