Java代码实例

Java_实例_接口实例

2018-04-27  本文已影响0人  Ethan丶Xiao
/**
 * 接口实例
 * @author 肖
 *
 */
interface IUSB{//定义一个IUSB接口
    void swapDate();
}

class Mouse implements IUSB{//定义一个Mouse类实现IUSB接口
    //实现接口方法
    public void swapDate() {
        System.out.println("Mouse...oning");
    }
}

class Print implements IUSB{//定义一个Print类实现IUSB接口
    //实现接口方法
    public void swapDate() {
        System.out.println("Print...oning");
    }
}

class MotherBind{//定义一个主板类,将实现IUSB接口的类插入主板
    /**
     * 定义一个IUSB类型的数组,数组容量为6
     */
    private static IUSB[] uss = new IUSB[6];
    private static int index = 0;
    /**
     * 在数组的容量类可以插入IUSB设备
     * @param usb
     */
    public static void gets(IUSB usb) {
        if(index == uss.length) {
            System.out.println("已满");
            return;
        }
        uss[index] = usb;
        index++;
    }
    /**
     * 让实现IUSB接口的类工作
     */
    public static void wolk() {
        for(IUSB usb : uss) {
            if(usb != null) {
                usb.swapDate();
            }
        }
    }
}

public class IntDemo {

    public static void main(String[] args) {
        /**
         * 插入实现IUSB接口的设备,如果插入的数量大于6,会显示"插满"
         */
        MotherBind.gets(new Mouse());
        MotherBind.gets(new Print());
        
        MotherBind.wolk();
    }

}

上一篇 下一篇

猜你喜欢

热点阅读