作者Tverous (Orah~)
看板java
標題[問題] math()詢問
時間Sun Jan 29 08:09:00 2017
在這詢問有關Math這個static method
有沒有曾經擁有過constructor
因為在搜尋之後發現它似乎是沒有的
但書上卻這樣寫
But you can restrict other code from instantiating
a non-abstract class by marking the constructor
private. Remember, a method marked private means
that only code from within the class can invoke
the method. A constructor marked private means
essentially the same thing-only code from within
the class can invoke the constructor. Nobody can
say 'new' from outside the class. That's how it works
with the Math class, for example. The constructor
is private, you cannot make a new instance of Math.
The compiler knows that your code doesn't have
access to that private constructor
大概是說Math不能被new是因為他的constructor被設為private
不知道我理解有沒有錯@@
看的是 HeadFirst Java
感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.83.127.79
※ 文章網址: https://www.ptt.cc/bbs/java/M.1485648542.A.C79.html
※ 編輯: Tverous (111.83.127.79), 01/29/2017 08:15:58
→ y3k: 上次我幫人寫工具的經驗是 這種寫法應該是為了好整理跟debug 01/29 10:36
→ ssccg: class一定有ctor,沒定義會變成有default ctor,不想讓人 01/29 12:06
→ ssccg: new就只能設成private,java沒有像C#的static class 01/29 12:09
→ Tverous: 那想請問Math的constructor是不是放在math's class裡? 01/29 12:20
→ Tverous: 畢竟是設為private 01/29 12:21
→ eieio: 當然是放在 Math class 裡 01/29 14:51
推 kyleJ: Math不是static method,而是Math底下有許多static method 01/29 15:15
→ kyleJ: 。Math本身是class,所以也沒有math()這東西 01/29 15:15