CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
(图)邪恶的韩国UMPC 使用 Java 编写数据库应用新规范
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

请教:我对接口的概念很模糊,那位大虾解释一下!!

楼主opk8848(opk8848)2005-01-07 13:40:35 在 Java / J2SE / 基础类 提问

我经常看到一个类implements某个接口,到底是什么意思啊1举个例子好么? 问题点数:50、回复次数:32Top

1 楼yiboo(生活在冻土的冰凌,爱上了沙漠的阳光)回复于 2005-01-07 13:43:52 得分 0

。。。。  
  建议多看书先  
  thinking   in   javaTop

2 楼caiyi0903(willpower)回复于 2005-01-07 13:47:14 得分 0

抽象类和接口你要区分开,通常认为接口是代替多重继承用的,其实并不全是这样。你学习了设计模式后,就会对接口的用处有个更高层次的了解!Top

3 楼javafaq2004(I will survive)回复于 2005-01-07 14:35:29 得分 0

就是可以这样操作了:  
   
  interface   I{}  
   
  class   C   implements   I{}  
   
  class   T{  
  I   i   =   new   C();  
  }Top

4 楼iforem(在咖啡香中醒来)回复于 2005-01-07 14:55:31 得分 5

1.接口用来约束对象的行为  
   
  2.接口用来隔离内部实现和外部调用  
   
  3....去看书...Top

5 楼opk8848(opk8848)回复于 2005-01-09 09:59:59 得分 0

非常感谢各位的支持!Top

6 楼classjava(原始野人)回复于 2005-01-09 10:33:26 得分 0

interface   Classjava  
  {  
  void   method();  
  }  
  public   class   Test   implements   Classjava  
  {  
  public   void   method()  
  {  
  System.out.println("接口的实现");  
  }  
  public   static   void   main(String[]   args)  
  {  
    new   Test().method();  
  }  
  }Top

7 楼sjg1981(★星の金币)回复于 2005-01-09 10:41:15 得分 0

补充一点:  
  如果想实现(implement)接口(interface)一定要完全实现interface中的所有方法,如果不能完全实现接口所有方法的类,我们称之为   abstract   class(抽象类).  
   
  共同进步!~Top

8 楼mrwest(James West)回复于 2005-01-09 10:43:19 得分 5

What   Is   an   Interface?    
   
   
  This   section   shows   you   how   to   create   and   to   use   interfaces   and   talks   about   why   you   would   use   an   interface   instead   of   a   class.    
  An   interface   defines   a   protocol   of   behavior   that   can   be   implemented   by   any   class   anywhere   in   the   class   hierarchy.   An   interface   defines   a   set   of   methods   but   does   not   implement   them.   A   class   that   implements   the   interface   agrees   to   implement   all   the   methods   defined   in   the   interface,   thereby   agreeing   to   certain   behavior.    
   
   
   
  --------------------------------------------------------------------------------  
  Definition:   An   interface   is   a   named   collection   of   method   definitions   (without   implementations).   An   interface   can   also   declare   constants.    
  --------------------------------------------------------------------------------  
   
   
  Because   an   interface   is   simply   a   list   of   unimplemented,   and   therefore   abstract,   methods,   you   might   wonder   how   an   interface   differs   from   an   abstract   class.   The   differences   are   significant.    
   
   
  An   interface   cannot   implement   any   methods,   whereas   an   abstract   class   can.    
  A   class   can   implement   many   interfaces   but   can   have   only   one   superclass.    
  An   interface   is   not   part   of   the   class   hierarchy.   Unrelated   classes   can   implement   the   same   interface.    
  Let's   set   up   the   example   we'll   be   using   in   this   section.   Suppose   that   you   have   written   a   class   that   can   watch   stock   prices   coming   over   a   data   feed.   This   class   allows   other   classes   to   register   to   be   notified   when   the   value   of   a   particular   stock   changes.   First,   your   class,   which   we'll   call   StockMonitor,   would   implement   a   method   that   lets   other   objects   register   for   notification:  
   
   
  public   class   StockMonitor   {  
            public   void   watchStock(StockWatcher   watcher,  
            String   tickerSymbol,   double   delta)   {  
            ...  
            }  
  }  
   
  The   first   argument   to   this   method   is   a   StockWatcher   object.   StockWatcher   is   the   name   of   an   interface   whose   code   you   will   see   in   the   next   section.   That   interface   declares   one   method:   valueChanged.   An   object   that   wants   to   be   notified   of   stock   changes   must   be   an   instance   of   a   class   that   implements   this   interface   and   thus   implements   the   valueChanged   method.   The   other   two   arguments   provide   the   symbol   of   the   stock   to   watch   and   the   amount   of   change   that   the   watcher   considers   interesting   enough   to   be   notified   of.   When   the   StockMonitor   class   detects   an   interesting   change,   it   calls   the   valueChanged   method   of   the   watcher.    
   
  The   watchStock   method   ensures,   through   the   data   type   of   its   first   argument,   that   all   registered   objects   implement   the   valueChanged   method.   It   makes   sense   to   use   an   interface   data   type   here   because   it   matters   only   that   registrants   implement   a   particular   method.   If   StockMonitor   had   used   a   class   name   as   the   data   type,   that   would   artificially   force   a   class   relationship   on   its   users.   Because   a   class   can   have   only   one   superclass,   it   would   also   limit   what   type   of   objects   can   use   this   service.   By   using   an   interface,   the   registered   objects   class   could   be   anything--Applet   or   Thread--for   instance,   thus   allowing   any   class   anywhere   in   the   class   hierarchy   to   use   this   service.  
   
   
  Top

9 楼sxy521(lilic)回复于 2005-01-09 11:19:29 得分 0

我觉得:接口好像是:为某个对象添加某一功能用的,  
  不能和抽象类混,因为抽象类是个无法实现的“对象”  
  接口:是“对象“的某个方法Top

10 楼opk8848(opk8848)回复于 2005-01-09 20:49:03 得分 0

非常感谢大家的支持,小弟受益匪浅,还请各位踊跃发言,把接口的概念搞清楚!Top

11 楼yqj2065(散分是美德)回复于 2005-01-10 02:21:11 得分 0

interface   纯粹是设计的一种表达方式,而类则是设计和实现的混合体。  
  ——GoslingTop

12 楼joyaga(joyaga)回复于 2005-01-10 08:16:41 得分 0

接口就是一个模式(高度抽象)   类是实现这种模式(相对具体化)Top

13 楼boyard(叶落知秋)回复于 2005-01-10 10:02:06 得分 10

接口用来描述类的功能,而不指明具体的实现方法。一个类可以实现一个或多个接口。  
  只要类实现了接口,就可以在任何需要该接口的地方使用这个类的对象。  
  在Java中,接口不是类,而是一组对类的要求,这些类要与接口一致。Top

14 楼opk8848(opk8848)回复于 2005-01-10 21:11:42 得分 0

boyard说的棒极了!Top

15 楼opk8848(opk8848)回复于 2005-01-10 21:15:21 得分 0

如果把类中的方法设为public,不是跟接口一样了么?还要接口干什么?请解释!谢谢!Top

16 楼Dan1980()回复于 2005-01-10 22:16:04 得分 5

To   楼上的:不是说了吗?接口是一系列功能的抽象定义,而类中的方法是要有实现的。  
   
  个人认为,接口是一种更加全局化的设计,它使得所有实现了它的类都具有类似的行为,而且还可以在需要的时候使用“UpCast”将它们相同对待。Top

17 楼skylan(君若为云,我愿为风,领君遨游宇宙中)回复于 2005-01-11 08:44:08 得分 0

接口不是类,它用来描述类的功能,对类有什么要求,但是不指明具体的实现方式Top

18 楼zonelive(peter)回复于 2005-01-11 09:03:54 得分 0

那它好比一个什么  
  听了那么多也是些抽想的东西  
  好象不好它直接用类就可以实现  
  它到底起到一个什么作用呢Top

19 楼opk8848(opk8848)回复于 2005-01-11 10:47:20 得分 0

哦   原来是这样!但我感觉,每个类都可以implement接口,是不是很混乱阿!Top

20 楼gaoan(流浪花---不迁怒,不二过.)回复于 2005-01-11 11:00:12 得分 0

默默的顶。。。。。。。。。。。。。Top

21 楼shenyouth(新晴)回复于 2005-01-11 11:01:19 得分 5

public   interface   Actionlistener(){      
   
    public   abstract   void   actionPerformed(ActionEvent   event);      
   
    }    
   
    只有实现了这个接口(重写这个接口中的唯一一个方法),你才有资格去事件监听器列表里注册(参数为Actionlistener类型),当事件源变动时,自动调用这个唯一的actionPerformed方法.Top

22 楼opk8848(opk8848)回复于 2005-01-12 09:53:19 得分 0

upTop

23 楼xiaopeipei2004(小裴)回复于 2005-01-12 09:58:02 得分 0

顶,不明白接口有什么用,不用不是更好。哪位大哥解释一下。Top

24 楼yqj2065(散分是美德)回复于 2005-01-12 12:29:11 得分 0

给我100分,给你1000字的解释。呵呵哈,不是一句话的问题。  
   
  简单的说,理解interface   要站在“设计、   OO   design”的角度去考虑,不要考虑功能实现的问题。Top

25 楼prok(换换口味)回复于 2005-01-12 14:14:34 得分 0

没有接口,你实现个command都麻烦.Top

26 楼opk8848(opk8848)回复于 2005-01-12 18:20:02 得分 0

upTop

27 楼Dan1980()回复于 2005-01-13 12:17:36 得分 20

我举个例子,欢迎大家批评指正!   (不止1000字啊,楼主要给我高分哦!!!)  
   
  假设我们要为篮球设计一个类Basketball,由于篮球是球形物体,可以从类Ball继承而来,类Ball定义了一般球形物体的特性,如半径,滚动等。  
   
  //Ball.java  
   
  public   class   Ball   {  
   
          protected   long   radius;  
   
          public   Ball   (long   radius)   {  
                  this.radius   =   radius;  
          }  
   
          public   void   roll   (long   distance)   {  
                  System.out.println("Rolling   for   "   +   distance);  
          }  
   
  }  
   
   
  //BasketBall.java  
   
  public   class   BasketBall   extends   Ball   {  
   
          public   BasketBall   (long   radius)   {  
                  super(radius);  
          }  
   
  }  
   
  因为只要是球形物体就可以滚动,篮球继承了球形物体的所有特性,当然也可以。但是和其他球形物体(如地球)不同,篮球是可以拿来玩的。我们会想到在类Basketball里面来实现这部分功能,修改Basketball为:  
   
  //BasketBall.java  
   
  public   class   BasketBall   extends   Ball   {  
   
          public   BasketBall   (long   radius)   {  
                  super(radius);  
          }  
   
          public   String   play   ()   {  
                  return   "Interesting...";  
          }  
   
  }  
   
  一个学生可以玩篮球,但是他/她当然不希望自己只能玩篮球,而不能玩其它的东西。如果把所有可以玩的东西都罗列一遍,那将是愚蠢的,能玩的东西太多了:  
   
  //Student.java  
   
  public   class   Student   {  
   
          public   void   play(Basketball   nikes)   {   }  
   
          //public   void   play(Football   nikes)   {   }  
          //public   void   play(Tabletennis   nikes)   {   }  
          //There   will   be   MUCH   more...  
   
  }  
   
  但是,如果把"可以拿来玩"定义为一项抽象功能(即接口),并且让任何对象选择性的去实现它的话,我们就可以确定哪些对象是可以玩的,而哪些不是了。  
   
  //Playable.java  
   
  public   interface   Playable   {  
   
          public   String   play();  
   
  }  
   
  下面用接口的方法重新实现篮球:  
   
  //Basketball.java  
   
  public   class   BasketBall   extends   Ball   implements   Playable   {  
   
          public   BasketBall   (long   radius)   {  
                  super(radius);  
          }  
   
          public   String   play   ()   {  
                  return   "Interesting...";  
          }  
   
  }  
   
  同样的方法可以实现任何可以玩的物体,不管它是不是球类:  
   
  //Football.java  
   
  public   class   FootBall   extends   Ball   implements   Playable   {  
   
          public   BasketBall   (long   radius)   {  
                  super(radius);  
          }  
   
          public   String   play   ()   {  
                  return   "Not   so   interesting...";  
          }  
   
  }  
   
  //Computer.java  
   
  public   class   Computer   implements   Playable   {  
   
          public   Computer   ()   {   }  
   
          public   String   play   ()   {  
                  return   "Very   interesting...";  
          }  
   
  }  
   
  这样一个学生就可以是:  
   
  //Student.java  
   
  public   class   Student   {  
   
          public   void   play(Playable   aThing)   {   }  
   
  }  
   
  JAVA规定,实现一个接口的类必须实现接口里的所有方法,因为如果你不去实现,就没有implements那个接口的必要,这是显而易见的。  
   
  上面的例子也同样可以解释为什么接口方法一定是抽象的,因为不同对象有不同的实现细节,比如同样是可以玩的物体,有些很有趣,而有些就不是那么有趣。  
   
  怎么样?用了接口以后,程序是不是变得合理多了?Top

28 楼opk8848(opk8848)回复于 2005-01-13 19:51:15 得分 0

请问怎么结贴?Top

29 楼programer23(亮子)回复于 2005-01-13 21:59:22 得分 0

接口就是规范!  
  你满足条件了就可以操作!漫漫理解吧!Top

30 楼yqj2065(散分是美德)回复于 2005-01-14 00:20:51 得分 0

Dan1980((   努力泡分中))   :  
  和我抢分,呵呵。  
  基本正确。  
   
  "JAVA规定,实现一个接口的类必须实现接口里的所有方法,因为如果你不去实现,就没有implements那个接口的必要,这是显而易见的。"不太正确。实现一个接口的类可以成为抽象类。  
   
   
  Top

31 楼Dan1980()回复于 2005-01-14 09:50:39 得分 0

To   yqj2065(严千钧):  
   
  谢谢!我不是抢分啊!我就是想把我的理解说出来让大家指点指点,自我提高一下。我也很菜的。  
   
  你说的对,“实现一个接口的类可以成为抽象类”,这是一种整体设计的考虑。Top

32 楼opk8848(opk8848)回复于 2005-01-14 15:48:51 得分 0

怎么结贴?Top

相关问题

  • 我有些概念模糊?
  • 对----Visual Studio2005概念模糊
  • Ejb Home接口 概念问题
  • 请教接口与类与类接口与COM的概念??
  • 概念模糊,不懂就问?
  • 一个有些模糊的概念
  • 关于INCLUDE的一个模糊概念
  • 模糊的概念:什么是模块?
  • 一些比较模糊的概念
  • 大师们,请解释这个概念!!

关键词

  • 接口
  • 物体
  • javapublic
  • basketball
  • 实现
  • 类
  • radius
  • playable
  • 球形物体
  • 抽象

得分解答快速导航

  • 帖主:opk8848
  • iforem
  • mrwest
  • boyard
  • Dan1980
  • shenyouth
  • Dan1980

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
惹火投票。。火热进行中...

社区焦点:

教你怎样用C#搞笑整人
最懒惰的程序员写的Cache
程序员如何掌握专业英语
Java栈与堆
分享:让人懊恼的面试
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo