luyantl 发表于 2018-9-14 00:16:30

JavaDemo_0914_0006接口

interface IUSB
{
        public abstract boolean check();
        public abstract void work();
}
//***************************************
class Computer
{
        public void plugin(IUSB usb)
        {
                if(usb.check())
                {
                        usb.work();
                }
        }
}
//*************************************
class Keyboard implements IUSB
{
        public boolean check()
        {
                return true;
        }
        public void work()
        {
                System.out.println("Keyboard类==>开始进行码字");
        }
}
//*****************************************
class Print implements IUSB
{
        public boolean check()
        {
                return false;
        }
        public void work()
        {
                System.out.println("Print-->开始进行打印工作");
        }
}
//**************************************
public class JavaDemo_0914_0006
{
        public static void main(String[] args)
        {
                Computer computer=new Computer();
                computer.plugin(new Keyboard());
                computer.plugin(new Print());
        }
}

luyantl 发表于 2018-9-14 00:22:16

封装、继承、多态,重载、覆写、向上转型、向下转型
页: [1]
查看完整版本: JavaDemo_0914_0006接口