V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
codeismylife
V2EX  ›  问与答

为什么 Java 的 list 接口和 collection 接口都定义了 size()方法。list 继承了 collection,为什么要重复定义一遍。

  •  
  •   codeismylife · 2019-01-21 11:01:48 +08:00 · 1495 次点击
    这是一个创建于 1914 天前的主题,其中的信息可能已经有所发展或是发生改变。
    4 条回复    2019-01-21 11:27:34 +08:00
    zjp
        1
    zjp  
       2019-01-21 11:08:45 +08:00
    https://www.v2ex.com/t/396685
    我倾向于认为是作者的个人习惯和语义问题。List 的大小是 `size` 而不是 `length` 经常让我很困惑...
    codeismylife
        2
    codeismylife  
    OP
       2019-01-21 11:18:19 +08:00
    我更倾向于架构方面。如果接口继承层级太多不容易看清楚,重复声明就不用向上查看。
    zhaohui318
        3
    zhaohui318  
       2019-01-21 11:21:40 +08:00
    The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.

    https://docs.oracle.com/javase/8/docs/api/java/util/List.html

    在 list 里再抄一遍是为了文档看起来方便
    codeismylife
        4
    codeismylife  
    OP
       2019-01-21 11:27:34 +08:00
    @zhaohui318 原……原来如此
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2719 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 00:22 · PVG 08:22 · LAX 17:22 · JFK 20:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.