当前位置: 技术问答>java相关
快来看看这道关于overload的考题!!!
来源: 互联网 发布时间:2017-04-03
本文导语: 答案是B、D 能把每个选项都详细些地分析一下吗?为什么选、为什么不选等等。 谢谢 1. public class MethodOver { 2. private int x, y; 3. private float z; 4. public void setVar(int a, int b, float c){ 5. x = a; 6. y = b; 7. z =...
答案是B、D
能把每个选项都详细些地分析一下吗?为什么选、为什么不选等等。
谢谢
1. public class MethodOver {
2. private int x, y;
3. private float z;
4. public void setVar(int a, int b, float c){
5. x = a;
6. y = b;
7. z = c;
8. }
9. }
Which two overload the setVar method? (Choose Two)
A.void setVar(int a, int b, float c){
x = a;
y = b;
z = c;
}
B. public void setVar(int a, float c, int b) {
setVar(a,b,c);
}
C. public void setVar(int a, float c, int b) {
this(a, b, c);
}
D. public void setVar(int a, float b){
x = a;
z = b;
}
E. public void setVar(int ax, int by, float cz) {
x = ax;
y = by;
z = cz;
}
能把每个选项都详细些地分析一下吗?为什么选、为什么不选等等。
谢谢
1. public class MethodOver {
2. private int x, y;
3. private float z;
4. public void setVar(int a, int b, float c){
5. x = a;
6. y = b;
7. z = c;
8. }
9. }
Which two overload the setVar method? (Choose Two)
A.void setVar(int a, int b, float c){
x = a;
y = b;
z = c;
}
B. public void setVar(int a, float c, int b) {
setVar(a,b,c);
}
C. public void setVar(int a, float c, int b) {
this(a, b, c);
}
D. public void setVar(int a, float b){
x = a;
z = b;
}
E. public void setVar(int ax, int by, float cz) {
x = ax;
y = by;
z = cz;
}
|
比较
public void setVar(int a, int b, float c){}
public void setVar(int a, float c, int b){}
这两个方法,你发现有什么不同了吗?
而且在后面public void setVar(int a, float c, int b) 这个里面
setVar(a,b,c);
你又发现什么了吗?
只是个顺序的问题。其实想想就出来了
这就是overload,不需要你自己去找,语言本身就可以识别出他们的不同
public void setVar(int a, int b, float c){}
public void setVar(int a, float c, int b){}
这两个方法,你发现有什么不同了吗?
而且在后面public void setVar(int a, float c, int b) 这个里面
setVar(a,b,c);
你又发现什么了吗?
只是个顺序的问题。其实想想就出来了
这就是overload,不需要你自己去找,语言本身就可以识别出他们的不同
|
overload的概念啊,直接往上套就可以了!
想overload 一个 mothed,方法名称一定是相同的,但是参数一定不可以相同(不是名称不同,而是个数或者类型等等)。
这样,A和E就排除了。
this()的概念属于构造子的范畴,但是这个class里面没有这个构造子,所以,C也排除了。
还有B和D,检查一下,没什么错误,就选喽
想overload 一个 mothed,方法名称一定是相同的,但是参数一定不可以相同(不是名称不同,而是个数或者类型等等)。
这样,A和E就排除了。
this()的概念属于构造子的范畴,但是这个class里面没有这个构造子,所以,C也排除了。
还有B和D,检查一下,没什么错误,就选喽
|
A method can have the same name as another method in the same
class, providing it forms either a valid overload or override.
A valid overload differs in the number or type of its arguments. Differences
in argument names are not significant. A different return type
is permitted, but it is not sufficient by itself to distinguish an overloading
method.
Methods that overload a name are different methods and can coexist
in a single class.
Both overloaded and overloading methods are accessed simply by providing
the correct argument types for the method call.
A valid override has identical argument types and order, identical
return type, and is not less accessible than the original method. The
overriding method must not throw any checked exceptions that were
illegal for the original method.
An overriding method completely replaces the original method unless
the derived class makes specific reference to that original method
using the super.xxx() construction.
An overriding method cannot be defined in the same class as the
method it overrides; rather, it must be defined in a subclass.
The super.xxx() mechanism gives access to an overridden method
from within the subclass that defines the overriding method.
An overridden method is not accessible outside the overriding class.
Virtual method invocation otherwise insures that the behavior associated
with the object class (not with the variable type) will be the
behavior that occurs.
class, providing it forms either a valid overload or override.
A valid overload differs in the number or type of its arguments. Differences
in argument names are not significant. A different return type
is permitted, but it is not sufficient by itself to distinguish an overloading
method.
Methods that overload a name are different methods and can coexist
in a single class.
Both overloaded and overloading methods are accessed simply by providing
the correct argument types for the method call.
A valid override has identical argument types and order, identical
return type, and is not less accessible than the original method. The
overriding method must not throw any checked exceptions that were
illegal for the original method.
An overriding method completely replaces the original method unless
the derived class makes specific reference to that original method
using the super.xxx() construction.
An overriding method cannot be defined in the same class as the
method it overrides; rather, it must be defined in a subclass.
The super.xxx() mechanism gives access to an overridden method
from within the subclass that defines the overriding method.
An overridden method is not accessible outside the overriding class.
Virtual method invocation otherwise insures that the behavior associated
with the object class (not with the variable type) will be the
behavior that occurs.
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。