在 v15 中刪除對象在 JSL 中,我可以定義一個類並將其用作實例化對象的模板。
如何刪除對象?
我一直在使用消息 deleteClass,假設它有點用詞不當,實際上它正在刪除對象。
但在第 15 版中,它似乎確實刪除了該類。使用該消息後,我將無法創建該類的任何其他實例。
我可以使用刪除消息,但我認為它只是 deleteClass 的別名,我也有同樣的問題。
刪除類真的是要消滅整個類定義嗎?如果是這樣,刪除對象的正確方法是什麼?
這是一個簡單的測試腳本:
names default to here(1);
defineClass("MyClass",{
_value = .,
_init_ = method({value},
_value = value;
),
getValue = method({},
return(_value)
)
});
// this works
obj = newObject( MyClass( 10 ) );
show(obj:getvalue);
// this works
obj = newObject( MyClass( 20 ) );
show(obj:getvalue);
// there should be no harm deleting
// an object once I am finished with it
obj<<DELETE></DELETE>
回复:在 v15 中刪除對象謝謝大家,我認為有證據表明重新分配對像變量會破壞對象而不是將其作為內存中的孤兒,
回复:在 v15 中刪除對象如果您只是覆蓋變量,它似乎正在覆蓋它。我跑了
Names Default To Here( 1 );
Define Class(
"Test",
nObs = 20;
addition = Method( {x, y},
(x + y) * nObs
);
append = Method( {a, b},
Char( a ) || " + " || Char( b )
);
);
for(i=1, i<=1000000, i++,
clref = New Object( Test() );
);
delete symbols(clref);
在查看 JMP 的內存使用情況時,它並沒有真正移動。我希望如果它沒有真正覆蓋它,如果我有一百萬個,我會看到內存增加。
回复:在 v15 中刪除對象我也不明白怎麼用。 我認為你不需要它:
Define Class(
"aa",
{_init_ = Method( {a}, this:b = a ), //
x=1, //
m1 = Method( {a, b}, a * b )}
);
//
lcaa42 = New Object( aa(42) );
lcaa42:x= Run Program(//
Executable( "PING.EXE" ), //
Options( {"-n 50", "localhost"} ), //
ReadFunction(
Function( {this},
write(this << read);
)
)
);
wait(5); // report about 5 of the 50 pings to the log
// this will destroy the old object and replace it with a
// new one. The log will report the RunProgram object was
// destroyed; there may be a more clever way to witness the
// destruction...best I could come up with...
lcaa42 = New Object( aa(42.1) ); // the old one is gone
lcaa43 = New Object( aa(43) ); // the class still works
show(lcaa42:b,lcaa43:b); // 42.1,43
Show Classes();使用 32 字節數據 ping v1-PC [::1]:
來自 ::1 的回复:時間<1ms
來自 ::1 的回复:時間<1ms
來自 ::1 的回复:時間<1ms
來自 ::1 的回复:時間<1ms
來自 ::1 的回复:時間<1ms
來自 ::1 的回复:時間<1ms
當 RunProgram 對像被銷毀時,外部程序正在生成輸出。
使用 JSL 變量來保存 RunProgram 對象。
(可選)向變量發送消息以管理對象的生命週期。
lcaa42:b = 42.1;
lcaa43:b = 43;
// 類 a
_init_ = 方法({a},這:b = a);
m1 = 方法({a, b}, a * b);
x = 1;
這表明第一個對像是通過為保存它的變量分配一個新值而被刪除的。
如果復制了其他變量(未克隆)
lcaa42b=lcaa42;
然後必須清除所有副本以銷毀對象。
lcaa42b=0;
lcaa42=0;
@EvanMcCorkle
回复:在 v15 中刪除對象我不知道。
在可以“定義類”之前,我使用命名空間創建了它們。
對於命名空間,這不起作用,即將變量設置為缺失與刪除命名空間不同。
ns = newNamespace("xyz");
ns:a = 10;
ns = .;
show namespaces();回复:在 v15 中刪除對象這行得通嗎:obj = .;