2009-12-16 63 views
0

如果Obj是本地的,則下面的新函數不起作用。如果我從/本地刪除它的作品。那麼該怎麼做才能使它與本地的Obj一起工作,謝謝?當然不難爲你。Rebol和/ local對象

Person: make object! [ 

Person: func[FirstName LastName][ 
    Self/FirstName: FirstName 
    Self/LastName: LastName 
    Print Self/FirstName 
    Print Self/LastName 
] 
FirstName: "" 
LastName: "" 

] 



new: func[Class [Object!] Param-Block [block!] /local Obj][ 
    Constructor: to-word pick pick Class 1 2 
    Obj: Make Class [] 
    params: copy "" 
    foreach param Param-Block [ 
     if string? param [ 
     param: rejoin [{"} param {"}] 
     ] 
     append params param 
     append params " " 
    ] 
    do rejoin [{do get in Obj Constructor} { } params] 
    Obj 
] 
;FOR TEST 
John: new Person["John" "Doe"] 

還會送

>> probe John 
make object! [ 
    Person: func [FirstName LastName][ 
     Self/FirstName: FirstName 
     Self/LastName: LastName 
     Print Self/FirstName 
     Print Self/LastName 
    ] 
    FirstName: "" 
    LastName: "" 
] 
>> 

也就是說名字和姓氏是 「」,而不是 「約翰」, 「李四」

+0

它擔任預計當我嘗試它。你遇到什麼問題? – 2009-12-17 11:13:35

+0

你已經設置了John在測試用例中返回的新值,它是值爲{「John」「Doe」}的字符串。 – 2009-12-17 11:16:36

+0

複製錯誤版本,以便在新的版本中返回Obj。 我的問題是: – 2009-12-17 23:28:00

回答

3

這工作...

rebol [] 

    Person: make object! [ 
     Person: func [ names] [ 
      Self/FirstName: names/1 
      Self/LastName: names/2 
     ] 
     FirstName: copy "" 
     LastName: copy "" 
    ] 

    new: func [class [object!] param-block [block!] 
     /local obj constructor 
    ] [ 
     constructor: second first class 
     obj: make class [] 
     do get in obj constructor copy param-block 
     obj 
    ] 

    test: new person [ "John" "Doe" ] 
    probe test 

    make object! [ 
     Person: func [names][ 
      Self/FirstName: names/1 
      Self/LastName: names/2 
     ] 
     FirstName: "John" 
     LastName: "Doe" 
    ] 

    test2: new person [ "Dick" "Nixon" ] 
    probe test2 

    make object! [ 
     Person: func [names][ 
      Self/FirstName: names/1 
      Self/LastName: names/2 
     ] 
     FirstName: "Dick" 
     LastName: "Nixon" 

] 
+0

非常感謝你:) – 2010-01-04 15:26:20