V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
codeismylife
V2EX  ›  问与答

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

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