Java 转型问题(向上转型和向下转型)

Java 转型问题其实并不复杂,只要记住一句话:父类引用指向子类对象。

什么叫父类引用指向子类对象?

从 2 个名词开始说起:向上转型(upcasting) 、向下转型(downcasting)

举个例子:有2个类,Father 是父类,Son 类继承自 Father。

第 1 个例子:

Father f1 = new Son(); // 这就叫 upcasting (向上转型) // 现在 f1 引用指向一个Son对象 Son s1 = (Son)f1; // 这就叫 downcasting (向下转型) // 现在f1 还是指向 Son对象

第 2 个例子:

Father f2 = new Father(); Son s2 = (Son)f2; // 出错,子类引用不能指向父类对象

你或许会问,第1个例子中:Son s1 = (Son)f1; 问为什么是正确的呢。

很简单因为 f1 指向一个子类对象,Father f1 = new Son(); 子类 s1 引用当然可以指向子类对象了。

而 f2 被传给了一个 Father 对象,Father f2 = new Father(); 子类 s2 引用不能指向父类对象。

总结:

1、父类引用指向子类对象,而子类引用不能指向父类对象。

2、把子类对象直接赋给父类引用叫upcasting向上转型,向上转型不用强制转换吗,如:

Father f1 = new Son();

3、把指向子类对象的父类引用赋给子类引用叫向下转型(downcasting),要强制转换,如:

f1 就是一个指向子类对象的父类引用。把f1赋给子类引用 s1 即 Son s1 = (Son)f1;

其中 f1 前面的(Son)必须加上,进行强制转换。

一、向上转型。

通俗地讲即是将子类对象转为父类对象。此处父类对象可以是接口。

1、向上转型中的方法调用:

实例

 1 public class Animal {
 2   
 3   public void eat(){
 4     System.out.println("animal eatting...");
 5   }
 6 }
 7 class Bird extends Animal{
 8   
 9   public void eat(){
10     System.out.println("bird eatting...");
11   }
12   
13   public void fly(){
14     
15     System.out.println("bird flying...");
16   }
17 }
18 class Main{
19    public static void doEat(Animal h) {
20     h.eat();
21   }
22   public static void main(String[] args) {
23     
24     Animal b=new Bird(); //向上转型
25     b.eat(); 
26     //! error: b.fly(); b虽指向子类对象,但此时丢失fly()方法
27     Animail c1=new Animal();
28     Bird c2=new Bird();
29     doEat(c1);
30     doEat(c2);//此处参数存在向上转型
31   }
32 }
转型

注意这里的向上转型:

Animal b=new Bird(); //向上转型 b.eat();

此处将调用子类的 eat() 方法。原因:b 实际指向的是 Bird 子类,故调用时会调用子类本身的方法。

需要注意的是向上转型时 b 会遗失除与父类对象共有的其他方法。如本例中的 fly 方法不再为 b 所有。

2、向上转型的作用

看上面的代码:

public static void doEate(Animail h) { h.sleep(); }

这里以父类为参数,调有时用子类作为参数,就是利用了向上转型。这样使代码变得简洁。不然的话,如果 doEate 以子类对象为参数,则有多少个子类就需要写多少个函数。这也体现了 JAVA 的抽象编程思想。

二、向下转型。

与向上转型相反,即是把父类对象转为子类对象。

实例

 
 1 public class Animail {
 2     private String name="Animail";
 3     public void eat(){
 4         System.out.println(name+" eate");
 5     }
 6 }
 7 
 8 public class Human extends Animail{
 9     private String name="Human";
10     public void eat(){
11         System.out.println(name+" eate");
12     }
13 }
14 
15 public class Main {
16     public static void main(String[] args) {
17         Animail a1=new Human();//向上转型
18         Animail a2=new Animail();
19         Human b1=(Human)a1;// 向下转型,编译和运行皆不会出错
20         Human c=(Human)a2;//不安全的向下转型,编译无错但会运行会出错
21     }
22 }
实例
Animail a1=new Human();//向上转型
Human b1=(Human)a1;// 向下转型,编译和运行皆不会出错

这里的向下转型是安全的。因为 a1 指向的是子类对象。

Animail a2=new Animail();
Human c=(Human)a2;//不安全的向下转型,编译无错但会运行会出错
 

运行出错:

Exception in thread "main" java.lang.ClassCastException: study.转型实例.Animail cannot be cast to study.转型实例.Human
at study.转型实例.Main.main(Main.java:8)

向下转型的作用

向上转型时 b会遗失除与父类对象共有的其他方法;可以用向下转型在重新转回,这个和向上转型的作用要结合理解。

猜你喜欢

转载自www.cnblogs.com/xiaoyezideboke/p/10939219.html