• Java冲突


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


     * 情况描述1:


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

    1. public class TestConflict {
    2. public static void main(String[] args) {
    3. Son s = new Son();
    4. s.play();
    5. }
    6. }
    7. //接口
    8. interface Friend{
    9. default void play(){
    10. System.out.println("跟朋友出去玩唱ktv");
    11. }
    12. }
    13. //父类
    14. class Father{
    15. public void play(){
    16. System.out.println("跟父亲出去钓鱼");
    17. }
    18. }
    19. class Son extends Father implements Friend {
    20. // 1.如果不重写冲突的方法,那么会保留父类的
    21. @Override
    22. public void play() {
    23. // 2.重写调用父类
    24. // super.play();
    25. // 3.父接口
    26. Friend.super.play();
    27. // 4.自己完全重写
    28. System.out.println("自己出去玩或者跟着momoko学java");
    29. }
    30. }

    情况描述2:

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

    1. public class TestConflict {
    2. public static void main(String[] args) {
    3. Girl girl = new Girl();
    4. girl.play();
    5. }
    6. }
    7. //接口1
    8. interface Friend{
    9. default void play(){
    10. System.out.println("跟朋友出去玩唱ktv");
    11. }
    12. }
    13. //接口2
    14. interface BoyFriend{
    15. default void play(){
    16. System.out.println("跟男朋友去约会");
    17. }
    18. }
    19. class Girl implements Friend,BoyFriend{
    20. @Override
    21. public void play() {
    22. // 1.保留1接口
    23. Friend.super.play();
    24. // 2.保留2接口
    25. BoyFriend.super.play();
    26. // 3.完全重写
    27. System.out.println("自己出去玩");
    28. }
    29. }

    情况描述3:

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

    1. public class TestConflict {
    2. public static void main(String[] args) {
    3. Girl girl =new Girl();
    4. girl.play();
    5. }
    6. }
    7. //接口1
    8. interface Friend{
    9. default void play(){
    10. System.out.println("跟朋友出去玩唱ktv");
    11. }
    12. }
    13. //接口2
    14. interface BoyFriend{
    15. default void play(){
    16. System.out.println("跟男朋友去约会");
    17. }
    18. }
    19. interface superFriend extends Friend,BoyFriend{
    20. // 接口解决冲突是要加上default
    21. @Override
    22. default void play() {
    23. Friend.super.play();
    24. }
    25. }
    26. class Girl implements superFriend{
    27. @Override
    28. public void play() {
    29. // superFriend.super.play();
    30. System.out.println("我自己再重写");
    31. }
    32. }

     情况描述4:


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

    1. public class TestSub {
    2. public static void main(String[] args) {
    3. SubClass sub = new SubClass();
    4. sub.method();
    5. }
    6. }
    7. class SubClass extends SuperClass implements SuperInterface,FatherInterface{
    8. public void method(){
    9. // 不清楚,你自己抉择->写清楚
    10. // System.out.println(x);
    11. // 通过父类.量名
    12. System.out.println("SuperClass的x"+ super.x);
    13. // 通过接口名.常量名
    14. System.out.println("SuperInterface的x"+ SuperInterface.x);
    15. System.out.println("FatherInterface的x"+ FatherInterface.x);
    16. }
    17. }
    18. //
    19. class SuperClass{
    20. int x = 1;
    21. }
    22. //接口1
    23. interface SuperInterface{
    24. // 其中public static final可以省略
    25. int x = 2;
    26. }
    27. //接口2
    28. interface FatherInterface{
    29. // 其中public static final可以省略
    30. int x = 3;
    31. }

    总结:

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

  • 相关阅读:
    node教程(四)Mongodb+mongoose
    计算机网络复习04——网络层
    网站部署SSL证书是否会影响网站流量?
    性能测试TPS/QPS/RT理解
    【牛客网面试必刷】链表篇
    秋风过处,五谷飘香
    Spring 注解开发及框架整合
    Git简单使用
    多模态表征—CLIP及中文版Chinese-CLIP:理论讲解、代码微调与论文阅读
    写个俄罗斯方块,感受嵌入式linux应用开发
  • 原文地址:https://blog.csdn.net/qq_57884857/article/details/138168322