Java冲突

本身  父类  接口(多) 如果出现同样名字的方法,就会出现冲突


 * 情况描述1:


 * 当一个类,继承了父类,实现了某接口,父类中的成员方法和接口中的方法重名
 * 解决方法:
 * 子类就近选择父类成员方法 亲爹优先原则
 *
 *使用格式:
 * 父类:super.方法名
 * 父接口:父接口名.super.方法名

public class TestConflict {
    public static void main(String[] args) {
        Son s = new Son();
        s.play();
    }
}
//接口
interface  Friend{
    default void play(){
        System.out.println("跟朋友出去玩唱ktv");
    }
}
//父类
class Father{
    public void play(){
        System.out.println("跟父亲出去钓鱼");
    }
}
class Son extends Father implements Friend {
//    1.如果不重写冲突的方法,那么会保留父类的
    @Override
    public void play() {
//        2.重写调用父类
//        super.play();
//        3.父接口
        Friend.super.play();
//        4.自己完全重写
        System.out.println("自己出去玩或者跟着momoko学java");
    }
}

情况描述2:

当一个类同时实现了多个接口,而多个接口中包含的方法出现了冲突(同名)
 * 都要做出选择
 *使用格式:
 * 父接口:父接口名.super.方法名

public class TestConflict {
    public static void main(String[] args) {
        Girl girl = new Girl();
        girl.play();
    }
}
//接口1
interface  Friend{
    default void play(){
        System.out.println("跟朋友出去玩唱ktv");
    }
}
//接口2
interface  BoyFriend{
    default void play(){
        System.out.println("跟男朋友去约会");
    }
}
class Girl implements Friend,BoyFriend{
    @Override
    public void play() {
//        1.保留1接口
        Friend.super.play();
//        2.保留2接口
        BoyFriend.super.play();
//        3.完全重写
        System.out.println("自己出去玩");
    }
}

情况描述3:

当一个类,实现了多个接口,由父接口继承后,来实现的,如果这种情况出现了冲突
 *解决方法:
 * 父接口已经解决了冲突,那么不需要再解决
 * 在接口中重写一定要加上default 而在实现类中重写不需要加上default
 

public class TestConflict {
    public static void main(String[] args) {
        Girl girl =new Girl();
        girl.play();
    }
}
//接口1
interface  Friend{
    default void play(){
        System.out.println("跟朋友出去玩唱ktv");
    }
}
//接口2
interface  BoyFriend{
    default void play(){
        System.out.println("跟男朋友去约会");
    }
}

interface superFriend extends Friend,BoyFriend{
//    接口解决冲突是要加上default
    @Override
    default void play() {
        Friend.super.play();
    }
}
class Girl implements superFriend{
    @Override
    public void play() {
//        superFriend.super.play();
        System.out.println("我自己再重写");
    }
}

 情况描述4:


 * 如果一个类,继承了父类,也实现了接口,呢么父类和父接口出现了量的冲突
 *
 * 解决方法:
 *  * 父类:super.量名
 *  * 父接口:父接口名.常量名
 *
 *  与方法冲突不同点:
 *  1.没有亲爹优先
 *  2.使用父接口的常量时,不加super
 

public class TestSub {
    public static void main(String[] args) {
        SubClass sub = new SubClass();
        sub.method();
    }


}
class SubClass extends SuperClass implements SuperInterface,FatherInterface{
    public void method(){
//        不清楚,你自己抉择->写清楚
//        System.out.println(x);
//        通过父类.量名
        System.out.println("SuperClass的x"+ super.x);
//        通过接口名.常量名
        System.out.println("SuperInterface的x"+ SuperInterface.x);
        System.out.println("FatherInterface的x"+ FatherInterface.x);
    }
}
//类
class SuperClass{
    int x = 1;
}
//接口1
interface  SuperInterface{
//    其中public static final可以省略
    int x = 2;
}
//接口2
interface  FatherInterface{
    //    其中public static final可以省略
    int x = 3;
}

总结:

当类在继承父类和实现接口中出现冲突的情况
 *方法冲突:
 *  1)父类和父接口中的方法名发生冲突  解决方案:默认亲爹优先  父类:super.方法名  父接口名.super.方法名
 *  2)实现多个接口中的方法名发生冲突  必须抉择(不然报错)  父接口名.super.方法名
 *  3)父接口继承了多个接口  解决方案:父接口解决冲突   子类中进行重写
 *  4)父类和父接口的量发生冲突     必须抉择(不然报错)  父类:super.量名 父接口:父接口名.常量名
 

  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值