作者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://webptt.com/cn.aspx?n=bbs/java/M.1485648542.A.C79.html
※ 编辑: Tverous (111.83.127.79), 01/29/2017 08:15:58
1F:→ y3k: 上次我帮人写工具的经验是 这种写法应该是为了好整理跟debug 01/29 10:36
2F:→ ssccg: class一定有ctor,没定义会变成有default ctor,不想让人 01/29 12:06
3F:→ ssccg: new就只能设成private,java没有像C#的static class 01/29 12:09
4F:→ Tverous: 那想请问Math的constructor是不是放在math's class里? 01/29 12:20
5F:→ Tverous: 毕竟是设为private 01/29 12:21
6F:→ eieio: 当然是放在 Math class 里 01/29 14:51
7F:推 kyleJ: Math不是static method,而是Math底下有许多static method 01/29 15:15
8F:→ kyleJ: 。Math本身是class,所以也没有math()这东西 01/29 15:15