本文介绍了如何解决由Java泛型中的交集类型引起的模糊方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现你可以在一个单一的类型参数绑定中指定多个类型(参见示例)。像任何新工具一样,我一直在探索如何使用(和滥用)的可能性。我制作了这个例子来帮助说明。



在下面的示例中,编译器给我一个错误

我可以理解这一点,因为任何方法签名都匹配。我的问题是,如何在不改变方法的情况下解决这个问题?如果我想要强制调用Soup版本,我可以降低到Soup:
$ b

但我不确定如何强制呼叫其他版本。是否有可能?

  public class Demo {

接口HasA {public char getA(); }
接口HasB {public char getB(); }
接口HasC {public char getC(); }

接口汤{
public void eat();
}

类实现HasA,HasB,HasC {
public char getA(){return'a'; }
public char getB(){return'b'; }
public char getC(){return'c'; }
}

类AlphabetSoup实现了Soup,HasA,HasB,HasC {
public void eat(){System.out.println(Mmm Mmm Good!); }
public char getA(){return'a'; }
public char getB(){return'b'; }
public char getC(){return'c'; }
}

public void dispatch(汤汤){
System.out.println(吃点汤);
soup.eat();
}

public< T扩展了HasA& HasB& HASC> void dispatch(T letters){
System.out.println(Reciting ABCs ...);
System.out.println(letters.getA());
System.out.println(letters.getB());
System.out.println(letters.getC());
}

public void test(){
dispatch(new Alphabet());
dispatch(new AlphabetSoup());



public static void main(String [] args){
new Demo()。test();


$ / code $ / pre


编辑: 多重有界类型参数被形式化地称为相交类型。 解决方案

请注意,错误与泛型无关,如果您使用接口并且类型为十字路口,则获得相同的结果:

  public class AA {

interface XX {};
interface YY {};

public void doSomething(XX x){}
public void doSomething(YY x){}

class XY implements XX,YY {

}

public void runner(){
doSomething(new XY());
}

$ / code>

在doSomething中出现同样的错误,编译器无法解决模糊问题。如果你有一个层次结构,比如YY extends XX和XY implements YY,编译器可以推断出正确的方法打电话。

I just recently discovered that you can specify multiple types in a single type parameter bound (see example). Like any new tool, I've been trying to explore the possibilities of how this can be used (and misused). I crafted this example to help illustrate.

On the sample below, the compiler is giving me an error

I can understand this because either method signature matches. My question is how could this be resolved without changing the methods? If I wanted force a call to the Soup version I could downcast to Soup:

But I'm unsure how you'd force a call to the other version. Is it possible?

public class Demo {

    interface HasA { public char getA(); }
    interface HasB { public char getB(); }
    interface HasC { public char getC(); }

    interface Soup { 
        public void eat();
    }

    class Alphabet implements HasA, HasB, HasC {
        public char getA() { return 'a'; }
        public char getB() { return 'b'; }
        public char getC() { return 'c'; }
    }

    class AlphabetSoup implements Soup,  HasA, HasB, HasC  { 
        public void eat() { System.out.println("Mmm Mmm Good!"); }
        public char getA() { return 'a'; }
        public char getB() { return 'b'; }
        public char getC() { return 'c'; }
    }

    public void dispatch(Soup soup) {
        System.out.println("Eating some soup...");
        soup.eat();
    }

    public <T extends HasA & HasB & HasC> void dispatch(T letters) {
        System.out.println("Reciting ABCs...");
        System.out.println(letters.getA());
        System.out.println(letters.getB());
        System.out.println(letters.getC());
    }

    public void test() {
        dispatch(new Alphabet());
        dispatch(new AlphabetSoup());
    }


    public static void main(String[] args) {
        new Demo().test();
    }
}

--Edit: Just learned that "multiple bounded type parameters are formally referred to as "Intersection Types"

解决方案

Note that the error is not related to generics, you get the same result if you use interfaces and a type is the intersection:

public class AA {

    interface XX{};
    interface YY{};

    public void doSomething(XX x){}
    public void doSomething(YY x){}

    class XY implements XX,YY{

    }

    public void runner(){
        doSomething(new XY());
    }
}

You get the same error in "doSomething", the compiler cannot resolve the ambiguity. Do you want to interpret as XX or as YY? You have to specify it with a cast.But if you have a hierarchy, like "YY extends XX" and "XY implements YY", the compiler can infer the correct method to call.

这篇关于如何解决由Java泛型中的交集类型引起的模糊方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:45