网站推广策划报告,wordpress修改数据库密码,百度小说风云榜排名完结,康乐县网站建设Java面向对象设计 - Java泛型方法和构造函数
泛型方法
我们可以在方法声明中定义类型参数#xff0c;它们在方法的返回类型之前的尖括号中指定。
包含泛型方法声明的类型不必是泛型类型。
我们可以在非静态方法声明中使用为泛型类型指定的类型参数。
例子
以下代码显示如…Java面向对象设计 - Java泛型方法和构造函数
泛型方法
我们可以在方法声明中定义类型参数它们在方法的返回类型之前的尖括号中指定。
包含泛型方法声明的类型不必是泛型类型。
我们可以在非静态方法声明中使用为泛型类型指定的类型参数。
例子
以下代码显示如何为方法m1()定义新的类型参数V.
新类型参数V强制方法m1()的第一个和第二个参数为相同类型。
第三个参数必须是相同的类型T这是类实例化的类型。
class MyBagT {private T ref;public MyBag(T ref) {this.ref ref;}public T get() {return ref;}public void set(T a) {this.ref a;}
}
class TestT {public V void m1(MyBagV a, MyBagV b, T c) {}
}使用泛型方法
要传递方法的形式类型参数的实际类型参数我们必须在方法调用中的点和方法名之间的尖括号中指定它。
class MyBagT {private T ref;public MyBag(T ref) {this.ref ref;}public T get() {return ref;}public void set(T a) {this.ref a;}
}
class TestT {public V void m1(MyBagV a, MyBagV b, T c) {}
}
public class Main {public static void main(String[] argv) {TestString t new TestString();MyBagInteger iw1 new MyBagInteger(new Integer(201));MyBagInteger iw2 new MyBagInteger(new Integer(202));// Specify that Integer is the actual type for the type parameter for m1()t.Integerm1(iw1, iw2, hello);t.m1(iw1, iw2, hello);}
}例2
以下代码显示了如何声明泛型静态方法。
我们不能在静态方法内引用包含类的类型参数。
静态方法只能引用它自己声明的类型参数。
以下静态通用类型定义了类型参数T用于约束参数source和dest的类型。
class MyBagT {private T ref;public MyBag(T ref) {this.ref ref;}public T get() {return ref;}public void set(T a) {this.ref a;}
}public class Main {public static T void copy(MyBagT source, MyBag? super T dest) {T value source.get();dest.set(value);}public static void main(String[] argv) {}
}要为静态方法调用指定实际的类型参数我们可以这样做:
Main.Integercopy(iw1, iw2);泛型构造函数
我们可以为构造函数定义类型参数。
下面的代码定义了类Test的构造函数的类型参数U.
它放置一个约束即构造函数的类型参数U必须是相同的或者它的类类型参数T的实际类型的子类型。
public class TestT {public U extends T Test(U k) {}
}要为构造函数指定实际的类型参数值请在新运算符和构造函数名称之间的尖括号中指定它如以下代码段所示
class TestT {public U extends T Test(U k) {}
}public class Main {public static void main(String[] argv) {// Specify the actual type parameter for the constructor as DoubleTestNumber t1 newDouble TestNumber(new Double(1.9));// Let the compiler figure out, Integer is// the actual type parameter for the constructorTestNumber t2 new TestNumber(new Integer(1));}
}泛型对象创建中的类型推断
Java 7在泛型类型的对象创建表达式中增加了对类型推断的有限支持。
对于以下语句:
ListString list new ArrayListString();在Java 7中可以在上面的语句中指定空尖括号称为菱形操作符或简单的菱形作为ArrayList的类型参数。
ListString list new ArrayList(); 如果我们不在对象创建表达式中为泛型类型指定类型参数那么类型是原始类型编译器生成未检查的警告。
例如以下语句将编译未选中的警告
ListString list new ArrayList(); // Generates an unchecked warning我们不能创建泛型的异常类。并且没有泛型的匿名类。