Day06 JAVA学习笔记之多态 instanceof和类型转换

多态

多态可以动态编译数据类型,有可扩展性。 

package com.oop;

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

public class Application {

    public static void main(String[] args) {

        //一个对象的实际类型是确定的
        //new Student();
        //new Person();

        //但是可以指向的引用类型就不确定了
        //左边为引用类型,右边为实际类型

        //Student能调用的方法都是自己的或者继承父类的
        Student s1 = new Student();
        //Person父类型,可以指向子类,但是不能调用子类独有的方法
        Person s2 = new Student();
        Object s3 = new Student();

        s2.run(); //子类重写了父类的方法,执行子类的方法
        s1.run();

        //s2.eat();//虽然s2的实际类型是Student但是引用类型是Person,Person没有eat这个方法
        //所以s2无法调用eat
        //对象能执行那些方法主要看左边,和右边关系不大
        ((Student)s2).eat();//强制转换之后就可以使用
        s1.eat();
    }
}

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("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}

instanceof

 判断是否有子父类关系

X instanceof Y

看X的引用类型与Y的关系,如果没有关系就会编译错误

看X的实际类型与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) {

        //Object > String
        //Object > Person > Student
        //Object > Person > Teacher
        Object object = new Student();

        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        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);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);编译报错
        System.out.println("==================================");

        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        //System.out.println(student instanceof Teacher);编译报错
        //System.out.println(student instanceof String);编译报错
    }
}
package com.oop.demo06;

public class Student extends Person{

}

public class Teacher extends Person{
    
}

类型转换

public class Application {

    public static void main(String[] args) {
        //类型之间的转化: 父  子

        // 高                    低
        Person obj = new Student();

        //student.go();
        //无法调用,因为obj为Person类,无法用Student类的独有方法
        //将obj这个对象转换为Student类型,我们就可以使用Student类型的方法了

        Student student = (Student)obj;
        student.go();
        //((Student)obj).go();也可以

        //子类转换为父类,可能丢失自己的本来的一些方法;
        Person person = student;
    }
}

 总结

1.父类引用指向子类的对象,不能反过来。

2.子类转换为父类,是向上转型,不用强制转换。

3.父类转换为子类,是向下转型,需要强制转换。 

4.方便方法的调用,减少重复的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值