JAVA向上转型的简单示例
2013-08-11 21:34
225 查看
建立父类Person
子类Student
主函数:
输出结果:
My name is:nullMy age is:0
My address is:null
address输出null是因为调用的introduce是子类的而非父类的。
class Person{ String name; int age; void introduce(){ System.out.println("My name is:"+name+"My age is:"+age); } }
子类Student
class Student extends Person{ String address; void study(){ System.out.println("Study ING"); } void introduce(){ super.introduce(); System.out.println("My address is:"+address); } }
主函数:
//一个引用能够调用哪些成员 变量和函数,取决于这个引用的类型,也就是指下文中的引用p能够调用哪些变量和函数取决于Person当中包含什么变量和函数 class Test{ public static void main(String args []){ Person p=new Student();//向上转型 Student s=(Student)p;//向下转型(需先向上转型) p.introduce();//调用的子类的introduce } }
输出结果:
My name is:nullMy age is:0
My address is:null
address输出null是因为调用的introduce是子类的而非父类的。
相关文章推荐
- Java之向上转型,向下转型简单理解和实例
- Java 转型(向上或向下转型)详解及简单实例
- java 向上转型和向下转型 源代码示例
- Java - Apache Mina 简单示例
- Java 泛型参数 向上转型
- java awt简单示例 frame and panel
- Java动态(后期)绑定和overloading 向上转型
- Java向上转型和向下转型
- Java的向上转型和向下转型
- Java连接ODBC数据源的简单示例
- java awt 事件监听 简单示例
- Java数据结构之简单的连接点(link)实现方法示例
- java简单实现八叉树图像处理代码示例
- Java数据结构之循环队列简单定义与用法示例
- java.misc.Unsafe底层操作类简单示例
- 从一个简单的Java单例示例谈谈并发
- java内存分配和强转/转型(向上和向下转型)相关
- java几种常用设计模式简单示例
- JAVA向上转型,构造函数
- java中的向上转型有什么妙用?