Java课堂练习-面向对象-多态概述 - 向上转型 - 向下转型举例

->记录锚点

面向对象编程-多态

  1. 向上转型
  2. 向下转型
  3. 对类的继承与对象的使用有足够的认识与了解

提示知识点【重点】

(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;      //可以
 */

Top

这是未使用多态时的代码,冗余严重;
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值