首页 > 其他 > 详细

使用接口定义标准

时间:2015-03-17 02:10:08      阅读:340      评论:0      收藏:0      [点我收藏+]

范例:定义一个USB的标准

interface USB {   // 操作标准       

         public void install() ;

         public void work() ;

}

范例:在电脑上应用此接口

class Computer {

         public void plugin(USB usb) {

                   usb.install() ;

                   usb.work() ;

         }

}

范例:定义USB设备

class Phone implements USB {

         public void install() {

                   System.out.println("安装手机驱动程序。") ;

         }

         public void work() {

                   System.out.println("手机与电脑进行工作。") ;

         }

}

范例:定义USB设备

class Print implements USB {

         public void install() {

                   System.out.println("安装打印机驱动程序。") ;

         }

         public void work() {

                   System.out.println("进行文件打印。") ;

         }

}

范例:连接

interface USB {   // 操作标准       

         public void install() ;

         public void work() ;

}

class Computer {

         public void plugin(USB usb) {

                   usb.install() ;

                   usb.work() ;

         }

}

class Phone implements USB {

         public void install() {

                   System.out.println("安装手机驱动程序。") ;

         }

         public void work() {

                   System.out.println("手机与电脑进行工作。") ;

         }

}

class Print implements USB {

         public void install() {

                   System.out.println("安装打印机驱动程序。") ;

         }

         public void work() {

                   System.out.println("进行文件打印。") ;

         }

}

public class Test {

         public static void main(String args[]) {

                   Computer c = new Computer() ;

                   c.plugin(new Phone()) ;         // USB usb = new Phone() ;

                   c.plugin(new Print()) ;

         }

}


使用接口定义标准

原文:http://9882931.blog.51cto.com/9872931/1621277

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!