[问题] 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