JAVA开发-泛型实例
2021-02-14 05:19
标签:核心 pac tac war 用例 架构 object one this 在贴录代码之前先说明一下相关类的关系 这是基础的关系,下附相关代码 /** */ private static String key ; private static String value; static { public int num ; private static String name; public void getKey() { } public void getValue() { } public void getName() { } } package generic.bean; public class TypeClass1 { private static String key ; } package generic.bean; /** */ private String typeClassA ; public void getTypeClassA(){ } } package generic.bean; /** */ private String typeClassB ; public void getTypeClassB(){ } 下边首先是 接口泛型, /** @param public T getResult() throws InstantiationException, IllegalAccessException; } 对接口泛型的一种实现,在实现中指定具体的类,ublic class GenericTIImpl implements IGenericT package generic.t.interfaces; import generic.bean.TypeClass; /** */@SuppressWarnings(" rawtypes rel="nofollow">br/>@SuppressWarnings("rawtypes") @Override } public static void main(String[] args) throws InstantiationException, IllegalAccessException { } } 对接口泛型的第二种实现,不指定具体的类 import generic.bean.TypeClass1; /** */@SuppressWarnings(" rawtypes rel="nofollow">br/>@SuppressWarnings("rawtypes") private Class clazz; @Override public static void main(String[] args) throws InstantiationException, IllegalAccessException { } } 泛型方法与泛型类的实现,该类就是泛型类的实现,其中getT方法根据入参不同,进行了重载,是需要重点关注的部分 package generic.t.classes; import generic.bean.TypeClass; /** */ private T t; public GenericClass(T t){ /* /* public static void main(String[] args) { } } 在泛型实现的潜在规则中。 package generic.kve; import java.util.ArrayList; import generic.bean.TypeClass; /** */ public Map public List public GenericKVE(Map public List public static void main(String[] args) { } } JAVA开发-泛型实例 标签:核心 pac tac war 用例 架构 object one this 原文地址:https://blog.51cto.com/4890631/2488309
TypeClass类与TypeClass1是并行的两个演示bean,TypeClassA extends TypeClass, TypeClassB extends TypeClassA
package generic.bean;
public class TypeClass {
key = "key-TypeClass";
value = "value-TypeClass";
name = "name-TypeClass";
}System.out.println("key:"+key);
System.out.println("value:"+value);
System.out.println("name:"+name);
private static String value;
static {
key = "key-TypeClass1";
value = "value-TypeClass1";
name = "name-TypeClass1";
}
public int num ;
private static String name;
public void getKey() {
System.out.println("key:"+key);
}
public void getValue() {
System.out.println("value:"+value);
}
public void getName() {
System.out.println("name:"+name);
}
public class TypeClassA extends TypeClass {System.out.println("当前对象类型:"+this.getClass().getSimpleName());
public class TypeClassB extends TypeClassA {System.out.println("当前对象类型:"+this.getClass().getSimpleName());
}
package generic.t.interfaces;
*/
public interface IGenericT
public class GenericTIImpl implements IGenericT
public TypeClass getResult() {return new TypeClass();
IGenericT t = new GenericTIImpl();
TypeClass type = (TypeClass)t.getResult();
type.getKey();
type.getValue();
type.getName();
package generic.t.interfaces;
public class GenericT1IImpl
public GenericT1IImpl(Class clazz){
this.clazz = clazz;
}
public T getResult() throws InstantiationException, IllegalAccessException {
T t = (T) clazz.newInstance();
return t;
}IGenericT t = new GenericTIImpl();
TypeClass1 type = (TypeClass1)t.getResult();
type.getKey();
type.getValue();
type.getName();
import generic.bean.TypeClass1;
public class GenericClass
this.t = t;
}
*/
public T getT(){
return this.t;
}
*/
public
T tt = null;
try {
tt = tClass.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tt;
}//泛型TypeClass的实现
GenericClass
在下附的代码中,对 E,K,V 同时进行了实现
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import generic.bean.TypeClass1;
public class GenericKVE
this.map = map;
E e = (E) this.map;
this.list = new ArrayList
this.list.add(e);
}
return this.list;
}Map
下附执行结果