继承,转型
2016-03-08 13:46
99 查看
package juxing; public class TestJiCheng { public static void main(String[] args) { Father f=new Father("父亲"); //f.setName("父亲"); f.setAge(50); f.work(); System.out.println(); Son s=new Son(); //s.setName("儿子"); s.setAge(20); System.out.println("儿子"+s.getAge()+"岁"); System.out.println(); s.work(); s.sing(); System.out.println(); System.out.println(); //转型 //向上转型,子类到父类 Father f1=new Son(); System.out.println("名字"+s.getName()); f1.work();//调用子类的work System.out.println(); System.out.println("向下转型,父类转子类"); //向下转型,父类转子类 //Son s1=(Son) new Father("父亲");子类有的父类不一定有 Son s1=(Son)f1; s1.work(); System.out.println(); //调用, System.out.println("类型不确定,用Object,先向上全部转成父类,再向下转型。"); Father f2=(Father)Son.getDate(2); f2.work(); } }
View Code
相关文章推荐
- 华为oj 迭代器
- iOS面试题纪录
- javascript之window对象
- Maven + Spring 简单示例
- C声明阅读
- UML类图关系
- CAMediaTimingFunction相关
- 深入探索透视投影变换
- processor register
- Re-Learn C(The C Programming Language.2Nd)
- 磁盘和文件系统
- 初识Github
- How browsers work(浏览器是如何工作的)
- Android 动态加载 (三) PAK 详解
- 10027---关于<:if>没有<c:else>解决方案
- HBase总结(6)--比较过滤器
- iOS Apps核心对象
- Property Declaration Attributes
- iOS分类和扩展(Categories和Extensions)
- volatile