[問題] 如何理解在class中new自己的意義

作者: Dong0129 (阿東跟上面講的情況一樣b)   2020-06-15 18:07:21
各位版友好,
如標題,請問該如何理解在一個class中new自己?
如:
class test{
test t=new test();
...
}
其中,又看見有些會有getInstance()有些沒有,
請問該如何理解這個函式的作用呢?
作者: james732 (好人超)   2020-06-15 18:31:00
你看到的可能是單例模式(Singleton)可以google一下
作者: ssccg (23)   2020-06-15 20:38:00
先分清楚class和instance在一個class中new自己 → 在一個class定義的程式碼中產生一個這個class的instance另外要注意不要在field宣告或constructor做這件事,顯然會變成無窮遞迴
作者: wawi2 (@@)   2020-06-17 01:02:00
singleton阿
作者: JustinHere (良葛格)   2020-06-17 08:33:00
Factory 之類的模式
作者: new122851 (未若柳絮因風起)   2020-07-11 10:53:00
Singleton, 通常constructor會是private

Links booklink

Contact Us: admin [ a t ] ucptt.com