首页 > 其他 > 详细

多态及instanof的引用类型

时间:2021-03-29 20:33:13      阅读:30      评论:0      收藏:0      [点我收藏+]

多态

           #多态及同一方法可以根据发送对象的不同而采用多种不同的行为方式。

           #一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

 

多态存在的条件

           #有继承关系

           #子类重写父类的方法

           #父类引用指向子类对象

 

注意  : 多态是方法的多态,属性没有多态性

 

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();

        //对象能执行哪种方法,主要看对象左边的类型,和右边关系不大
        ((Student) s2).eat();  //子类重写了父亲的方法,执行了子类的方法
        s1.run();
    }
}

 

 

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

 

 

instanceof     (类型转换) 引用类型

             1. 父类引用指向子类的的对象 

             2.把子类转换为父类,向上转型

             3.把父类转换为子类,向下转型:强制转换

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

       

    抽象:封装、继承、多态!

                          

public class Application {
    public static void main(String[] args) {

        //类型之间的转换:父    子

        //高                   低
        //Person obj=new Student();
        //student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
        //((Student) obj).go();

        //子类转换为父类,可能丢失自己本来的一些方法!
        Student student=new Student();
        student.go();
        Person person=student;
    }
}
public class Student extends Person{
    public void go(){
        System.out.println("go");
    }
}


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

        //System.out.println(X instanceof Y); //X与Y是否存在父子关系,判断是否编译通过!

        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);//false
        //System.out.println(student instanceof String);//编译报错!
 */

 

多态及instanof的引用类型

原文:https://www.cnblogs.com/clblogs/p/14593707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!