[問題] math()詢問

作者: Tverous (Orah~)   2017-01-29 08:09:00
在這詢問有關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
感謝
作者: y3k (激流を制するは静水)   2017-01-29 10:36:00
上次我幫人寫工具的經驗是 這種寫法應該是為了好整理跟debug
作者: ssccg (23)   2017-01-29 12:06:00
class一定有ctor,沒定義會變成有default ctor,不想讓人new就只能設成private,java沒有像C#的static class
作者: Tverous (Orah~)   2017-01-29 12:20:00
那想請問Math的constructor是不是放在math's class裡?畢竟是設為private
作者: eieio (好多目標)   2017-01-29 14:51:00
當然是放在 Math class 裡
作者: kyleJ (資工人)   2017-01-29 15:15:00
Math不是static method,而是Math底下有許多static method。Math本身是class,所以也沒有math()這東西

Links booklink

Contact Us: admin [ a t ] ucptt.com