• 多态的使用和向上向下转型


    补充知识点:1.一个java文件中只能有一个公共类(公共类名必须和这个JAVA源程序文件名相同)

          2.如果是继承的话,只能继承一个类,但可以继承多个接口。

    多态==晚绑定 实质就是父类型的引用可以指向子类型的对象。

          Parent p = new Child();//向上转型

        当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误;

        如果有,调用子类的方法

    例如

     1 class animal{
     2     protected void show()
     3     {
     4         System.out.println("this is animal");
     5     }
     6 }
     7 
     8 class dog extends animal{
     9     public void show(){
    10         System.out.println("this is dog");
    11     }
    12 }
    13 public class basic {
    14     public static void main(String[]  args){
    15         animal a = new dog();//这里new的是dog,就调用dog里面的同名方法
    16         a.show();
    17     }
    18 }
    View Code

    运行结果 this is dog

    向上转型:animal a = new dog();

    向下转型:注意1.父类型的引用必须指向子类的对象,指向谁才会转换谁,不然编译出错,例如

    public class basic {
        public static void main(String[]  args){
            animal a = new animal();
            dog d =(dog)a;
            d.show();
        }
    }
    View Code

    出错:Exception in thread "main" java.lang.ClassCastException: animal cannot be cast to dog

    at basic.main(basic.java:18)

    public class basic {
        public static void main(String[]  args){
            animal a = new dog();
            dog d =(dog)a;
            d.show();
        }
    }
    View Code

    正确;       2.必须使用强制类型转换

    总结:多态存在必须有继承、方法覆盖、父类引用指向子类对象三个条件

     特别的:向上转型中存在静态方法,调用的是自己的本身的静态方法,不管new 的是什么,只看引用类型,即,谁定义,谁调用

    class animal{
        protected static void show()
        {
            System.out.println("this is animal");
        }
    }
    
    class dog extends animal{
        public static void  show(){
            System.out.println("this is dog");
        }
    }
    public class basic {
        public static void main(String[]  args){
            animal a = new dog();//静态方法 调用的是animal,非静态调用的是dog
            a.show();
        }
    }
    View Code

    结果:this is animal

     1 import java.util.*;
     2 
     3 class animal{
     4     protected static void show()
     5     {
     6         System.out.println("this is animal");
     7     }
     8 }
     9 
    10 class dog extends animal{
    11     public static void  show(){
    12         System.out.println("this is dog");
    13     }
    14 }
    15 public class basic {
    16     public static void main(String[]  args){
    17         animal a = new dog();//前面的例子已经说明这里的向下转型不能 new animal
    18         dog d = (dog)a;
    19         d.show();//谁定义谁调用
    20         
    21     }
    22 }
    View Code

    结果:this is dog

  • 相关阅读:
    Spring中Model,ModelMap以及ModelAndView之间的区别
    ssm框架中Controller层的junit测试_我改
    Controller、Service、Dao进行Junit单元
    ssm controller层 junit单元测试
    spring常用注解
    Spring MVC测试框架
    ssm框架junit简单测试_我写
    spring注入时报错::No qualifying bean of type 'xxx.xxMapper'
    Eclipse 报 "The builder launch configuration could not be found" 错误
    IIS负载均衡-Application Request Route详解第一篇: ARR介绍(转载)
  • 原文地址:https://www.cnblogs.com/hxjbc/p/5177901.html
Copyright © 2020-2023  润新知