※ 引述《macbuntu (邀怪)》之銘言:
: ※ 引述《macbuntu (邀怪)》之銘言:
: : 剛剛看到這個寫法真是大驚, 這竟然是合法的語法:
: : public int func()[] {
: : return new int[] { 1, 2, 3 };
: : }
: 剛剛玩了一下, 發現還可以混用:
: public int[][] func() { ... }
: public int[] func() [] { ... }
: public int func() [][] { ... }
: 上面這三種寫語意一模一樣, 都是回應 int[][]
: 尤其第二種, 那樣寫實在是太超過了 Orz
在 Java Language Spec 中找到說明了:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#38703
"For compatibility with older versions of the Java platform,
a declaration form for a method that returns an array is allowed
to place (some or all of) the empty bracket pairs that form
the declaration of the array type after the parameter list. This is
supported by the obsolescent production:
MethodDeclarator:
MethodDeclarator [ ]
but should not be used in new code."
一切都是為了與舊版相容,
但老話一句,可以那樣用不代表就真的應該那麼用。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.175.148.198