`
alexgreenbar
  • 浏览: 87722 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

hard to resolve method signature conflict in two interfaces

    博客分类:
  • Java
阅读更多
sometimes, it need implement two interfaces, like:

public interface IAface {
  public static final String A = "AFACE";
  public int doSmile();
}

public interface IBface {
  public static final String B = "BFACE";
  public String doSmile();
}


usually, it can be done like:

public class Face implements IAface, IBface {
  public int doSmile() {
    ...
  }

  public String doSmile() {
    ...
  }
}


then Java compile will complain on can't different those two methods which you must implement in your concrete class, so here have two choices:

1. if those two interfaces are own by you, then you can change the signature of those methods

2. otherwise, you can write a abstract class first like:
public abstract class MyAface implements IAface {
  public int doSmile() {
    return -1;
  }

  public abstract int myDoSmile();

}


then let your concrete class extends your abstract class and implements IBface again like:
public class Face2 extends MyAface implements IBface {
  public int myDoSmile() {
    ...
  }
  
  public String doSmile() {
    ...
  }
}


this fix the name conflict, but lost interface value because when do like:
  IAface aface = new Face2();
  aface.doSmile(); //ok
  aface.myDoSmile(); //can't do like this


it may even worse when you haven't the right to change source code of IAface and IBface, so seems pay attention to naming in programming is so important, but sometimes it can't guarantee all names won't conflict especially when you want to extend/implement others' code

what's your insight?
分享到:
评论
3 楼 歆渊 2007-08-09  
启用类似 ActionScript 3.0 中 namespace 的机制就很容易解决这种问题了, 同名方法甚至其他语法元素只要标记为不同的 namespace 就可以在同一个 scope 中共存. 希望 Java 7 或者后面什么版本能引入这个特性.

更详细的信息见我前面一篇文章.
2 楼 Qieqie 2007-08-08  
alexgreenbar 写道

it may even worse when you haven't the right to change source code of IAface and IBface, so seems pay attention to naming in programming is so important, but sometimes it can't guarantee all names won't conflict especially when you want to extend/implement others' code

what's your insight?



public class AFaceAdapter implements IAFace {
    private Face face;
    public IAFaceWarp(Face face) {
        this.face = face;
    }
    public int doSmile() {   
        return this.face.doASimle();
    }  
}

public class BFaceAdapter implements IBFace {
    private Face face;
    public IBFaceWarp(Face face) {
        this.face = face;
    }
    public String doSmile() {   
        return this.face.doBSimle();
    }  
}

public class Face {
    public int doASimle() {   
        return 1;
    } 

    public String doBSimle() {   
        return "2";
    } 
}


public class Main {
    public static void main(String[] a) {
        Face face = new Face();
        IAFace aface = new AFaceAdapter(face);
        IBFace bface = new BFaceAdapter(face);
        aface.doSimle();
        bface.doSimle();
    }
}
1 楼 tonyyl 2007-08-08  
learned,thx!

相关推荐

Global site tag (gtag.js) - Google Analytics