Java 学习笔记 Day012(续)

来源:互联网 发布:c语言三角形判断 编辑:程序博客网 时间:2024/06/06 10:00

多态

子类的对象转化为 父类的引用(向上转型),或父类的引用转化为子类的引用的(向下转型);
多态 简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。
多态的优点:消除类型之间的耦合关系、可替换性、可扩充性、接口性、灵活性、简化性
多态存在的三个必要条件:继承、重写、父类引用指向子类对象
当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误;如果有,再去调用子类的同名方法。(先到父类查找方法的定义,执行的时候优先调用子类的同名方法
//多态的实现方式:1.重写 2.接口 3.抽象类和抽象方法
通过例子简单理解吧。

package com.yan.www.polymorphism;//file name: Human.java//@author Gonsonpublic class TheMain {    public static void main(String[] args) {        //子类的对象转化为 父类的引用(向上转型)        Human hm = new Man();        Human hw = new Woman();        //多态的体现:一个种类的引用可以 对应多种对象。        hm.sayYourName();        hw.sayYourName();        //或父类的引用转化为子类的引用的(向下转型)        Man m = (Man)hm;        m.whatSex();        //主要强行转型没有语法错误,但是可能执行(编译)错误。        Woman w = (Woman)hm;        w.whatSex();        //上面两行没有语法错误,但是执行后会报  java.lang.ClassCastException 错误        //所以向下转型时应该要满足:原来的父类引用指向 对应原来子类对象,或原来子类对象的子类。        // 因为 hm 这个引用对应的是Man 这个子类,当强制向下转型为 Women 时,就会发生错误。    }}//声明一个Human 的父类class Human{    public void sayYourName(){        System.out.println("human name");    }   }//声明一个man 的子类class Man extends Human{    @Override    public void sayYourName() {        // TODO Auto-generated method stub        System.out.println("man name");    }       public void whatSex(){        System.out.println("I am male");    }}//声明一个woman 的子类class Woman extends Human{    @Override    public void sayYourName() {        // TODO Auto-generated method stub        System.out.println("woman name");    }       public void whatSex(){        System.out.println("I am female");    }}/*执行结果:man namewoman nameI am maleException in thread "main" java.lang.ClassCastException: com.yan.www.polymorphism.Man cannot be cast to com.yan.www.polymorphism.Woman    at com.yan.www.polymorphism.TheMain.main(TheMain.java:16)*/

instanceof关键字

(instance of 中文翻译: 实例化)
instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例
在上面的例子中,因为强行转型而导致的错误,可用instanceof 运算符在向下转型之前就判断转型是否成功。语法: 对象 instanceof 类型名
修改上面例子出错部分:

        //Woman w = (Woman)hm;        //w.whatSex();        //为了避免上面代码执行导致的错误可以将上面代码改为:        if (hm instanceof Woman) {            Woman w = (Woman)hm;            w.whatSex();        } else {            System.out.println("cannot do \"Woman w = (Woman)hm\" ");        }

instanceof 使用情况如下:

//filename: InstTest.java//@author Gonsonpublic class InstTest {    public static void main(String[] args) {        Test t = new Test();        TestA ta = new TestA();        Test t1 = new TestA(); //向上转型        Test t2 = new TestB(); //向上转型        if(t instanceof Test)System.out.println("t instance of Test");        //对象t 是 Test 类 (的对象),结果为true        if(t instanceof TestA)System.out.println("t instanceof TestA");        //对象t 不是 TestA类 (的对象),结果fault        if(ta instanceof Test)System.out.println("ta instanceof Test");        //对象ta 是Tset 子类(的对象),结果为true        if(ta instanceof TestA)System.out.println("ta instanceof TestA");        //对象ta 是TsetA 类(的对象),结果为true                if(t1 instanceof Test)System.out.println("t1 instanceof Test");        // 虽然t1 是Test父类的引用,但t1 是 TestA 的对象,所以 t1 是Test的子类(的对象),结果为true        if(t1 instanceof TestA)System.out.println("t1 instanceof TestA");           //对象ta 是TsetA 类,结果为true                     if(t2 instanceof TestA)System.out.println("t1 instanceof TestA");        //t2 不是TestA类,结果为fault        System.out.println("---done---");    }}//创建三个类class Test{}class TestA extends Test{}class TestB extends Test{}/*执行结果:t instance of Testta instanceof Testta instanceof TestAt1 instanceof Testt1 instanceof TestA---done---*/
0 0
原创粉丝点击