转换 : 继承 « 类定义 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 类定义 » 继承 
5. 22. 9. 转换
  1. With objects, you can cast an instance of a subclass to its parent class.
  2. Casting an object to a parent class is called upcasting.
Child child = new Child ();
Parent parent = child;

To upcast a Child object, all you need to do is assign the object to a reference variable of type Parent. The parent reference variable cannot access the members that are only available in Child.

Because parent references an object of type Child, you can cast it back to Child. It is called downcasting because you are casting an object to a class down the inheritance hierarchy. Downcasting requires that you write the child type in brackets. For example:

Child child2 = (Childparent;
5. 22. 继承
5. 22. 1. 继承
5. 22. 2. 无障碍
5. 22. 3. 方法重载
5. 22. 4. 关键字的延伸
5. 22. 5. 衍生类
5. 22. 6. The keyword super represents an instance of the direct superclass of the current object.
5. 22. 7. Derived Class Constructors: Calling the Base Class Constructor
5. 22. 8. 覆盖一个基类方法
5. 22. 9. 转换
5. 22. 10. 继承,构造和参数
5. 22. 11. 结合和继承
5. 22. 12. 继承和upcasting 。
5. 22. 13. Overloading a base-class method name in a derived class does not hide the base-class versions.
5. 22. 14. 清理和继承
5. 22. 15. 建立一个多层次集成
5. 22. 16. Demonstrate when constructors are called in a Multilevel Hierarchy
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.