Fruit父类
public class Fruit {
public <T> Fruit(T t) {//父类没有默认构造器,只有有参构造器
}
}
Apple子类
public class Apple extends Fruit {
//子类要通过super传递有参构造器
public <T> Apple(T t) {
super(t);
}
}
不然会提示下面的错误
image.png
泛型类的使用
public class GenericClass<T> {
public static void main(String[] args) {
//交换String数组里两元素的位置
String[] strArr = {"我爷爷","今年","89岁了"};
//交换String数组里两元素的位置
Integer[] intArr = new Integer[]{2, 5, 8, 9, 6, 7};
changeArrPos(strArr,0,2);
changeArrPos(intArr,0,4);
List<String> stringList = new ArrayList<>();
stringList.add("我爸爸是");
stringList.add("java工程师");
stringList.add("我妈妈是");
stringList.add("病毒科学家");
GenericClass gc = new GenericClass();
gc.printList(stringList);
List<Integer> intList = new ArrayList<>();
intList.add(5);
intList.add(7);
intList.add(0);
intList.add(3);
gc.printList(intList);
}
/**
* 用泛型方法交换数组里两元素的位置
* @param arr 数组
* @param index1 元素的下标
* @param index2 元素的下标
*/
/*静态方法不能使用类定义的泛型,而应该单独定义泛型。
静态方法用类调用,类调用静态方法时还没有类后面的泛型,
所以静态方法中的泛型,必须要方法自己去定义*/
public static <F> void changeArrPos(F[] arr , int index1, int index2){
F temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
System.out.println(Arrays.toString(arr));
}
/**
* 用泛型方法做的list遍历
* @param lists 要遍历的列表
*/
public void printList(List<T> lists){
T t = lists.get(0);
for (T list : lists) {
System.out.println(list);
}
}
}
输出结果:
image.png
通配符的使用
<?>
通配符<?> 相当于<? extent Object>
/**
* 用泛型方法做的list遍历(使用通配符<?>)
* @param lists 要遍历的列表
* @param <?> 传入的类型
*/
//通配符<?> 相当于<? extent Object>
public void printList2(List<?> lists){
Object o = lists.get(0);
for (Object object : lists) {
System.out.println(object);
}
}
<? extends XXX> 和<? super XXX>
大家可以建三个类去进行下买水果方法
public class Fruit {
}
public class Apple extends Fruit {
}
public class RedApple extends Apple{
}
public static void buyFruit(List<? super RedApple> list){
}
RedApple redApple = new RedApple();
List<RedApple> list = new ArrayList<>();
list.add(redApple);
buyFruit(list);
通过改变类型,来习得这里面的奥妙之处
类型擦除
/**
* 类型擦除
*/
public static void typeErasure(){
List<String> stringList = new ArrayList<>();
List<Integer> integerList = new ArrayList<>();
//getClass 获取一下运行时Object的类
System.out.println(stringList.getClass());
System.out.println(integerList.getClass());
}
运行结果:
class java.util.ArrayList
class java.util.ArrayList
获取反射的类
//获取反射的类
public static void printReflectionClass() throws Exception {
Fruit fruit = new Fruit();
Class<?> class1 = fruit.getClass();
System.out.println(class1);
Class<?> class2 = Fruit.class;
System.out.println(class2);
Class<?> class3 = Class.forName("com.golearning.learning.bean.Fruit");
System.out.println(class3);
}
运行结果:
class com.golearning.learning.bean.Fruit
class com.golearning.learning.bean.Fruit
class com.golearning.learning.bean.Fruit
获取一下传入的类的属性和类型
/**
* 获取一下传入的类的属性和类型
* @param cla
*/
public static void getRefClassField(Class<?> cla){
Field[] fields = cla.getDeclaredFields();
for (Field field : fields) {
System.out.println(field.getName()+" = "+field.getType());
}
}
Class<?> cla = Class.forName("com.golearning.learning.bean.Fruit");
getRefClassField(cla);
运行结果为:
size = int
color = class java.lang.String
shape = class java.lang.String
获取一下传入的类里面的方法
/**
* 获取一下传入的类里面的方法
* @param cla
*/
public static void getRefClassMethod(Class<?> cla) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Method[] methods = cla.getDeclaredMethods();
for (Method method : methods) {
//获取方法的名字和有参参数个数数
System.out.println(method.getName() + " == 参数个数 == "+method.getParameterCount());
//获取方法的参数类型
Class[] classes = method.getParameterTypes();
for (Class aClass : classes) {
System.out.println(aClass);
}
}
//执行反射获取到的方法
Method method1 = cla.getDeclaredMethod("setColor",String.class);
Object object = cla.newInstance();
method1.invoke(object,"红色");
System.out.println(object);
}
给一个泛型List 插入一条非泛型的数据
/**
* 给一个泛型List 插入一条非泛型的数据
* @param list
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static void addDataInGenericList(List<String> list) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//获取它的类
Class<?> cla = list.getClass();
//获取它的add方法 参数为任意对象
Method method = cla.getDeclaredMethod("add",Object.class);
method.invoke(list,514);
for (Object o : list) {
System.out.println(o);
}
}
List<String> list = new ArrayList<>();
list.add("爷爷今年有");
list.add("99了");
//给一个泛型List 插入一条非泛型的数据
addDataInGenericList(list);
运行结果:
爷爷今年有
99了
514
获取传入类的构造器创建对象
public class Fruit {
//大小
private Integer size;
//颜色
private String color;
//形状
private String shape;
public Fruit(Integer size, String color, String shape) {
System.out.println("=====================获取有参构造器===================");
this.size = size;
this.color = color;
this.shape = shape;
}
public Fruit() {
System.out.println("=====================获取无参构造器===================");
}
public int getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
@Override
public String toString() {
return "Fruit{" +
"size=" + size +
", color='" + color + '\'' +
", shape='" + shape + '\'' +
'}';
}
}
/**
* 获取传入类的构造器创建对象
* @param cla
*/
public static void getRefClassConstractor(Class<?> cla) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//获取无参构造器
Constructor<?> constructor = cla.getConstructor(null);
Object object = constructor.newInstance(null);
System.out.println(object);
//获取有参构造器
Constructor<?> constructor2 = cla.getConstructor(Integer.class,String.class,String.class);
Object object2 = constructor2.newInstance(40,"红色","圆形");
System.out.println(object2);
}
Class<?> cla = Class.forName("com.golearning.learning.bean.Fruit");
getRefClassConstractor(cla);
运行结果为:
=====================获取无参构造器===================
Fruit{size=null, color='null', shape='null'}
=====================获取有参构造器===================
Fruit{size=40, color='红色', shape='圆形'}
网友评论