面向对象编程-多态
- 向上转型
- 向下转型
- 对类的继承与对象的使用有足够的认识与了解
提示知识点【重点】
(1) 属性看编译,方法看类型
(2)一个对象的编译类型和运行类型可以不一致
(3) 编译类型在定义对象时,就确定了,不能改变
(4) 运行类型是可以变化的
(5) 编译类型看定义时 = 号的左边,运行类型看 = 号的右边
Animal animal = new Dog(); 【animal 编译类型是Animal,运行类型Dog】
animal = new Cat(); 【animal 的运行类型变成了Cat,编译类型仍然是 Animal】
源码笔记
向上转型
package com.tao.polymorphic_.upcasting;
/**
* Create By 刘鸿涛
* 2021/12/17 9:27
*/
public class Animal {
String name;
int age;
public void sleep(){
System.out.println("睡");
}
public void run(){
System.out.println("跑");
}
public void eat(){
System.out.println("吃");
}
public void show(){
System.out.println("hello,你好");
}
}
package com.tao.polymorphic_.upcasting;
/**
* Create By 刘鸿涛
* 2021/12/17 9:27
*/
public class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
public void catchMouse(){ //Cat特有方法
System.out.println("猫抓老鼠");
}
}
package com.tao.polymorphic_.upcasting;
/**
* Create By 刘鸿涛
* 2021/12/17 9:25
*/
public class PolyDetail {
public static void main(String[] args) {
//向上转型:分类的引用指向了子类的对象
//语法:父类类型引用名 = new 子类类型();
Animal animal = new Cat();
Object obj = new Cat(); //也是可以的 可以的 Object也是Cat的父类
System.out.println("ok~~~");
//可以调用父类中的所有成员(需遵守访问权限)
//但是不能调用子类的特有的成员->无法调用Cat类catchMouse()
//因为在编译阶段,能调用哪些成员,是由编译类型来决定的
//animal.catchMosue() error
//最终运行效果看子类的具体实现
animal.eat(); //猫吃鱼(子类)
animal.run(); //跑(父类)
}
}
向下转型
package com.tao.polymorphic_.downcasting;
/**
* Create By 刘鸿涛
* 2021/12/17 9:25
*/
public class PolyDetail {
public static void main(String[] args) {
//向上转型:分类的引用指向了子类的对象
//语法:父类类型引用名 = new 子类类型();
Animal animal = new Cat();
Object obj = new Cat(); //也是可以的 可以的 Object也是Cat的父类
//可以调用父类中的所有成员(需遵守访问权限)
//但是不能调用子类的特有的成员->无法调用Cat类catchMouse()
//因为在编译阶段,能调用哪些成员,是由编译类型来决定的
//animal.catchMouse() error
//最终运行效果看子类的具体实现
animal.eat(); //猫吃鱼(子类)
animal.run(); //跑(父类)
//现在使用向下转型调用子类Cat中catchMouse()方法
//多态的向下转型
//语法:子类类型 引用名 = (子类类型)父类引用;
//问一个问题?cat 的编译类型
Cat cat = (Cat) animal;
cat.catchMouse();
Animal animalDog = new Dog();
Dog dog = (Dog) animalDog;
System.out.println("ok~~");
}
}
多态细节分析
package com.tao.polymorphic_.detail_;
/**
* Create By 刘鸿涛
* 2021/12/17 10:22
*/
public class PolyDetail01 {
public static void main(String[] args) {
//属性没有重写之说!属性的值看编译类型
Base base = new Sub(); //向上转型
System.out.println(base.count); //? 属性看编译类型 10
Sub sub = new Sub();
System.out.println(sub.count); //20
System.out.println(sub instanceof Base); //true
System.out.println(sub instanceof Sub); //true
}
}
class Base{ //父类
int count = 10; //属性
}
class Sub extends Base{ //子类
int count = 20; //属性
}
/**
* 课堂练习:======================================
* double d = 13.4; //ok
* long l = (long)d; //ok
* System.out.println(l); //13
* int in = 5; //ok
* boolean b = (boolean)in; //不对,boolean -> int
* Object obj = "hello"; //可以,向上转型
* String objStr = (String)obj; //可以,向下转型
* System.out.println(objStr); //hello
*
* Object objPri = new Integer(5); //可以,向上转型
* String str = (String)objPri; //错误ClassCastExcetpion,指向Integer父类引用,转成String
* Integer str1 = (Integer)objPri; //可以
*/
这是未使用多态时的代码,冗余严重;