其他分享
首页 > 其他分享> > 用instanceof关键字进行类型判断和笔记本USB接口案例

用instanceof关键字进行类型判断和笔记本USB接口案例

作者:互联网

instanceof关键字进行类型判断

如何知道一个父类引用的对象,本来是什么子类

格式:

对象  instanceof  类名称

这将会得到一个boolean值结果,也就是判断前面的对象能不能当做后面类型的实例。

public class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

    @Override
    public void watch() {
        System.out.println("猫卖萌");
    }
}
public class Dog extends Animal{
    @Override
    public void eat() {
        System.out.println("狗吃shit");
    }

    @Override
    public void watch() {
        System.out.println("狗看家");
    }
}
public abstract class Animal {
public abstract void eat();
public abstract void watch();
}
public class Test {
    public static void main(String[] args) {
        //本来是一只狗
        Animal animal = new Dog();
        //如果希望掉用子类特有方法,需要向下转型
        //判断一下父类引用animal本来是不是Dog
        if (animal instanceof Dog){
            Dog dog = (Dog) animal;
            dog.watch();
        }
        if (animal instanceof Cat){
            Cat cat = (Cat) animal;
            cat.watch();
        }

        giveMe(new Dog());
    }
    public static void giveMe(Animal animal){
        if (animal instanceof Dog){
            Dog dog = (Dog) animal;
            dog.watch();
        }
        if (animal instanceof Cat){
            Cat cat = (Cat) animal;
            cat.watch();
        }
    }
}

 

笔记本USB接口案例

 

public interface USB {
    //打开设备
    public abstract void open();
    //关闭设备
    public abstract void close();
}
public class Computer {
    public void powerOn(){
        System.out.println("笔记本电脑开机");
    }

    public void powerOff(){
        System.out.println("笔记本电脑关机");
    }
    //使用USB
    public void useDevice(USB usb){
        usb.open();
        //先判断
        if (usb instanceof Mouse){
            //向下转型
            Mouse mouse = (Mouse) usb;
            mouse.click();
        }else if (usb instanceof Keyboard){
            Keyboard keyboard = (Keyboard) usb;
            keyboard.type();
        }
        usb.close();
    }
}
public class Mouse implements USB{
    @Override
    public void open() {
        System.out.println("打开鼠标");
    }

    @Override
    public void close() {
        System.out.println("关闭鼠标");
    }

    public void click(){
        System.out.println("鼠标滑动");
    }
}
public class Keyboard implements USB{
    @Override
    public void open() {
        System.out.println("打开键盘");
    }

    @Override
    public void close() {
        System.out.println("关闭键盘");
    }
    public void type(){
        System.out.println("键盘输入");
    }
}
public class Test {
    public static void main(String[] args) {
        //创建一个笔记本电脑
        Computer computer = new Computer();
        computer.powerOn();
        //准备一个鼠标,给电脑使用
//        Mouse mouse = new Mouse();
        //进行向上转型(多态写法)
        USB usbMouse = new Mouse();
        //参数是USB类型,正好传递进去的就是USB鼠标
        computer.useDevice(usbMouse);
        //创建一个USB键盘(没使用多态)
        Keyboard keyboard = new Keyboard();
        //方法参数是USB类型,传递进去的是实现类对象
        computer.useDevice(keyboard);
        computer.powerOff();
        System.out.println("==============");
        //double转double
        method(10.0);
        //int转double
        method(10);
        int a= 30;
        method(a);
    }
    public static void method(double num){
        System.out.println(num);
    }
}

 

 

搜索

复制

标签:instanceof,USB,void,System,关键字,println,public,out
来源: https://www.cnblogs.com/pengtianyang/p/16440669.html