美文网首页
浅谈多态

浅谈多态

作者: 安安静静写代码 | 来源:发表于2017-08-05 09:42 被阅读10次

    一种事物有多种状态称为多态
    多态分为运行时多态和编译时多态
    下面来介绍几种体现多态的例子
    1.向上转型和重载体现了编译时多态
    2.向下转型和重写体现了运行时多态
    下面重点介绍一下向上转型和向下转型

    向上转型

    向上转型指父类引用指向子类对象,所以向上转型必须发生在继承关系中
    例如i:父类是一个抽象的动物类,而猫类继承了动物类那么声明一个父类的引用变量指向子类的对象,这种被称为向上转型,转型之后父类引用只能引用自己的变量和方法,但如果子类重写了父类中同名方法那么父类调用此方法时是调用的子类重写之后的方法

    Animal animal = new Cat();
    animal.eat();//调用的为子类中重写父类的方法
    animal.sleep();
    

    向下转型(强制的)

    因为父 类引用不能调用子类特有的属性和方法所以需要将父类引用变量给子类
    注意:被强制转换的父类引用一开始就是指向子类对象的而不是指向父类创建的对象,所以向下转型的条件为先向上转型再向下转型

    Father father  = new Son();
    Son son  = (Son) father;     //这种方式是正确的
    
    Father father  = new Father();//一开始就是父类引用变量指向了父类的对象
    Son son  = (Son) father;     //这种方式是错误的
    

    那么向下转型时怎么判断要转型的对象是不是由你想转成的对象类型呢,这就用到了一个判断语句
    对象 instance of 类名 返回值为boolean类型

    
    package com.qf.demo5;
    
    import com.qf.demo4.Animal;
    
    public class Test {
    
        public static void main(String[] args) {
            
            Fathor fathor = new Son();// 自动的向上转型
            fathor.beijingOpera();
            // 父亲的名字  不能直接调用儿子特有的方法 
            //fathor.song();
            
            // 向下转型  因为  父类名字不能调用子类特有的方法  
            Son son = (Son) fathor;// 向下转型  
            son.song();
            Fathor fathor2 = new SecondSon();
            fathor2.beijingOpera();
            //fathor2.dance();
            // 向下转型   
            SecondSon secondSon = (SecondSon) fathor2;
            secondSon.dance();
            SecondSon secondSon2 = new SecondSon();
            show(secondSon2);//调用show()方法
            Son son2 = new Son();
            show(son2);
            
        }
        public static void show(Fathor fathor){// SecondSon
            
            fathor.beijingOpera();
            // ClassCastExecption  类型转换异常
            // 通常发生在 向下转型时 ,转成的类 并不是对象的真正的实体的类型
            // 左边的对象  是否是有哦  右边的类  创建出来的
            if(fathor  instanceof  Son){     //调用该判断函数
                Son son = (Son) fathor;
                son.song();
            }else 
                           if(fathor instanceof SecondSon){
                SecondSon secondSon = (SecondSon) fathor;
                secondSon.dance();
            }
            
        }
        
    }
    

    下面再看一个例子

    package com.qf.demo12;
    
    public class Test1 {
    
        public static void main(String[] args) {
            Object o1 = new Object();
            System.out.println(o1 instanceof Object);
            System.out.println(o1 instanceof String);
                    String o2 = new String();
                    System.out.println(o2 instanceof Object);
            System.out.println(o2 instanceof String);
               Object o3 = new String();
                System.out.println(o3 instanceof Object);
            System.out.println(o3 instanceof String);
                o2 = (String)o1;//向下转型的前提是o1是子类所创但是此o1是父类引用指向的就是父类对象                  
                o2 = (String)o3;//此处是向下转型,正确
                o2 = (String)o1;//此处错误在强转o1(o1是父类所创),不在于前面是谁接收
               o3 = o1;        //此处正确,父类引用指向父类对象
        
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:浅谈多态

          本文链接:https://www.haomeiwen.com/subject/audilxtx.html