Is there any way to include the name of the interface when implementing the method

荒凉一梦 提交于 2019-12-24 09:41:40

问题


Is there any way to include the name of the interface when implementing the method? If I have to implement 3 interfaces, then it would be hard to remind me where the implemented method comes from.

If I have 2 interface required to implement the same method name. How can I tell which method I am implementing?

public interface BarInt {
void method();
}
public interface GeeInt{
void method();
}
public class Foo implements BarInt, GeeInt{

@Override
public void method() {
    // TODO Auto-generated method stub

}
}

Thanks


回答1:


Yes, you can just use a @see javadoc comment

public interface BarInt {
    void method();
}

public class Foo implements BarInt{

    /**
     * @see BarInt#method()
     */    
    @Override 
    public void method() {
        // TODO Auto-generated method stub
    }
}



回答2:


Novaterata's answer in good, this is a similar approach with annotations.


You can always roll your own annotations.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Source {

    Class value();

}

Then you can use it like this:

@Source(List.class)
@Override
public boolean add(Object o) {
    return false;
}

With the retention policy of Source the annotation will not be part of the bytecode. It only serves as additional information for the reader, IDE and compiler.




回答3:


Design wise name of method would reflect what the method does and you should be able to co-relate with the Interface the method belongs to.

De-bugging wise, most editor will take you the implemented Interface and method on few key strokes/clicks.



来源:https://stackoverflow.com/questions/45090032/is-there-any-way-to-include-the-name-of-the-interface-when-implementing-the-meth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!