简体   繁体   English

Javadoc方法和继承的方法

[英]Javadoc methods and inherited methods

In javadoc, why is there a separate section for inherited methods? 在javadoc中,为什么对继承的方法有单独的部分? For example in LinkedList, http://docs.oracle.com/javase/1.5.0/docs/api/java/util/LinkedList.html , there is a Method Summary section and a section for inherited methods, like "Methods inherited from interface java.util.List". 例如,在LinkedList中, http://docs.oracle.com/javase/1.5.0/docs/api/java/util/LinkedList.html中 ,有一个“方法摘要”部分和一个有关继承方法的部分,例如“继承的方法”来自接口java.util.List”。 Some of the method in Method Summary are inherited from other classes so why is there this separate section? “方法摘要”中的某些方法是从其他类继承的,那么为什么有单独的部分? How does javadoc decide which methods to put in the Method Summary and which in the inherited section (even when a method in Method Summary is inherited)? javadoc如何确定要在“方法摘要”中放置哪些方法以及在“继承的”部分中放置哪些方法(即使继承了“方法摘要”中的方法)?

It uses the Reflection-API to decide whether a method is inherited or not. 它使用Reflection-API来确定是否继承方法。

  • getMethod() returns inherited methods getMethod()返回继承的方法
  • getDeclaredMathod() returns all methods getDeclaredMathod()返回所有方法

for further references see also Discovering Class Members 有关更多参考,请参见发现班级成员

An example that demonstrates the usage of the API 一个演示API用法的示例

public class Reflect extends ArrayList {
    public static void main(String[] args) {
        Reflect r = new Reflect();
        r.dump();
    }

    private void dump() {
        Method[] methods = this.getClass().getMethods();
        Set<String> ms = new  HashSet<String>();
        for ( Method m : methods ) {  
            ms.add( m.getName() );
        }
        Method[] declMethods = this.getClass().getDeclaredMethods();
        Set<String> ds = new  HashSet<String>();
        for ( Method m : declMethods ) {  
            ds.add( m.getName() );
        }
        for ( String name : ms ) {
            System.out.println(name + " is inherited =" +! ds.contains(name));
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM