多态题目:
题一:
class Parent {
private void method1() {
System.out.println("Parent's method1()");
}
public void method2() {
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent {
public void method1() {
System.out.println("Child's method1()");
}
public static void main(String args[]) {
Parent p = new Child();
p.method2();
}
}
A. compile time error
B. run time error
C. prints: parent’s method2() parent’s method1()
D. prints: parent’s method2() child’s method1()
C is correct.
分析:
The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object.
The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed.
Now from the method2() , there is a call to method1().
1]Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked.
2] Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called.
Thus C is correct answer.
由以上可知,父类方法如果被修饰为private,子类根本无法访问该方法,不具备可见性,所以你在子类中即使写了一个同样的方法(当然修饰级别要提高),也不构成覆盖。
在netbeans下,我测试了,如果你覆写了一个方法,编辑器会提示你加上@override注解,但你在子类中想覆写一个父类的private方法,却没有提示要加上该注解,也可以证明这根本没有构成覆写,所以要覆写的方法必须修饰级别不能为private。
所以猜想子类构成覆写的方法才能以多态的方法调用。
题二:
class Parent {
public static int countA = 10;
public int countB = 20;
public static void methodA() {
System.out.println("methodA() in Parent " + countA);
}
public void methodB() {
System.out.println("methodB() in Parent " + countA + countB);
}
}
public class Child extends Parent {
public static int countA = 30;
public int countB = 40;
public static void methodA() {
System.out.println("methodA() in Child "+ countA );
}
public void methodB() {
System.out.println("methodB() in Child "+ countA + countB);
}
public static void main(String[] args) {
Parent parent = new Child();
parent.methodA();
parent.methodB();
}
}
KEY:
methodA() in Parent 10
methodB() in Child 3040
分析:
对于静态方法,也不构成多态。因为静态方法不依赖于类的实例,根据变量的类型,就可以找到对应的静态方法,无需考虑多态。
题目三:
class Base{
int i = 99;
public void amethod(){
System.out.println("Base.amethod()");
}
Base(){
amethod();
}
}
public class Derived extends Base{
int i = -1;
public static void main(String argv[]){
Base b = new Derived();
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("Derived.amethod()");
}
}
分析:
In this situation a reference to any of the fields such as i will refer to the value in the Base class
直接访问父类的变量,不会构成多态,访问的永远是父类的成员变量,不过为了访问子类的成员变量可以使用方法来间接访问,这样就可以利用多态了。
分享到:
相关推荐
Java ------ 多态作业
JAVA面向对象基础测试题-继承-封装-多态等测试题.docx
黑马程序员 - Java基础教学 - 08 - 面向对象(4)-多态.doc
JAVA面向对象---多态(完结)
Java基础精品课11-多态录屏5.多态调用演示.mp4
Java基础精品课11-多态录屏1.作业讲解1.mp4
Java基础精品课11-多态录屏3.作业讲解3.mp4
Java基础精品课11-多态录屏2.作业讲解2.mp4
Java基础精品课11-多态录屏6.instanceof和转型.mp4
Java基础精品课11-多态录屏4.静态方法不能被覆盖.mp4
C++day15-多态 文档
内容概要: 本文介绍了Java中多态(Polymorphism)的概念和用法。读者将学习什么是多态,为什么需要多态,以及如何在程序中实现多态。此外,还会讨论多态时的一些注意事项和规则。 能学到什么: 理解多态的定义、...
JAVA学习笔记,java知识点总结
【IT十八掌徐培成】Java基础第06天-03.多态-类多态-接口多态-同名属性问题.zip
JAVA多态思维导图,便捷整理思路,多态的好处、多态的语法格式、多态中的两种类型转换:向上转型、向下转型
面向对象--多态.md
C++实验-多态
Java,面向对象编程的第二站--多态和继承,包含一个实验报告word文档(内包括体会和程序简介)和一个源代码压缩包(在Myeclipse 6.0下编译通过)
面向对象练习之三--多态 (抽象方法和虚方法)
第八讲 多态 1、再谈向上类型转换(upcasting) 忘记对象的类型 2、多态机理 方法绑定(method binding) 产生正确的行为 可扩展性 陷阱:置换私有方法 3、抽象类与抽象方法 4、构造方法与多态 ...