codeismylife
V2EX  ›  问与答

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

  •  
  •   codeismylife · Jan 21, 2019 · 2264 views
    This topic created in 2866 days ago, the information mentioned may be changed or developed.
    4 replies  •  2019-01-21 11:27:34 +08:00
    zjp
    zjp
    Jan 21, 2019
    1
    https://www.v2ex.com/t/396685
    我倾向于认为是作者的个人习惯和语义问题。List 的大小是 `size` 而不是 `length` 经常让我很困惑...
    codeismylife
    codeismylife
    OP
    Jan 21, 2019
    2
    我更倾向于架构方面。如果接口继承层级太多不容易看清楚,重复声明就不用向上查看。
    bqbkbz
    bqbkbz
    Jan 21, 2019
    3
    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
    codeismylife
    OP
    Jan 21, 2019
    4
    @zhaohui318 原……原来如此
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   920 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:30 · PVG 05:30 · LAX 13:30 · JFK 16:30
    ♥ Do have faith in what you're doing.