java多态性---upcasting and downcasting
来源:互联网 发布:希尔伯特矩阵方程组 编辑:程序博客网 时间:2024/05/09 02:12
1、upcasting和downcasting
class Person{void fun1(){System.out.println("Person's fun1");}void fun2(){System.out.println("Person's fun2");}}class Student extends Person{Student(){}void fun1(){System.out.println("Student's fun1");}void fun2(){System.out.println("Student's fun2");}}public class JavaTest{public static voidmain(String[] agrs){Person p=new Student();p.fun1();p.fun2();}}
2、强制类型转换需要注意的问题
class Person{void fun1(){System.out.println("Person's fun1");}void fun2(){System.out.println("Person's fun2");}}class Student extends Person{void fun1(){System.out.println("Student's fun1");}void fun2(){System.out.println("Student's fun2");}}public class JavaTest{public static void main(String[] agrs){Person p=new Person();Student stu=(Student)p;stu.fun1();stu.fun2();}}
3、casting可见性问题
upcasting之后仅仅对父类的属性和方法可见
class Person{void fun1(){System.out.println("Person's fun1");}void fun2(){System.out.println("Person's fun2");}}class Student extends Person{void fun1(){System.out.println("Student's fun1");}void fun2(){System.out.println("Student's fun2");}void fun3(){System.out.println("Student's fun3");}}public class JavaTest{public static void main(String[] agrs){Person p=new Student();p.fun3();Student stu=(Student)p;stu.fun3();}}
改成如下code即可
class Person{void fun1(){System.out.println("Person's fun1");}void fun2(){System.out.println("Person's fun2");}}class Student extends Person{void fun1(){System.out.println("Student's fun1");}void fun2(){System.out.println("Student's fun2");}void fun3(){System.out.println("Student's fun3");}}public class JavaTest{public static void main(String[] agrs){Person p=new Student();//upcasting,此时p对fun3不可见//p.fun3();Student stu=(Student)p;//downcasting,此时stu对fun3可见stu.fun3();}}
1 0
- java多态性---upcasting and downcasting
- upcasting and downcasting
- java upcasting downcasting
- 在Java中使用Upcasting和Downcasting
- Java Upcasting 上溯造型 Downcasting -Java 学习笔记 (17)
- java向上转型 向下转型(upcasting downcasting)
- java 向上转型(upcasting)和向下转型(downcasting)区别举例详解
- JAVA中的向下转型(downcasting)及泛型与向上转型(upcasting)及RTTI、反射机制
- JAVA中的向下转型(downcasting)及泛型与向上转型(upcasting)及RTTI、反射机制
- JAVA中的向下转型(downcasting)及泛型与向上转型(upcasting)及RTTI、反射机制
- upcasting
- think in java笔记 upcasting
- Java中Downcasting向下转型的用处
- java 多态性
- java 多态性
- java多态性
- Java多态性
- java 多态性
- shell sed 与正则替换时间串
- kerl管理erlang debian linux 系统
- 23中设计模式,详细举例说明,通俗易懂,适合新手阅读
- jQuery Wookmark-2 jQuery动态表格插件的效果展示
- ssh优缺点
- java多态性---upcasting and downcasting
- CSV解析
- mvn scope
- MVC 优缺点
- 架构技术总结
- 电源引起的内存报警故障
- BonCP性能高的原因
- JVM GC
- mvc原理和mvc模式的优缺点