签名?什么签名?看不懂。 James gosling是java语言的创始人,他不会搞错的。请把英语原文贴出来。
Thinking in java 中有說明 也就是java無法僅根據返回值類型區分方法 fantasyCoder(牛仔+T恤)說的很清楚了-- 这个我知道,我意思是头一段说接口可以继承相同签名的多个方法,为什么后面又说两个继承来的方法,只是在返回值上不一样,那么就会报错 --這里指的應該是多態
我查了一下,英文版应该是112页的最后一段: If an interface inherits more than one method with the same signature, or a class implements different interfaces containing a method with the same signature,there is only one such method- whose implementation is ultimately defined by the class implementing the interfaces.There is no question of ambiguity in this case. 这段话的意思是这样的,假设有两个接口A和B,各提供一个方法,接口C 继承A和B: interface A{ void func(); } interface B{ void func(); } interface C extends A,B{ //...... } 很明显,C中不可能有两个void func() 同样的,假设有一个类D现A,B接口: class D implements A,B{ void func(); } D中也只能有一个func() Java不能只根据返回值区别不同方法,如果是下面这样写 就会报错: interface A{ void func(); } interface B{ int func(); } interface C extends A,B{ void func(); int func(); } class D implements C{ void func(); int func(); }(*)翻译有问题 “如果某个声明的方法和继承方法,或者两个继承来的方法,只是在 返回值不一样,那么就会报错。” | 的类型上 p113: If a declared method differs only in return type from an inherited method, or if two inherited methods differ only in return type, then an error occurs.
OK 我想我知道是怎么回事了 谢了! 但又产生两个新问题: interface A{ void func(); } interface B{ void func(); } class D implements A,B{ void func(); } 1.那么D中的实现的void func()是接口A的还是接口B的? 2.这样一来D是不是应该被声明为abstract的,因为A和B中肯定有一个接口的方法没有被全部实现。
这个我知道,我意思是头一段说接口可以继承相同签名的多个方法,为什么后面又说两个继承来的方法,只是在返回值上不一样,那么就会报错
James gosling是java语言的创始人,他不会搞错的。请把英语原文贴出来。
也就是java無法僅根據返回值類型區分方法
fantasyCoder(牛仔+T恤)說的很清楚了--
这个我知道,我意思是头一段说接口可以继承相同签名的多个方法,为什么后面又说两个继承来的方法,只是在返回值上不一样,那么就会报错
--這里指的應該是多態
If an interface inherits more than one method with the same
signature, or a class implements different interfaces containing
a method with the same signature,there is only one such method-
whose implementation is ultimately defined by the class implementing
the interfaces.There is no question of ambiguity in this case. 这段话的意思是这样的,假设有两个接口A和B,各提供一个方法,接口C
继承A和B:
interface A{
void func();
}
interface B{
void func();
}
interface C extends A,B{
//......
}
很明显,C中不可能有两个void func()
同样的,假设有一个类D现A,B接口:
class D implements A,B{
void func();
}
D中也只能有一个func() Java不能只根据返回值区别不同方法,如果是下面这样写
就会报错:
interface A{
void func();
}
interface B{
int func();
}
interface C extends A,B{
void func();
int func();
}
class D implements C{
void func();
int func();
}(*)翻译有问题
“如果某个声明的方法和继承方法,或者两个继承来的方法,只是在
返回值不一样,那么就会报错。”
|
的类型上
p113:
If a declared method differs only in return type from an
inherited method, or if two inherited methods differ only in
return type, then an error occurs.
我想我知道是怎么回事了
谢了!
但又产生两个新问题:
interface A{
void func();
}
interface B{
void func();
}
class D implements A,B{
void func();
}
1.那么D中的实现的void func()是接口A的还是接口B的?
2.这样一来D是不是应该被声明为abstract的,因为A和B中肯定有一个接口的方法没有被全部实现。
void func();
}
interface B{
void func();
}
class D implements A,B{
void func();
}//这样编译肯定会出错的!
B被忽略了。interface A{
void func();
}
interface B{
void func();
}
public class D implements A,B{
public void func(){
System.out.println("No problem!");
}
public static void main(String[] args){
D d=new D();
d.func();
}
}