Java中多态的实例
2015-06-03 12:43
411 查看
public class cf { /** * 实际上这里涉及方法调用的优先问题, * 优先级由高到低依次为:this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)。 */ 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)); // ① A and A System.out.println(a1.show(c)); // ② A and A System.out.println(a1.show(d)); // ③ A and D System.out.println(a2.show(b)); // ④ B and A System.out.println(a2.show(c)); // ⑤ B and A System.out.println(a2.show(d)); // ⑥ A and D System.out.println(b.show(b)); // ⑦ B and B System.out.println(b.show(c)); // ⑧ B and B System.out.println(b.show(d)); // A and D } } class A { public String show(D obj) { return ("A and D"); } public String show(A obj) { return ("A and A"); } } 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 { }
复制去Google翻译翻译结果
搜索
复制
相关文章推荐
- 疯狂Java学习笔记(70)-----------挚爱Java
- selenium登录脚本
- java个人学习笔记:类属性,类方法
- weblogic部署struts2项目访问action404错误
- java调用c dll,指针参数和结构体参数搞定(转)
- 关于spring管理hibernate事物
- Java for LeetCode 136 Single Number
- Spring整合Hibernate的时候使用hibernate.cfg.xml
- java中计算出差天数
- Java for LeetCode 135 Candy
- struts2 jsp提交到action出现中文乱码问题
- java个人学习笔记:多态
- 二叉树的存储结构
- Eclipse下导入外部jar包的3种方式
- struts1简单入门使用实例
- 单元测试之TestNG与spring集成
- java内存分配 常量池详解
- 超详细在Ubuntu下安装JDK图文解析
- Unsupported major.minor version 51.0(jdk版本错误)
- java基础入门-泛型(1)-为什么需要使用泛型?