Java instanceof and type conversion-16 days study notes

Instanceof

instanceof (type conversion) refers to the type to determine what type an object is

Can help us determine whether the two classes have an inheritance relationship

If it is a parent-child relationship, it will be true, if it is a sibling relationship, it will return false.

//Object > String
        //Object > Person1 > Teacher
        //Objiec > Person1 >Student1

        Object object = new Stunden1();

        //System.out.println(X instanceof Y);//判断是否编译通过

        System.out.println(object instanceof Stunden1);//ture
        System.out.println(object instanceof Person1);//ture
        System.out.println(object instanceof Object);//ture
        System.out.println(object instanceof Teacher);//false
        // Object > Person1 > Teacher 没有在Student1里面所有false,兄弟不能跟父子一样
        System.out.println(object instanceof String);//false
        //Object > String    String跟Person1属于同类也不知Student1里面

        Person1 person = new Stunden1();
        System.out.println("================================");

        System.out.println(person instanceof Stunden1);//ture
        System.out.println(person instanceof Person1);//ture
        System.out.println(person instanceof Object);//ture
        System.out.println(person instanceof Teacher);//false
       // System.out.println(person1 instanceof String);//编译报错

        Stunden1 stunden1 = new Stunden1();
        System.out.println("================================");

        System.out.println(stunden1 instanceof Stunden1);//ture
        System.out.println(stunden1 instanceof Person1);//ture
        System.out.println(stunden1 instanceof Object);//ture
        //System.out.println(stunden1 instanceof Teacher);//编译报错
       // System.out.println(stunden1 instanceof String);//编译报错

Conversion between types

The basic type of conversion from high to low 128> 64> 32 high to low requires strong conversion,

In the same way, the father-son relationship needs to be changed.

1.父类引用指向子类的对象
2.把子类转换为父类,向上转型,自动转型,丢失子类中原本可以之间调用的特有方法
3.父类转换为子类要强制转换,向下转型,丢失父类被子类所重写的方法。
4.方便方法的调用,减少重复的代码

抽象 : 封装,继承,多态
package com.oop;

import com.oop.Demo05.Person1;
import com.oop.Demo05.Stunden1;

//一个项目应该只有一个main方法
public class Application {

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

        //向上转型 自动转型 ,又叫多态                  
       Person1 s1 = new Stunden1();
        

       //s1将这个对象转换为Student类型,我们就可以使用Student1类型的方法
        
        ((Stunden1)s1).go(); //强制转换

        //子类转成父类会丢失一些方法
        Stunden1 s2 = new Stunden1();//可以调用本身的方法
        s2.go();;
        // 低转高,自动转换
        
        
    }
    /*
    1.父类引用指向子类的对象
    2.把子类转换为父类,向上转型,自动转型,丢失子类中原本可以之间调用的特有方法
    3.父类转换为子类要强制转换,向下转型,丢失父类被子类所重写的方法。
    4.方便方法的调用,减少重复的代码
    
    抽象 : 封装,继承,多态
    

           */
}
public class Person1 {


}
package com.oop.Demo05;

public class Stunden1 extends Person1 {

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

Teacher

package com.oop.Demo05;

public class Teacher extends Person1 {

}

Guess you like

Origin blog.csdn.net/yibai_/article/details/114903119
Recommended