一种在没有警告的情况下创建匹配的泛型类对的方法?

时间:2011-01-08 03:00:49

标签: java generics

我想知道当我使用相互引用的泛型类时,是否有办法消除rawtypes警告:

public class DummyDeleteMe {
abstract class RightSide<L extends LeftSide>{ //Can I use a type parameter here?
    L getLeftSide() {return _mate;}
    void setLeftSide(L mate) {_mate = mate;}
    L _mate;
    void connectToMate(){
        getLeftSide().setRightSide(this);//warning: 
        /** [unchecked] unchecked call to setRightSide(R) as a member of the raw type DummyDeleteMe.LeftSide
         where R is a type-variable:  * **/
    }
}

abstract class LeftSide<R extends RightSide>{// And here?
    R getRightSide(){return _mate;}
    void setRightSide(R mate) {_mate = mate;}
    R _mate;
}
 class RightSideSub extends RightSide<LeftSideSub>{
    void connectToMate(){
        getLeftSide().setRightSide(this);//No warning
    }
 }
  class LeftSideSub extends LeftSide<RightSideSub>{}
}

编译器警告是因为参数绑定中的LeftSide是原始类型。用LeftSide<?>替换它会导致connectToMate出错。覆盖connectToMate中的RightSideSub,相同的代码不会生成警告。

我想我正在寻找某种第二种类型的参数,即自我参照:

abstract class RightSide<R extends RightSide<R,L>, L extends LeftSide<L,R>>{}

但是这会导致返回R的{​​{1}}变量和方法中的其他类型不匹配错误。

2 个答案:

答案 0 :(得分:4)

我知道这可能不是你想要的,但它可以解决你的问题。仅基于您在代码示例中显示的内容,根本不需要通用。这让问题复杂化了。也就是说,如果您的示例与您需要的内容相当匹配。

假设是,那么这样的事情。

public interface Half{
    public Half getOtherHalf();
}

public class RightSide implements Half{
    private LeftSide leftSide;

    public void setLeftSide(LeftSide leftSide){
        this.leftSide = leftSide;
    }

    @Override
    public LeftSide getOtherHalf() {
        return leftSide;
    }
}

public class LeftSide implements Half{
    private RightSide rightSide;

    public void setRightSide(RightSide rightSide){
        this.rightSide = rightSide;
    }

    @Override
    public RightSide getOtherHalf(){
        return rightSide;   
    }
}

答案 1 :(得分:0)

这是一个解决方案。我已删除了对“左侧”的L和“右侧”的R的引用,并将其替换为T表示“此方”而O替换为“另一方” “

public class Solution1 {
    abstract static class Side<T extends Side<T, O>, O extends Side<O, T>> {
        Side<O, T> _mate = null;

        Side<O, T> getOtherSide() {
            return _mate;
        }

        void setOtherSide(Side<O, T> mate) {
            _mate = mate;
        }

        void connectToMate() {
            getOtherSide().setOtherSide(this);
        }
    }

    //You concrete implementations can replace This and Other
    //with Right and Left as you see fit.

    static class RightSideSub extends Side<RightSideSub, LeftSideSub> {
    }

    static class LeftSideSub extends Side<LeftSideSub, RightSideSub> {
    }
}

但是,我建议采用另一种更简单的解决方案,代价是放弃connectToMate()方法并将其替换为下面main()方法中的代码:

class OtherSolution{
    abstract static class Side<O>{
        O _mate = null;
        void setOtherSide(O mate){
            _mate = mate;
        }
    }

    static class RightSideSub extends Side<LeftSideSub> {
    }

    static class LeftSideSub extends Side<RightSideSub> {
    }

    public static void main(String[] args) {
        RightSideSub rs = new RightSideSub();
        LeftSideSub ls = new LeftSideSub();
        //do the connectToMate() operation externally like this:
        rs.setOtherSide(ls);
        ls.setOtherSide(rs);
    }
}