如何在Java中使用类的逆变性

huangapple go评论60阅读模式
英文:

How to use Contravariance in Java with Classes

问题

I am learning Java variance.
我正在学习Java的变异性。

I am getting the Covariant to work but not Contravariance where I can accept a super type.
协变性可以工作,但逆变性无法工作,无法接受超类型。

I have a simple class hierarchy for this:
我有一个简单的类层次结构:

Animal,
Cat,
Lion
动物,猫,狮子

Then I have two classes as Generic holders:
然后我有两个作为泛型持有者的类:

CovariantAnimalHolder
ContraVariantHolder
协变动物持有者,逆变持有者

The Covariant works fine but Contravariance isnt working.
协变性可以正常工作,但逆变性不起作用。

Here is the code:
以下是代码:

public class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public Animal() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Cat extends Animal {
    private boolean isWild;

    public Cat(boolean isWild) {
        this.isWild = isWild;
    }

    public Cat(){

    }

    public boolean isWild() {
        return isWild;
    }

    public void setWild(boolean wild) {
        isWild = wild;
    }
}

public class Lion extends Cat{

    private boolean isExtinct;

    public Lion (boolean isExtinct) {
        this.isExtinct = isExtinct;
    }

    public Lion () {

    }

    public boolean isExtinct() {
        return isExtinct;
    }

    public void setExtinct(boolean extinct) {
        isExtinct = extinct;
    }
}

public class CovariantAnimalHolder<T extends Cat> {
    private T animalHolder;

    public T getAnimalHolder() {
        return animalHolder;
    }

    public void setAnimalHolder(T animalHolder) {
        this.animalHolder = animalHolder;
    }
}

public class VarianceMain {

    public static void main(String[] args) {
        //This works
        CovariantAnimalHolder<Lion> lionCovariantAnimalHolder = new CovariantAnimalHolder<Lion>();

        //This works:
        CovariantAnimalHolder<Cat> catCovariantAnimalHolder = new CovariantAnimalHolder<>();

        //This should not work, Perfect!
        //CovariantAnimalHolder<Animal> animalCovariantAnimalHolder = new CovariantAnimalHolder<Animal>();
    }
}

But when I create the ContraVariant holder I am getting some basic errors which I can't understand:
但是当我创建逆变持有者时,我遇到了一些基本错误,我无法理解:

public class ContravariantAnimalHolder<? super Cat>{
    private T animalHolder;
}

Error:
错误:

英文:

I am learning Java variance.

I am getting the Covariant to work but not Contravariance where I can accept a super type.

I have a simple class hierarchy for this:

Animal,
Cat,
Lion

Then I have two classes as Generic holders:

CovariantAnimalHolder
ContraVariantHolder

The Covariant works fine but Contravariance isnt working.

Here is the code:

public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public Animal() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Cat extends Animal {
private boolean isWild;
public Cat(boolean isWild) {
this.isWild = isWild;
}
public Cat(){
}
public boolean isWild() {
return isWild;
}
public void setWild(boolean wild) {
isWild = wild;
}
}
public class Lion extends Cat{
private boolean isExtinct;
public Lion (boolean isExtinct) {
this.isExtinct = isExtinct;
}
public Lion () {
}
public boolean isExtinct() {
return isExtinct;
}
public void setExtinct(boolean extinct) {
isExtinct = extinct;
}
}

Animal Holders:

public class CovariantAnimalHolder&lt;T extends Cat&gt; {
private T animalHolder;
public T getAnimalHolder() {
return animalHolder;
}
public void setAnimalHolder(T animalHolder) {
this.animalHolder = animalHolder;
}
}

Main program:

So far great

public class VarianceMain {
public static void main(String[] args) {
//This works
CovariantAnimalHolder&lt;Lion&gt; lionCovariantAnimalHolder = new CovariantAnimalHolder&lt;Lion&gt;();
//This works:
CovariantAnimalHolder&lt;Cat&gt; catCovariantAnimalHolder = new CovariantAnimalHolder&lt;&gt;();
//This should not work, Perfect!
//CovariantAnimalHolder&lt;Animal&gt; animalCovariantAnimalHolder = new CovariantAnimalHolder&lt;Animal&gt;();
}
}

But when I create the ContraVariant holder I am getting some basic errors which I can't understand:

public class ContravariantAnimalHolder&lt;T super Cat&gt;{
private T animalHolder;
}

Error:

如何在Java中使用类的逆变性

答案1

得分: 0

在Java中,无法像在其他语言中那样将泛型类型定义为协变或逆变。例如,在Scala中可以这样做。

在Java中,您定义的是具有上界的泛型类型,这会限制您在处理类型时只能使用 extends,而不能在泛型类型中使用 super,这也是您不能在 ContravariantAnimalHolder 类中使用它的原因。

不过,您可以定义具有下界通配符的方法,这根据您的需求可能适用于逆变。

英文:

In Java, you cannot define a generic type as a covariant or contravariant, as you can in other languages, for example, in scala

Instead, in java, what you define are generic types with Upper bounds which limit you to the use of extends when it comes to types, and you cannot use super with generic types, reason you cannot use it in your ContravariantAnimalHolder class.

You can, however define methods with lower bounded wildcards, which, depending on what you need may fit your needs for contravariant

huangapple
  • 本文由 发表于 2023年3月3日 18:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定