JAVA基础之泛型程序设计《一》
1.定义一个简单的泛型类
? 设计一个简单的多类型数组类,可以简单的支持多种类型的数组(String 和Integer)还有些其他漏洞,这里只是简单的写一个泛型类,做个示范
? ?? java ODE; 库中经常用 E 表示集合的元素类型 K,V分别表示 表的关键字和值的类型。 T表示任意类型有时候用 U和S。
package priv.zsl.fxxx;
import java.util.Arrays;
public class ArrayTest{
public static void main(String[] args) {
Array<String> stringArray = new Array<String>("this","is","a","test");
Array<Integer> integerArray = new Array<Integer>(1,32,112,12);
System.out.println("长度"+stringArray.getLength());
System.out.println("长度"+integerArray.getLength());
System.out.println("数组内容"+stringArray.getArray());
System.out.println("数组内容"+integerArray.getArray());
}
}
class Array<T>{
private T[] array;
private int length;
public int getLength() {
return length;
}
public String getArray() {
return Arrays.toString(array);
}
@SafeVarargs
public Array(T...args){
this.length = args.length;
array = args;
}
}
2.定义一个简单的泛型方法
package priv.zsl.fxxx;
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayTest{
public static <T> void printArray(T[] array){
System.out.println(Arrays.toString(array));
}
public static void main(String[] args) {
Array<String> stringArray = new Array<String>("this","is","a","test");
Array<Integer> integerArray = new Array<Integer>(1,32,112,12);
Object[] objects = new Object[10];
objects[0] = integerArray;
objects[1] = stringArray;
printArray(objects);
}
}
class Array<T>{
private T[] array;
private int length;
public int getLength() {
return length;
}
public String getArray() {
return Arrays.toString(array);
}
@SafeVarargs
public Array(T...args){
this.length = args.length;
array = args;
}
@Override
public String toString(){
return "Array"+ " array = "+Arrays.toString(this.array)+" length= "+this.length;
}
}
3.类型变量的限定
在上面的代码基础上进行类型变量限定===><kbd><T extends xxx></kbd> 只有继承该类的类 或者实现该接口的类。才能成为 泛型方法和泛型变量中的数据类型。
? 代码解释: printArray 是打印数组内容。Array是泛型类 ,并且限定只有实现Comparable接口的类才能成为Array中的数据类型。
由于Person类没有实现Comparable接口,所以在执行代码Array<Person> personArray = new Array<Person>(persons); 会报错,
? java: 类型参数priv.zsl.fxxx.Person不在类型变量T的范围内
package priv.zsl.fxxx;
import java.util.Arrays;
public class ArrayTest {
public static <T> void printArray(T[] array) {
System.out.println(Arrays.toString(array));
}
public static void main(String[] args) {
Array<String> stringArray = new Array<String>("this", "is", "a", "test");
Array<Integer> integerArray = new Array<Integer>(1, 32, 112, 12,23,1230);
Object[] objects = new Object[2];
Person[] persons = new Person[3];
persons[0] = new Person("jack",28,180);
persons[1] = new Person("hey",8,183);
persons[2] = new Person("hah",81,170);
objects[0] = integerArray;
objects[1] = stringArray;
System.out.println("排序前");
printArray(objects);
Arrays.sort(objects);
System.out.println("排序后");
printArray(objects);
Array<Person> personArray = new Array<Person>(persons);
}
}
class Array<T extends Comparable> implements Comparable<Array<T>>{
private T[] array;
private int length;
public int getLength() {
return length;
}
public String getArray() {
return Arrays.toString(array);
}
@SafeVarargs
public Array(T... args) {
this.length = args.length;
array = args;
}
@Override
public String toString() {
return "Array" + " array = " + Arrays.toString(this.array) + " length= " + this.length;
}
@Override
public int compareTo(Array<T> o1){
return this.length - o1.length;
}
}
class Person{
private String name;
private int age;
private int height;
public Person(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
|