demo01
Demo01
package demo01;
import java.util.ArrayList;
import java.util.List;
//泛型的使用
public class Demo01 {
public static void main(String[] args) {
//為了解決通用性的問(wèn)題涕滋,jdk提供了封裝好的集合,但是集合可以存放不同類型的數(shù)據(jù)
List list = new ArrayList();
list.add("111");
list.add(111);
list.add(true);
list.add(11.1);
//加入泛型腌逢,做約束
ArrayList<String> alist = new ArrayList<>();
alist.add("111");
//alist.add("111");加入泛型看峻,不可儲(chǔ)存泛型之外的數(shù)據(jù)類型
}
}
demo02
Demo02
package demo02;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 15:23
*/
public class Demo02 {
public static void main(String[] args) {
Pair<Integer> pair1 = new Pair<>(1, 2);
System.out.println(pair1);
Pair<Double> pair2 = new Pair<>(1.1, 2.2);
System.out.println(pair2);
Pair<String> pair3 = new Pair<>("haha", "hehe");
System.out.println(pair3);
}
}
Pair
package demo02;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 15:20
*/
public class Pair<T> {
private T first ;
private T second ;
//構(gòu)造方法
public Pair(){
}
public Pair(T first, T second){
this.first = first;
this.second = second ;
}
//setter & getter
public T getFirst() {
return first;
}
public T getSecond() {
return second;
}
public void setFirst(T first) {
this.first = first;
}
public void setSecond(T second) {
this.second = second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
}
demo03
Demo03
package demo03;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 15:33
*/
public class Demo03 {
public static void main(String[] args) {
Integer[] a = {1,2,3,4,5,6};
System.out.println(AeeayAlg.getMiddle(a));
Integer[] b = {1,2,3,4,5};
System.out.println(AeeayAlg.getMiddle(b));
String[] str = {"111","222","333","444"};
System.out.println(AeeayAlg.getMiddle(str));
String[] str1 = {"111","222","333","444"};
}
}
AeeayAlg
package demo03;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 15:34
*/
public class AeeayAlg {
//數(shù)組相關(guān)算法隘弊,求出通用的數(shù)據(jù)中間位置的值
public static <T> T getMiddle(T[] a){
return a[a.length/2];
}
}
demo04
Demo04
package demo04;
import java.awt.*;
import java.util.ArrayList;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 16:06
*/
public class Demo04 {
public static void main(String[] args) {
Canvas canvas = new Canvas();
Circle circle =new Circle();
canvas.draw(circle);
//定義多個(gè)形狀落恼,組成一個(gè)數(shù)組列表撒顿,傳入
Circle circle1 =new Circle();
Rectabgle rectabgle = new Rectabgle();
Rectabgle rectabgle1 = new Rectabgle();
// Rectabgle rectangle =new Rectangle();
// Rectabgle rectangle1 = new Rectangle();
Circle circle2 =new Circle();
//準(zhǔn)備一個(gè)集合
ArrayList<Shape>list = new ArrayList<>();
list.add(circle);
list.add(circle1);
list.add(circle2);
list.add(rectabgle);
list.add(rectabgle1);
canvas.drawAll(list);
}
}
Canvas
package demo04;
import java.util.ArrayList;
import java.util.List;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 16:03
*/
public class Canvas {
// 在畫布上畫的一個(gè)
public void draw(Shape shape){
shape.xingzhuang();
}
// 在畫布上畫多個(gè)
public void drawAll(List< ? extends Shape >shapeList){
for (Shape s : shapeList){
s.xingzhuang();
}
}
public void drawAll(ArrayList<Shape> list) {
}
}
Circle
package demo04;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 16:02
*/
public class Circle extends Shape{
@Override
public void xingzhuang() {
System.out.println("正在畫圓");
}
}
Rectabgle
package demo04;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 16:14
*/
public class Rectabgle extends Shape{
@Override
public void xingzhuang(){
System.out.println("正在畫矩形");
}
}
Shape
package demo04;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 16:00
*/
public abstract class Shape {
public abstract void xingzhuang();
}
demo05
Demo05
package demo05;
import java.util.ArrayList;
import java.util.List;
/**
* @qvthor liuwenzheng
* @date 2021/5/11 16:27
*/
public class Demo05 {
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
List tempList = list ; // 泛型的擦除
tempList.add(1213);
System.out.println(tempList);
}
}