關(guān)于類的實(shí)例化
調(diào)用 Calendar 類的 getInstance() 方法獲取一個(gè)實(shí)例這句話有點(diǎn)看不懂,,請(qǐng)問為啥調(diào)用這個(gè)方法就可以得到一個(gè)實(shí)例,不是要通過new 得到實(shí)例嗎??
調(diào)用 Calendar 類的 getInstance() 方法獲取一個(gè)實(shí)例這句話有點(diǎn)看不懂,,請(qǐng)問為啥調(diào)用這個(gè)方法就可以得到一個(gè)實(shí)例,不是要通過new 得到實(shí)例嗎??
2016-07-23
舉報(bào)
2016-07-23
Calendar不能用new的方式去實(shí)例化,你可以看一下這個(gè)類的源碼:
/**
???? * Constructs a new instance of the {@code Calendar} subclass appropriate for the
???? * default {@code Locale} and default {@code TimeZone}, set to the current date and time.
???? */
??? public static synchronized Calendar getInstance() {
??????? return new GregorianCalendar();
??? }
可以看到,當(dāng)你調(diào)用它的getInstance()方法后,它會(huì)返回這個(gè)類的實(shí)例。