学习设计模式的时候,发现不少模式都用到了向上转型(eg. 工厂方法)。而我对向上转型(upcasting)的机制并不十分熟悉。这篇文章将深刻分析向上转型的机制、内存分析。设计模式
先从几个基本概念开始:学习
Java中的数据类型分为两类:基本数据类型(int, double)和引用类型(String)。引用类型对象中保存着一个地址(称为“引用”),引用指向实际对象(称为“实例”),实际对象中保存着值。spa
关于Java数据类型在内存中的分析详解,请参考知友Intopass的解答:设计
Java 究竟是值传递仍是引用传递? - Intopass的回答 - 知乎
https://www.zhihu.com/questio...
Heap中的对象会继续指向内存中的数据段(data segment)。数据保存对象的实际值code
定义:将父类的引用指向子类的实例 or 将子类对象赋值给父类引用对象
Dog dog = new Dog(); Animal anim = (Animal) dog; //实际对象类型没变,仅引用类型改变了 anim.eat();
因为实际对象类型没变,因此,anim调用的eat方法还是Dog类中重写的eat方法,而不是父类Animal类中的eat方法。内存
咱们为何要使用向上转型?get
Generally, upcasting is not necessary. However, we need upcasting when we want to write general code that deals with only the supertype.
通常说来,向上转型不是必须的。但有时候,当咱们的代码只须要与父类打交道时,能够使用向上转型,来使咱们的代码不依赖具体子类,好比:it
public class AnimalTrainer { public void teach(Animal anim) { anim.move(); anim.eat(); } }
如下代码,teach方法能够接受Animal类的任意子类:io
Dog dog = new Dog(); Cat cat = new Cat(); AnimalTrainer trainer = new AnimalTrainer(); trainer.teach(dog); trainer.teach(cat);
咱们来分析如下转型代码在内存中的表示:
Dog dog = new Dog(); Animal anim = (Animal)dog;