问题
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