[問題] math()詢問

看板java作者 (Orah~)時間8年前 (2017/01/29 08:09), 8年前編輯推噓1(107)
留言8則, 5人參與, 最新討論串1/1
在這詢問有關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

01/29 10:36, , 1F
上次我幫人寫工具的經驗是 這種寫法應該是為了好整理跟debug
01/29 10:36, 1F

01/29 12:06, , 2F
class一定有ctor,沒定義會變成有default ctor,不想讓人
01/29 12:06, 2F

01/29 12:09, , 3F
new就只能設成private,java沒有像C#的static class
01/29 12:09, 3F

01/29 12:20, , 4F
那想請問Math的constructor是不是放在math's class裡?
01/29 12:20, 4F

01/29 12:21, , 5F
畢竟是設為private
01/29 12:21, 5F

01/29 14:51, , 6F
當然是放在 Math class 裡
01/29 14:51, 6F

01/29 15:15, , 7F
Math不是static method,而是Math底下有許多static method
01/29 15:15, 7F

01/29 15:15, , 8F
。Math本身是class,所以也沒有math()這東西
01/29 15:15, 8F
文章代碼(AID): #1OZJAUnv (java)
文章代碼(AID): #1OZJAUnv (java)