The Java Tutorials: Defining Methods – Overloading Methods州,The Java programming language supports overloading methods,and Java
can distinguish between methods with different method signatures. This
means that methods within a class can have the same name if they have
different parameter lists.
也,
You cannot declare more than one method with the same name and the
same number and type of arguments,because the compiler cannot tell
them apart.
The compiler does not consider return type when differentiating
methods,so you cannot declare two methods with the same signature
even if they have a different return type.
两个实现的方法共享一个常用的方法签名(即data()),因此编译器不能区分两者,并且将具有满足两个接口契约的单一方法.
编辑:
例如,
public class Foo implements IFoo,IBar{
public static void main(String[] args) {
Foo foo = new Foo();
((IFoo) foo).print();
((IBar) foo).print();
}
@Override
public void print() {
System.out.println("Hello,World!");
}
}
public interface IBar {
void print();
}
public interface IFoo {
void print();
}
将输出,
Hello,World!
Hello,World!