在面向?qū)ο蟮恼Z言中,我使用類變量通過增加構(gòu)造次數(shù)和減少破壞次數(shù)來跟蹤當(dāng)前產(chǎn)生了多少個(gè)實(shí)例。我嘗試在go中實(shí)現(xiàn)類似的行為:package entitytype Entity struct { Name string}func New(name string) Entity { entity := Entity{name} counter++ return entity}var counter int = 0func (e *Entity) Count() int { return counter}由于我無法通過析構(gòu)函數(shù)遞減計(jì)數(shù)器,因此該方法起作用了一半。我能以某種方式模仿對(duì)象破壞嗎?我如何正確跟蹤實(shí)例計(jì)數(shù)?
- 2 回答
- 0 關(guān)注
- 195 瀏覽
添加回答
舉報(bào)
0/150
提交
取消