Use of the instanceof keyword

Code example

public class InstanceofTest {
        public static void main(String[] args) {
            Dog d=new Dog("哈士奇","白色");
            d.run();
            System.out.println("狗是狗类吗?"+(d instanceof Dog));
            System.out.println("狗是动物类吗?"+(d instanceof Animal1));
            Mouse m=new Mouse("汤姆","黑色");
            m.bite();
            // System.out.println("狗是动物类吗?"+(d instanceof Mouse));

            // Animal1 animal1=(Animal1)d;
            // System.out.println(animal1.getName());

            Animal1 animal11 = new Dog("小妞");
            System.out.println((animal11 instanceof Dog)+"  父类是狗狗");
            Dog dog=(Dog)animal11;
            System.out.println(dog.getName()+" 狗狗姓名");

            Animal1 animal12 = new Mouse("小妞niu");
            Mouse mouse=(Mouse)animal12;
            System.out.println(mouse.getName()+"  老鼠姓名");




            //     Animal1 a=new Animal1("动物","蓝色");
        //     System.out.println("动物类是狗吗?"+(a instanceof Dog));
        //     if(a instanceof Dog){
        //         Dog dog=(Dog)a;
        //         System.out.println("强转成功");
        //     }
    }
}
class Animal1{   //动物类
    String name;
    String color;
    public Animal1(String name,String color){
        this.name=name;
        this.color=color;
    }

    public Animal1() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

class Dog extends Animal1{          //狗类
    private String name;

    public Dog(String name, String color) {
        super(name, color);
    }
    public Dog(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    public void run(){
        System.out.println(name+"开始奔跑");
    }
}

class Mouse extends Animal1{         //老鼠类
    private String name;

    public Mouse(String name) {
        this.name = name;
    }

    public Mouse(String name,String color){
        super(name,color);
    }
    public void bite(){
        System.out.println(name+"吃东西");
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }
}

Print result

null开始奔跑
狗是狗类吗?true
狗是动物类吗?true
null吃东西
true  父类是狗狗
小妞 狗狗姓名
小妞niu  老鼠姓名

1. Java inheritance
means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance domain and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has the same behavior as the parent class. .

2. The Java subclass is forced to the parent class. The
parent class reference points to the subclass object: in
java, the subclass is forced to the parent class, but it is actually still a subclass;
the reference can only call the methods and variables defined in the parent class;
if the subclass is A method in the parent class is overridden in this method, then when this method is called, this method in the subclass will be called;

3. The strong rotor class of the Java parent class
only has the parent class object itself when the subclass new comes out , It can be coerced into a subclass object in the future.

Persistence or non-persistence in this life is not terrible. What I am afraid of is walking on the road of persistence alone! ! !

Guess you like

Origin blog.csdn.net/taiguolaotu/article/details/113822355