Generic + advanced use wildcards - limited generics

The concept of generic 01_

 

 =========================================================================================================================

02_ using generics benefits

Demo01Generic.java

package com.itheima.demo03.Generic;

import java.util.ArrayList;
import java.util.Iterator;

public class Demo01Generic {
public static void main(String[] args) {
show02();
}

/ *
Create a collection of objects, use the generic
benefits:
1. Avoid trouble type conversion, storage of what type, is taken out of what type
2. The runtime exception (after the code runs throws exception), to enhance the compile (write the code will complain)
drawbacks:
generic what type, what type of data can only be stored
* /
Private static void show02 () {
ArrayList <String> List = new new ArrayList <> ();
list.add ( "abc");
//list.add(1); //add(java.lang.String)in not ArrayList CAN BE Applied to (int)

// set list iterators to traverse
the Iterator <String> IT = list.iterator ();
the while (it.hasNext ()) {
String it.next S = ();
System.out.println (S + "->" + s.length ());
}
}

/ *
Create a collection of objects, do not use the generic
benefits:
a collection without the use of generics, the default type is Object types that can store any type of data
drawbacks:
unsafe, throws an exception
* /
Private static void show01 () {
ArrayList List the ArrayList new new = ();
List.add ( "ABC");
List.add (. 1);

// Use iterates over the set of list
// Get iterator
the Iterator list.iterator IT = ();
// Use the iterator hasNext next through the collection and
the while (it.hasNext ()) {
// Object element is also removed type
Object obj = it.next ();
System.out.println (obj);

// String class want to use specific methods, length acquired length of the string; not polymorphic obj = Object "ABC";
// needs to downcast
// throws an exception ClassCastException type conversion, can not convert type Integer type String
String S = (String) obj;
System.out.println (s.length ());
}
}
}

=============================================================================================================

03_定义和使用含有泛型的类

 

 GenericClass.java

package com.itheima.demo03.Generic;
/*
定义一个含有泛型的类,模拟ArrayList集合
泛型是一个未知的数据类型,当我们不确定什么数据类型的时候,可以使用泛型
泛型可以接收任意的数据类型,可以使用Integer,String,Student...
创建对象的时候确定泛型的数据类型
*/
public class GenericClass<E> {
private E name;

public E getName() {
return name;
}

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

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Demo02GenericClass.java

package com.itheima.demo03.Generic;

public class Demo02GenericClass {
public static void main(String[] args) {
//不写泛型默认为Object类型
GenericClass gc = new GenericClass();
gc.setName("只能是字符串");
Object obj = gc.getName();

//创建GenericClass对象,泛型使用Integer类型
GenericClass<Integer> gc2 = new GenericClass<>();
gc2.setName(1);

Integer name = gc2.getName();
System.out.println(name);

//创建GenericClass对象,泛型使用String类型
GenericClass<String> gc3 = new GenericClass<>();
gc3.setName("小明");
String name1 = gc3.getName();
System.out.println(name1);
}
}

====================================================================================================

04_定义和使用含有泛型的方法

 

 GenericMethod.java

package com.itheima.demo03.Generic;
/*
定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间

格式:
修饰符 <泛型> 返回值类型 方法名(参数列表(使用泛型)){
方法体;
}

含有泛型的方法,在调用方法的时候确定泛型的数据类型
传递什么类型的参数,泛型就是什么类型
*/
public class GenericMethod {
//定义一个含有泛型的方法    <任意字符>
public <M> void method01(M m){
System.out.println(m);
}

//定义一个含有泛型的静态方法
public static <S> void method02(S s){
System.out.println(s);
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------

Demo03GenericMethod.java

package com.itheima.demo03.Generic;
/*
测试含有泛型的方法
*/
public class Demo03GenericMethod {
public static void main(String[] args) {
//创建GenericMethod对象
GenericMethod gm = new GenericMethod();

/*
调用含有泛型的方法method01
传递什么类型,泛型就是什么类型
*/
gm.method01(10);
gm.method01("abc");
gm.method01(8.8);
gm.method01(true);

gm.method02("静态方法,不建议创建对象使用");

//静态方法,通过类名.方法名(参数)可以直接使用
GenericMethod.method02("静态方法");
GenericMethod.method02(1);
}
}

=========================================================================================

05_定义和使用含有泛型的接口

 

 GenericInterface.java

package com.itheima.demo03.Generic;
/*
定义含有泛型的接口
*/
public interface GenericInterface<I> {
public abstract void method(I i);
}

----------------------------------------------------------------------------------------------

GenericInterfaceImpl1.java

package com.itheima.demo03.Generic;
/*
含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型
public interface Iterator<E> {
E next();
}
Scanner类实现了Iterator接口,并指定接口的泛型为String,所以重写的next方法泛型默认就是String
public final class Scanner implements Iterator<String>{
public String next() {}
}
*/
public class GenericInterfaceImpl1 implements GenericInterface<String>{
@Override
public void method(String s) {
System.out.println(s);
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

GenericInterfaceImpl2(1).java

package com.itheima.demo03.Generic;

/*
含有泛型的接口第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List<E>{
boolean add(E e);
E get(int index);
}
public class ArrayList<E> implements List<E>{
public boolean add(E e) {}
public E get(int index) {}
}
*/
public class GenericInterfaceImpl2<I> implements GenericInterface<I> {
@Override
public void method(I i) {
System.out.println(i);
}
}

===================================================================================================

06_泛型通配符

 

 

 

 

 

 

 

 

 Demo05Generic.java

package com.itheima.demo03.Generic;

import java.util.ArrayList;
import java.util.Iterator;

/*
泛型的通配符:
?:代表任意的数据类型
使用方式:
不能创建对象使用
只能作为方法的参数使用
*/
public class Demo05Generic {
public static void main(String[] args) {
ArrayList<Integer> list01 = new ArrayList<>();
list01.add(1);
list01.add(2);

ArrayList<String> list02 = new ArrayList<>();
list02.add("a");
list02.add("b");

printArray(list01);
printArray(list02);

//ArrayList<?> list03 = new ArrayList<?>();
}

/*
定义一个方法,能遍历所有类型的ArrayList集合
这时候我们不知道ArrayList集合使用什么数据类型,可以泛型的通配符?来接收数据类型
注意:
泛型没有继承概念的
*/
public static void printArray(ArrayList<?> list){
//使用迭代器遍历集合
Iterator<?> it = list.iterator();
while(it.hasNext()){
//it.next()方法,取出的元素是Object,可以接收任意的数据类型
Object o = it.next();
System.out.println(o);
}
}
}

 =============================================================================================================

 

 Demo06Generic.java

package com.itheima.demo03.Generic;

import java.util.ArrayList;
import java.util.Collection;

/*
泛型的上限限定: ? extends E   代表使用的泛型只能是E类型的子类/本身
泛型的下限限定: ? super E      代表使用的泛型只能是E类型的父类/本身
*/
public class Demo06Generic {
public static void main(String[] args) {
Collection<Integer> list1 = new ArrayList<Integer>();
Collection<String> list2 = new ArrayList<String>();
Collection<Number> list3 = new ArrayList<Number>();
Collection<Object> list4 = new ArrayList<Object>();

getElement1(list1);
//getElement1(list2);//报错
getElement1(list3);
//getElement1(list4);//报错

//getElement2(list1);//报错
//getElement2(list2);//报错
getElement2(list3);
getElement2(list4);

/*
类与类之间的继承关系
Integer extends Number extends Object
String extends Object
*/

}
// 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
public static void getElement1(Collection<? extends Number> coll){}
// 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
public static void getElement2(Collection<? super Number> coll){}
}

Guess you like

Origin www.cnblogs.com/curedfisher/p/12424631.html