gpt4 book ai didi

java - 在泛型中使用 super 和 extends 的有用示例?

转载 作者:搜寻专家 更新时间:2023-11-01 04:03:31 29 4
gpt4 key购买 nike

我知道有很多关于这个话题的问题,但不幸的是他们不能帮助我消除我的晦涩。首先,看下面的例子。我不明白,为什么下面的“添加”-method someCage.add(rat1)不工作并中止,出现以下异常:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(capture#2-of ? extends Animal) in the type Cage is not applicable for the arguments (Rat)

这与 Cage<Rat> 的原因相同吗?不是 Cage<Animal> ?如果是,我在这个例子中不理解它,所以我不确定编译器到底做了什么。这是代码示例:

package exe;

import cage.Cage;
import animals.Animal;
import animals.Ape;
import animals.Lion;
import animals.Rat;

public class Main {

public static void main(String[] args) {
Lion lion1 = new Lion(true, 4, "Lion King", 8);
Lion lion2 = new Lion(true, 4, "King of Animals", 9);
Ape ape1 = new Ape(true, 2, "Gimpanse", true);
Ape ape2 = new Ape(true, 2, "Orang Utan", true);
Rat rat1 = new Rat(true, 4, "RatBoy", true);
Rat rat2 = new Rat(true, 4, "RatGirl", true);
Rat rat3 = new Rat(true, 4, "RatChild", true);

Cage<Animal> animalCage = new Cage<Animal>();
animalCage.add(rat2);
animalCage.add(lion2);

Cage<Rat> ratCage = new Cage<Rat>();
ratCage.add(rat3);
ratCage.add(rat1);
ratCage.add(rat2);
// ratCage.add(lion1); //Not Possible. A Lion is no rat

Cage<Lion> lionCage = new Cage<Lion>();
lionCage.add(lion2);
lionCage.add(lion1);

Cage<? extends Animal> someCage = new Cage<Animal>(); //? = "unknown type that is a subtype of Animal, possibly Animal itself"
someCage = ratCage; //OK
// someCage = animalCage; //OK
someCage.add(rat1); //Not Possible, but why?

animalCage.showAnimals();
System.out.println("\nRatCage........");
ratCage.showAnimals();
System.out.println("\nLionCage........");
lionCage.showAnimals();
System.out.println("\nSomeCage........");
someCage.showAnimals();
}
}

这是笼类:

package cage;

import java.util.HashSet;
import java.util.Set;

import animals.Animal;

public class Cage<T extends Animal> { //A cage for some types of animals
private Set<T> cage = new HashSet<T>();

public void add(T animal) {
cage.add(animal);
}

public void showAnimals() {
for (T animal : cage) {
System.out.println(animal.getName());
}
}
}

此外,如果你能给我一个有意义的“ super ”例子,我会很高兴这个 animal-cage-code。直到现在我还没有明白如何使用它。有很多理论示例,我读到了 PECS 概念,但无论如何我还不能将它用于有意义的事情。在这个例子中有一个“消费者”(有 super)意味着什么?

最佳答案

超绑定(bind)示例

介绍transferTo()方法接受 Cage<? super T> - 拥有父类(super class) T 的笼子.因为 T 是其父类(super class)的实例,所以可以放置 TCage<? super T> .

public static class Cage<T extends Animal> { 
private Set<T> pen = new HashSet<T>();

public void add(T animal) {
pen.add(animal);
}

/* It's OK to put subclasses into a cage of super class */
public void transferTo(Cage<? super T> cage) {
cage.pen.addAll(this.pen);
}

public void showAnimals() {
System.out.println(pen);
}
}

现在让我们看看<? super T>在行动中:

public static class Animal {
public String toString() {
return getClass().getSimpleName();
}
}
public static class Rat extends Animal {}
public static class Lion extends Animal {}
public static class Cage<T extends Animal> { /* above */ }

public static void main(String[] args) {
Cage<Animal> animals = new Cage<Animal>();
Cage<Lion> lions = new Cage<Lion>();
animals.add(new Rat()); // OK to put a Rat into a Cage<Animal>
lions.add(new Lion());
lions.transferTo(animals); // invoke the super generic method
animals.showAnimals();
}

输出:

[Rat, Lion]



另一个重要的概念是,尽管确实如此:

Lion instanceof Animal // true

是真的

Cage<Lion> instanceof Cage<Animal> // false

如果不是这种情况,这段代码可以编译:

Cage<Animal> animals;
Cage<Lion> lions;
animals = lions; // This assignment is not allowed
animals.add(rat); // If this executed, we'd have a Rat in a Cage<Lion>

关于java - 在泛型中使用 super 和 extends 的有用示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6826916/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com