石大远程Java语言程序设计在线考试第二题.pdf
文本预览下载声明
中国石油大学(北京)远程教育学院
期 末 考 试
《 Java 语言程序设计 》
学习中心:通州 _ 姓名: _程潇 学号: _117910_
一、简答题
1 什么是多态性 ? 方法的重载和覆盖有何区别 ? 阅读下列代码,指出其中存在的重载和覆
盖, 写出输出结果是什么?解释为什么这样输出 ? (15 分 )
class Class1
{
public void find() {
System. out .println( Class1.find );
}
}
class Class2 extends Class1
{
public void find() {
System. out .println( Class2.find );
}
}
class Class3 {
public void get(Class1 one) {
System. out .println( get(Class1) );
one.find();
}
public void get(Class2 two) {
System. out .println( get(Class2) );
two.find();
}
}
public class Test1
{
public static void main(String[] args)
{
Class1 one = new Class2();
Class3 three = new Class3();
three.get(one);
}
}
1 什么是多态性 ? 方法的重载和覆盖有何区别 ? 阅读下列代码,指出其中存在的重载和覆
盖, 写出输出结果是什么?解释为什么这样输出 ? (15 分 )
多态性: 指允许不同类的对象对同一消息做出响应。 即同一消息可以根据发送对象的不
精选文库
同而采用多种不同的行为方式。(发送消息就是函数调用)
方法的重载和覆盖的区别:
重载
public void println(int i);
public void println(char c);
public void println(String s);
覆盖
public class Employee {
String name;
int salary;
public String getDetails() {
return Name: + name
显示全部