-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
250 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/HsqDog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package cn.bravedawn.generic.wildcards; | ||
|
||
public class HsqDog extends Dog{ | ||
|
||
// 哈士奇 | ||
} |
24 changes: 24 additions & 0 deletions
24
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/Pair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cn.bravedawn.generic.wildcards; | ||
|
||
public class Pair<T> { // public class Pair<T extends java.lang.Object> extends java.lang.Object | ||
|
||
/** | ||
* Java的类型擦除,可以使用 javap -v xx.class去查看 | ||
*/ | ||
|
||
private T first; // private Object first; | ||
private T last; // private Object last; | ||
|
||
public Pair(T first, T last) { // public Pair(Object first, Object last) | ||
this.first = first; | ||
this.last = last; | ||
} | ||
|
||
public T getFirst() { // public Object getFirst() | ||
return first; | ||
} | ||
|
||
public T getLast() { // public Object getLast() | ||
return last; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/SmyDog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package cn.bravedawn.generic.wildcards; | ||
|
||
public class SmyDog extends Dog{ | ||
|
||
// 萨摩耶 | ||
} |
33 changes: 33 additions & 0 deletions
33
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/lowerbounds/LowerBoundsExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cn.bravedawn.generic.wildcards.lowerbounds; | ||
|
||
import cn.bravedawn.generic.wildcards.Dog; | ||
import cn.bravedawn.generic.wildcards.HsqDog; | ||
import cn.bravedawn.generic.wildcards.SmyDog; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LowerBoundsExample { | ||
|
||
/** | ||
* 我们现在有一个场景,将src列表的元素复制到dest列表中 | ||
* @param dest 目标列表 | ||
* @param src 源列表 | ||
*/ | ||
public static void copyHappyDog(List<Dog> dest, List<Dog> src) { | ||
for (Dog dog : src) { | ||
dest.add(dog); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 如果将Dog子类的集合传入到src中就会编译失败 | ||
*/ | ||
public static void main(String[] args) { | ||
|
||
copyHappyDog(new ArrayList<Dog>(), new ArrayList<Dog>()); | ||
//copyHappyDog(new ArrayList<Dog>(), new ArrayList<HsqDog>()); // 编译失败 | ||
//copyHappyDog(new ArrayList<Dog>(), new ArrayList<SmyDog>()); // 编译失败 | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...vaTrain/src/main/java/cn/bravedawn/generic/wildcards/lowerbounds/LowerBoundsExample2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cn.bravedawn.generic.wildcards.lowerbounds; | ||
|
||
import cn.bravedawn.generic.wildcards.Animal; | ||
import cn.bravedawn.generic.wildcards.Dog; | ||
import cn.bravedawn.generic.wildcards.HsqDog; | ||
import cn.bravedawn.generic.wildcards.SmyDog; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LowerBoundsExample2 { | ||
|
||
/** | ||
* 我们现在有一个场景,将src列表的元素复制到dest列表中 | ||
* @param dest 目标列表 | ||
* @param src 源列表 | ||
*/ | ||
public static void copyHappyDog(List<Dog> dest, List<? extends Dog> src) { | ||
for (Dog dog : src) { | ||
dest.add(dog); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* copyHappyDog()方法的src参数使用上边界符进行限定,Dog子类的列表都可以接受 | ||
* 但是如果参数dest传递Dog父类的集合就会编译失败 | ||
*/ | ||
public static void main(String[] args) { | ||
|
||
copyHappyDog(new ArrayList<Dog>(), new ArrayList<Dog>()); | ||
copyHappyDog(new ArrayList<Dog>(), new ArrayList<HsqDog>()); | ||
copyHappyDog(new ArrayList<Dog>(), new ArrayList<SmyDog>()); | ||
//copyHappyDog(new ArrayList<Animal>(), new ArrayList<SmyDog>()); // 编译失败 | ||
//copyHappyDog(new ArrayList<Object>(), new ArrayList<SmyDog>()); // 编译失败 | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...vaTrain/src/main/java/cn/bravedawn/generic/wildcards/lowerbounds/LowerBoundsExample3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.bravedawn.generic.wildcards.lowerbounds; | ||
|
||
import cn.bravedawn.generic.wildcards.Animal; | ||
import cn.bravedawn.generic.wildcards.Dog; | ||
import cn.bravedawn.generic.wildcards.HsqDog; | ||
import cn.bravedawn.generic.wildcards.SmyDog; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LowerBoundsExample3 { | ||
|
||
/** | ||
* 我们现在有一个场景,将src列表的元素复制到dest列表中 | ||
* @param dest 目标列表 | ||
* @param src 源列表 | ||
*/ | ||
public static void copyHappyDog(List<? super Dog> dest, List<? extends Dog> src) { | ||
for (Dog dog : src) { | ||
dest.add(dog); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* copyHappyDog()方法的dest参数使用 下边界符 进行限定,Dog父类的列表都可以接受 | ||
*/ | ||
public static void main(String[] args) { | ||
|
||
copyHappyDog(new ArrayList<Dog>(), new ArrayList<Dog>()); | ||
copyHappyDog(new ArrayList<Dog>(), new ArrayList<HsqDog>()); | ||
copyHappyDog(new ArrayList<Dog>(), new ArrayList<SmyDog>()); | ||
copyHappyDog(new ArrayList<Animal>(), new ArrayList<SmyDog>()); | ||
copyHappyDog(new ArrayList<Object>(), new ArrayList<SmyDog>()); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...vaTrain/src/main/java/cn/bravedawn/generic/wildcards/lowerbounds/LowerBoundsExample4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cn.bravedawn.generic.wildcards.lowerbounds; | ||
|
||
import cn.bravedawn.generic.wildcards.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LowerBoundsExample4 { | ||
|
||
/** | ||
* 1.泛型的下界描述符是支持添加元素的,因为他没有类型转换失败的风险 | ||
* 2.泛型的下界描述符修饰的集合只能添加指定类型或是指定类型的子类,添加其他类型是会编译失败的,切记不要将接收泛型类和添加泛型类弄混了 | ||
* 3.大家就会发现,协变和逆变正好相反,上界发生协变只读不写,下界发生逆变只写不读。所以两者的使用场景也就出来了,正如copyHappyDog()方法 | ||
* 1)如果我对泛型只读不写时,使用协变,下界描述符。 | ||
* 2)如果我对泛型只写不读时,使用逆变,上界描述符。 | ||
* 3)如果我又想写又想读,那你不需要使用逆变和协变,你需要使用准确的泛型类型。 | ||
* 4)泛型的协变和逆变一般只使用在方法上,声明形参来接收调用者传递的实参,之前的代码中使用协变和逆变修饰变量和类是为了方便演示。 | ||
*/ | ||
|
||
|
||
/** | ||
* 我们现在有一个场景,将src列表的元素复制到dest列表中 | ||
* @param dest 目标列表 | ||
* @param src 源列表 | ||
*/ | ||
public static void copyHappyDog(List<? super Dog> dest, List<? extends Dog> src) { | ||
for (Dog dog : src) { | ||
dest.add(dog); | ||
} | ||
} | ||
|
||
public static void func(List<? super Dog> list) { | ||
list.add(new Dog()); | ||
list.add(new HsqDog()); | ||
list.add(new SmyDog()); | ||
|
||
//list.add(new Animal()); // 添加其他元素就会编译错误 | ||
//list.add(new Cat()); // 编译错误 | ||
|
||
// 使用下界描述符声明的对象,只能使用Object类型获取对象 | ||
Object object = list.get(0); | ||
|
||
//Animal animal = list.get(0); // 编译错误 | ||
//Dog dog = list.get(0); // 编译错误 | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
func(new ArrayList<Dog>()); | ||
func(new ArrayList<Animal>()); | ||
func(new ArrayList<Object>()); | ||
|
||
//func(new ArrayList<SmyDog>()); // 传入子类的泛型类就会编译错误 | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/lowerbounds/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* @author : depers | ||
* @program : JavaTrain | ||
* @description: | ||
* @date : Created in 2023/8/22 19:51 | ||
*/ | ||
package cn.bravedawn.generic.wildcards.lowerbounds; | ||
|
||
|
||
/** | ||
* 泛型下界描述符的相关代码 | ||
*/ |
3 changes: 2 additions & 1 deletion
3
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package cn.bravedawn.generic.wildcards; | ||
|
||
/** | ||
* | ||
* 这个包主要介绍了泛型的协变和逆变 | ||
* 还有就是PECS原则,关于PECS原则,大家可以参考java.util.Collections#copy(java.util.List, java.util.List)方法 | ||
*/ |
32 changes: 29 additions & 3 deletions
32
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/upperbounds/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,38 @@ | ||
package cn.bravedawn.generic.wildcards.upperbounds; | ||
|
||
import cn.bravedawn.generic.wildcards.Animal; | ||
import cn.bravedawn.generic.wildcards.Cat; | ||
import cn.bravedawn.generic.wildcards.Dog; | ||
|
||
public class Node <T extends Animal> { | ||
public class Node <T extends Animal> { // public class Node<T extends Animal> | ||
|
||
|
||
/** | ||
* extends关键字的用途 | ||
* 1.如果extends关键字用于类上,则他可以对成员变量的泛型类型进行校验 | ||
* 2.成员变量可以访问父类的方法 | ||
*/ | ||
|
||
|
||
private T previous; | ||
private T next; | ||
private final T animal; // private final Animal animal; | ||
|
||
public static void main(String[] args) { | ||
Node<Animal> animalNode; | ||
Node<Dog> dogNode; | ||
Node<Cat> catNode; | ||
|
||
// 可以对成员变量的泛型类型进行校验 | ||
// Node<String> stringNode; // 编译错误 | ||
} | ||
|
||
|
||
public Node(T animal) { // public void Node(Animal animal) | ||
this.animal = animal; | ||
} | ||
|
||
// 成员变量可以访问泛型父类的方法。 | ||
public void process() { | ||
animal.eat(); | ||
animal.sleep(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
2.JavaTrain/src/main/java/cn/bravedawn/generic/wildcards/upperbounds/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package cn.bravedawn.generic.wildcards.upperbounds; | ||
|
||
/** | ||
* 泛型的上界描述符 | ||
* | ||
* 泛型的上界描述符的相关知识点 | ||
*/ |