`
sober1988
  • 浏览: 8984 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Interface与Object之间的关系问题

SUN 
阅读更多

最近在想Interface与Object之间的关系问题?
我们都知道,在JAVA中,所有的类都是继承了Object类,但是接口呢?
在我们定义接口时,是不需要显示 extends java.lang Object
但是一个接口没有继承Object类,但我们在任何接口上调用Object类的方法,编译器都不报错,这种现象该如何解释呢?例如,下面的代码中 Runnable是一个接口,但我们针对这个接口调用了Object类的一些方法,可以成功通过编译,是不是编译器对这种情况进行了特殊对待?
class InterfaceAndObject
{
  public void interfaceTest(Runnable r)
  {
    System.out.println(r.toString() + r.hashCode());
  }
}

去看Sun的官方文档TJLS(The Java Language Specification)吧!其中第9章9.2节关于接口有这么一段话:

If an interface has no direct superinterfaces, then the interface implicitly
declares a public abstract member method m with signature s, return type r,
and throws clause t corresponding to each public instance method m with
signature s, return type r, and throws clause t declared in Object, unless a
method with the same signature, same return type, and a compatible throws
clause is explicitly declared by the interface. It is a compile-time error if the
interface explicitly declares such a method m in the case where m is declared to
be final in Object.

大概意思是接口隐含定义了一套与Object类中的方法签名完全相同的方法,所以,我们在程序中调用接口的那些与Object中具有相同签名的方法时,编译器不会报错!
这段描述对我很有帮助,说了这么多,只是想让大家在空闲时间来考虑JAVA的设计思路和理念,巩固和加深对它的理解.
分享到:
评论

相关推荐

    ArcObject 10.3.1 For Java 对象模型(ODM)

    ArcObject的对象模型图(ODM),ArcGIS10.3.1 For Java版本,ArcObject是ArcGIS Desktop、ArcGIS Engine、ArcGIS Server共用的底层组件集,文档中说明ArcObject里对象之间的关系,可以用于ArcGIS Engine、ArcGIS ...

    Spring.net框架

    在这个设计中,组件与组件之间、类与类之间存在着紧密的耦合关系。SayHello类中的_helloGen字段类型为 EnHelloGenerator,这将导致我们很难给它赋予一个其它的HelloGenerator(例如CnHelloGenerator,用于生成 中文...

    PHP实战:对象,设计,敏捷(PHP in Action :Objects, Design, Agility )

    5 了解类之间的关系 (Understanding class relationships) 6 面向对象原理 (Object-oriented principles) 7 设计模式 (Design patterns) 8 怎样设计的问题:日期和时间处理 (Design how-to: date and time handling)...

    JAVA面向对象详细资料

    21 类与类之间的关系 32 22 GC:垃圾回收机制 33 23 Object类 33 23.1 常用方法:boolean equals(Object) 33 23.2 常用方法:String toString() 34 24 继承整理 34 25 面向对象三大特性:多态(核心) 34 25.1 多态...

    重构_改善既有代码的设计[高清版]中文版

     Problems with Refactoring 重构的问题   Refactoring and Design 重构与设计   Refactoring and Performance 重构与性能   Where Did Refactoring Come From? 重构的起源  Chapter 3:Bad Smells in ...

    重构-改善既有代码的设计+中文版

     Problems with Refactoring 重构的问题   Refactoring and Design 重构与设计   Refactoring and Performance 重构与性能   Where Did Refactoring Come From? 重构的起源  Chapter 3:Bad Smells in Code...

    重构——改善既有代码的设计

     Problems with Refactoring 重构的问题   Refactoring and Design 重构与设计   Refactoring and Performance 重构与性能   Where Did Refactoring Come From? 重构的起源  Chapter 3:Bad Smells in Code...

    重构-改善既有代码的设计(中文版)

     Problems with Refactoring 重构的问题   Refactoring and Design 重构与设计   Refactoring and Performance 重构与性能   Where Did Refactoring Come From? 重构的起源  Chapter 3:Bad Smells in Code...

    重构,改善既有代码的设计

     Problems with Refactoring 重构的问题   Refactoring and Design 重构与设计   Refactoring and Performance 重构与性能   Where Did Refactoring Come From? 重构的起源  Chapter 3:Bad Smells in ...

    重构 改善既有代码的设计

     Problems with Refactoring 重构的问题   Refactoring and Design 重构与设计   Refactoring and Performance 重构与性能   Where Did Refactoring Come From? 重构的起源  Chapter 3:Bad Smells in ...

    数据库系统的一些专用词汇表.txt

    外部关键字(Forgien Key): 在一个关系中含有的与另一个关系的关键字相对应的属性组称为该关系的外部关键字。外部关键字取空值或为外部表中对应的关键字值。 . 网络库(Net-Library): 一个通信软件包,对数据库...

    Java相关技术(详细资料)

     4、Java IDL(Java Interface Definition Language) 提供与CORBA(Common Object Request Broker Architecture)的无缝的互操作性。这使得Java能集成异构的商务信息资源。  5、JNDI(Java Naming and Directory ...

    重构-改善既有代码的设计 中文版.pdf

    11.8 Extract Interface(提炼接口) 11.9 Collapse Hierarchy(折叠继承体系) 11.10 Form Template Method(塑造模板函数) 11.11 Replace Inheritance with Delegation(以委托取代继承) 11.12 Replace ...

    重构-改善既有代码的设计 中文版

    11.8 Extract Interface(提炼接口) 11.9 Collapse Hierarchy(折叠继承体系) 11.10 Form Template Method(塑造模板函数) 11.11 Replace Inheritance with Delegation(以委托取代继承) 11.12 Replace ...

    重构:改善既有代码的设计.[美]Martin Fowler.epub【文字版手机格式】

    11.8 Extract Interface(提炼接口) 11.9 Collapse Hierarchy(折叠继承体系) 11.10 Form Tem Plate Method(塑造模板函数) 11.11 Replace Inheritance with Delegation(以委托取代继承) 11.12 Replace Delegation...

    重构-改善既有代码的设计

    11.8 Extract Interface(提炼接口) 341 11.9 Collapse Hierarchy(折叠继承体系) 344 11.10 Form Tem Plate Method(塑造模板函数) 345 11.11 Replace Inheritance with Delegation(以委托取代继承) ...

    C#微软培训资料

    7.4 关系操作符和关系表达式.65 <<page 2>> page begin==================== 7.5 逻辑操作符和逻辑表达式.68 7.6 位 运 算 .69 7.7 其它特殊操作符 .72 7.8 小 结 .77 第八章 流 程 控 制 .79 8.1 ...

    java 面试题 总结

    重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,...

    超级有影响力霸气的Java面试题大全文档

    重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,...

    千方百计笔试题大全

    92、一般情况下,关系数据模型与对象模型之间有哪些匹配关系(A,B,C) 21 93、以下关于SessionFactory的说法哪些正确?(B,C) 21 94、元素有一个cascade属性,如果希望Hibernate级联保存集合中的对象,casecade属性...

Global site tag (gtag.js) - Google Analytics