Question 44
Click the Exhibit button.
1. public class A {
2. public String doit(int x, inty) {
3. return “a”
4. }
5.
6. public String doit(int... vals) {
7. return “b!”
8. }
9. }
Given:
25. Aa=newA();
26. System.out.println(a.doit(4, 5));
What is the result?
A. Line 26 prints “a” to System.out
B. Line 26 prints !(R)b” to System.out
C. An exception is thrown at line 26 at runtime.
D. Compilation of class A will fail due to an error in line 6.
Answer: A问题:如何判断什么时候调用doit(int x,int y)而什么时候调用doit(int …vals)呢???
谢谢
Click the Exhibit button.
1. public class A {
2. public String doit(int x, inty) {
3. return “a”
4. }
5.
6. public String doit(int... vals) {
7. return “b!”
8. }
9. }
Given:
25. Aa=newA();
26. System.out.println(a.doit(4, 5));
What is the result?
A. Line 26 prints “a” to System.out
B. Line 26 prints !(R)b” to System.out
C. An exception is thrown at line 26 at runtime.
D. Compilation of class A will fail due to an error in line 6.
Answer: A问题:如何判断什么时候调用doit(int x,int y)而什么时候调用doit(int …vals)呢???
谢谢
}
public void paint(String str){
}和上面是一个道理
}
public void paint(String str){
}和上面是一个道理
public String doit(int x, inty)
{
return “a”
}
public String doit(int... vals)
{
return “b!”
} 先匹配参数类型,然后看参数的个数,调用对应的方法
String invade(short ships) { return "a few"; }
String invade(short... ships) { return "many"; }
}
class Defender {
public static void main(String [] args) {
System.out.println(new Alien().invade((short)7));
} }
相同的The String invade(short... ships) can also match the call, but it doesn't do so exactly ... so if you removed invade(short ships) method then invade(short... ships) would would be invoked.Does that clear things up?