instanceof 和类型转换

instanceof 和类型转换

package zaiyang.oop.demo06;

public class Application {

    public static void main(String[] args) {

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

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

    }
}
package zaiyang.oop.demo06;

public class Application {

    public static void main(String[] args) {
        //类型之间的转换:  父  子。高转低需要强制转换

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


    }
}
package zaiyang.oop.demo06;

public class Person {

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



}





/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类要有联系。类型转换异常! ClassCastException!
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();

不能方法需要重写的
1.static 静态方法,属于类,它不属于实例
2.final 常量;
3.private方法;
 */
package zaiyang.oop.demo06;

public class Student extends Person{
    public void go(){
        System.out.println("go");
    }

    }

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

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

    */
package zaiyang.oop.demo06;

public class Application {

    public static void main(String[] args) {
        //类型之间的转换:  父  子。高转低需要强制转换
        //
        Student student = new Student();
        student.go();
        Person person = student;
        person.go;//强制转换,丢失方法


    }
}
/*
1.父类的引用指向子类的对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型:强制转换,丢失方法
4.方便方法的调用,减少重复的代码!简洁

抽象:封装、继承、多态! 抽象类
 */
posted @ 2022-04-13 21:43  追风的羊  阅读(25)  评论(0)    收藏  举报