栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多态、instanceof和类型转换 2021-11-30

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多态、instanceof和类型转换 2021-11-30

Day26 多态

动态编译:
同一种方法可以根据发送对象的不同而采用多种不同的行为方式。
一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

多态存在的条件
有继承关系
子类重写父类的方法
父类引用指向子类对象

实例1
package com.oop.demo06;

public class Person {
    public void run(){
        System.out.println("run");
    }
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //可以指向的引用类型就不确定了
        Student s1 = new Student();
        Person s2 = new Student();  //父类的引用指向子类
        Object s3 = new Student();

        s2.run();       
    }
}

实例1

当子类重写了父类的方法,则执行子类的方法

父类

package com.oop.demo06;

public class Person {
    public void run(){
        System.out.println("run");
    }
}

子类

package com.oop.demo06;

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("run1");
    }
}
package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //可以指向的引用类型就不确定了
        Student s1 = new Student();
        Person s2 = new Student();  //父类的引用指向子类
        Object s3 = new Student();
        s2.run();
        s1.run();   //当子类重写了父类的方法,执行子类的方法
    }
}

例子
package com.oop.demo06;

public class Person {
    public void run(){
        System.out.println("run");
    }
}
package com.oop.demo06;

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("run1");
    }
    public void eat(){
        System.out.println("eat");
    }
}
package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //可以指向的引用类型就不确定了
        Student s1 = new Student();     //子类指向的引用
        // Student 能调用的方法都是自己的或者继承父类的
        Person s2 = new Student();  //父类的引用指向子类,//父类指向的引用
        // Person 父类型,可以指向子类,但是不能调用子类独有的方法
        Object s3 = new Student();
        s2.run();
        s1.run();   //当子类重写了父类的方法,执行子类的方法
        s1.eat();
        s2.eat();    **报错,Person方法里面是没有这个eat方法的,所以不可以调用
    }
}


高类型强制转换为低类型

如果子类和父类都有一个方法的情况下,只要子类没有重写父类,那么就调用父类的方法,如果子类重写了父类,那么就调用子类的方法。
对象能执行哪些方法,主要看对象左边的类型,和右边关系不大

 
instanceof:判断一个对象是什么类型 
package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        //instanceof判断类型是否相似,然后判断是否需要强制转换
        Object object = new Student();
        System.out.println(object instanceof Student);  //Ture
        System.out.println(object instanceof Person);   //Ture
        System.out.println(object instanceof Object);   //Ture
        System.out.println(object instanceof Teacher);  //False
        System.out.println(object instanceof String);   //False
    }
}

package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        //instanceof判断类型是否相似,然后判断是否需要强制转换
        Object object = new Student();
        System.out.println(object instanceof Student);  //Ture
        System.out.println(object instanceof Person);   //Ture
        System.out.println(object instanceof Object);   //Ture
        System.out.println(object instanceof Teacher);  //False
        System.out.println(object instanceof String);   //False
        System.out.println("==============");
        Person person = new Student();
        System.out.println(person instanceof Student);  //Ture
        System.out.println(person instanceof Person);   //Ture
        System.out.println(person instanceof Object);   //Ture
        System.out.println(person instanceof Teacher);  //False
        //System.out.println(person instanceof String);   //编译错误
    }
}

输出结果

package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        //instanceof判断类型是否相似,然后判断是否需要强制转换
        Student student = new Student();
        System.out.println(student instanceof Student);  //Ture
        System.out.println(student instanceof Person);   //Ture
        System.out.println(student instanceof Object);   //Ture
        //System.out.println(student instanceof Teacher);  //编译错误
        //System.out.println(student instanceof String);   //编译错误
    }
}


总结:

//System.out.println(X instanceof Y);  //能不能编译通过


package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
    public static void main(String[] args) {
        //类型之间的转换:父   子
        //由低转高是不需要强制转换
        //高                 低    需要强制转换
        Person student = new Student();
        student.go();     //报错     
    }
}


强制转换方式


当然也可以写成一行

类型转换
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
    public static void main(String[] args) {
        //类型之间的转换:父   子
        //子类转换为父类,可能会丢失自己本来的一些方法
        Student student = new Student();
        student.go();
        Person person = student;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632219.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号