JAVA多实现会出现的问题:接口冲突、重载模糊

===============================[color=red]接口冲突问题[/color]==============================

interface Ainterface {
public String say();
}
interface Binterface {
public int say();
}
/**
* 接口冲突的问题(不能被解决的问题)
* @author Ericy
* @version 1.0
*/
public class Interface implements Ainterface, Binterface {
// 提示错误一:The return type is incompatible with B.say()
// -->返回类型和B.say()方法不兼容
// 提示错误二:Duplicate method say() in type Interface
// -->在Interface类型中say()方法重复了
// 因为返回值类型不包含在方法的重载的范围内(方法重载:形式参数类型、个数、顺序的不同)
@Override
public String say() {
// TODO Auto-generated method stub
return null;
}
@Override
public int say() {
// TODO Auto-generated method stub
return null;
}
}

=================================[color=red]重载模糊问题[/color]===============================

interface A {}
interface B {}
/**
* C类实现A接口和B接口
* @author Ericy
* @version 1.0
*/
class C implements A, B {}

class D {
/**
* say(...)方法重载
* @param a A类型的参数
*/
public void say(A a) {
System.out.println("A");
}
/**
* say(...)方法重载
* @param b B类型的参数
*/
public void say(B b) {
System.out.println("B");
}
}
/**
* 重载模糊的问题(是可以被解决的问题)
* @author Ericy
* @version 1.0
*/
public class Overload {
public static void main(String[] args) {
D d = new D();
C c = new C();
// 这就是重载出现模糊的地方,编译器不知道要怎么识别c对象到底是A类型的还是B类型的。
// 解决该问题:你就必须告诉编译器我当前的这个c对象是A类型还是B类型的。
d.say((A) c);
d.say((B) c);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值