java 继承与多态练习题目
2016-06-14 16:53
399 查看
//请写出以下程序的运行结果....
public class A {
public String show(D obj) {
return ("A and D");
}
public String show(A obj) {
return ("A and A");
}
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b)); // ①
System.out.println(a1.show(c)); // ②
System.out.println(a1.show(d));// ③
System.out.println(a2.show(b)); // ④
System.out.println(a2.show(c));// ⑤
System.out.println(a2.show(d));// ⑥
System.out.println(b.show(b)); // ⑦
System.out.println(b.show(c)); // ⑧
System.out.println(b.show(d)); // ⑨
}
}
class B extends A {
public String show(B obj) {
return ("B and B");
}
public String show(A obj) {
return ("B and A");
}
}
class C extends B {
}
class D extends B {
}
ans:
A and A
A and A
A and D
B and A
B and A
A and D
B and B
B and B
A and D
public class A {
public String show(D obj) {
return ("A and D");
}
public String show(A obj) {
return ("A and A");
}
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b)); // ①
System.out.println(a1.show(c)); // ②
System.out.println(a1.show(d));// ③
System.out.println(a2.show(b)); // ④
System.out.println(a2.show(c));// ⑤
System.out.println(a2.show(d));// ⑥
System.out.println(b.show(b)); // ⑦
System.out.println(b.show(c)); // ⑧
System.out.println(b.show(d)); // ⑨
}
}
class B extends A {
public String show(B obj) {
return ("B and B");
}
public String show(A obj) {
return ("B and A");
}
}
class C extends B {
}
class D extends B {
}
ans:
A and A
A and A
A and D
B and A
B and A
A and D
B and B
B and B
A and D
相关文章推荐
- javaWeb项目中连接MySQL出现无法加载驱动的问题
- activiti教程(一)eclipse或者myEclipse安装Activiti designer插件
- [疯狂Java]正则表达式:Pattern、Matcher、String对正则表达式的支持
- Ubuntu14.04安装JDK与配置环境变量
- Spring 学习笔记
- 深入理解Java的接口和抽象类
- Jimoshi_Spring 框架学习(一)--Spring实现IOC思想、javabean对象、Spring对象和对象之间关系
- JAVA的静态变量、静态方法、静态类
- Java读写txt文件
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- java并发编程
- 由eclipse工程创建一个Android studio 工程
- java通过JDBC链接SQLSERVER2012
- JAVA学习路线图---(JAVA1234)
- java Date操作
- eclipse快捷键以及使用技巧大全
- spring(13)缓存数据
- java 汉字排序
- 【Spring4揭秘√】BeanFactory与@Configuration基础
- SpringBoot之Creating Asynchronous Methods