什么是Java中的上下转换?
类型转换将一种数据类型转换为另一种数据类型。
向上转换-将子类类型转换为超类类型被称为向上转换。
示例
class Super {
void Sample() {
System.out.println("method of super class");
}
}
public class Sub extends Super {
void Sample() {
System.out.println("method of sub class");
}
public static void main(String args[]) {
Super obj =(Super) new Sub(); obj.Sample();
}
}向下转换-将超类类型转换为子类类型称为向下转换。
示例
class Super {
void Sample() {
System.out.println("method of super class");
}
}
public class Sub extends Super {
void Sample() {
System.out.println("method of sub class");
}
public static void main(String args[]) {
Super obj = new Sub();
Sub sub = (Sub) obj; sub.Sample();
}
}热门推荐
10 未来事业祝福语简短
11 发给家人祝福语简短英文
12 黄奕结婚祝福语简短高级
13 口红祝福语长辈文案简短
14 王字生日祝福语简短
15 祝福语送超市同事简短
16 给阿姨简短生日祝福语
17 口才课拜年祝福语简短
18 婚礼祝福语真心的话简短