Java/Generics/Generic Parameter
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 A simple generic class with two type parameters: T and V.
- 2 A subclass can add its own type parameters.
- 3 Boxing Generic Example
- 4 Create a generic class that can compute the average of an array of numbers of any given type.
- 5 Demonstrate a raw generic type.
- 6 Demonstrate a raw type.
- 7 Java generic: Hierarchy argument
- 8 the type argument for T must be either Number, or a class derived from Number.
- 9 T is a type parameter that will be replaced by a real type when an object of type Gen is created.
A simple generic class with two type parameters: T and V.
class TwoGen<T, V> {
T ob1;
V ob2;
TwoGen(T o1, V o2) {
ob1 = o1;
ob2 = o2;
}
void showTypes() {
System.out.println("Type of T is " + ob1.getClass().getName());
System.out.println("Type of V is " + ob2.getClass().getName());
}
T getob1() {
return ob1;
}
V getob2() {
return ob2;
}
}
public class SimpGen {
public static void main(String args[]) {
TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics");
tgObj.showTypes();
int v = tgObj.getob1();
System.out.println("value: " + v);
String str = tgObj.getob2();
System.out.println("value: " + str);
}
}
A subclass can add its own type parameters.
class Gen<T> {
T ob; // declare an object of type T
Gen(T o) {
ob = o;
}
// Return ob.
T getob() {
return ob;
}
}
// A subclass of Gen that defines a second type parameter, called V.
class Gen2<T, V> extends Gen<T> {
V ob2;
Gen2(T o, V o2) {
super(o);
ob2 = o2;
}
V getob2() {
return ob2;
}
}
class HierDemo {
public static void main(String args[]) {
Gen2<String, Integer> x = new Gen2<String, Integer>("Value is: ", 99);
System.out.print(x.getob());
System.out.println(x.getob2());
}
}
Boxing Generic Example
import java.util.*;
public class BoxingGenericsExample {
public static void main(String args[])
{
HashMap<String,Integer> hm = new HashMap<String,Integer>();
hm.put("speed", 20);
}
}
Create a generic class that can compute the average of an array of numbers of any given type.
class Stats<T> {
T[] nums;
Stats(T[] o) {
nums = o;
}
double average() {
double sum = 0.0;
for (int i = 0; i < nums.length; i++)
sum += nums[i].doubleValue(); // Error!!!
return sum / nums.length;
}
}
Demonstrate a raw generic type.
/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/
class Gen<T> {
T ob; // declare an object of type T
// Pass the constructor a reference to
// an object of type T.
Gen(T o) {
ob = o;
}
// Return ob.
T getob() {
return ob;
}
}
// Demonstrate raw type.
public class RawDemo {
public static void main(String args[]) {
// Create a Gen object for Integers.
Gen<Integer> iOb = new Gen<Integer>(88);
// Create a Gen object for Strings.
Gen<String> strOb = new Gen<String>("Generics Test");
// Create a raw-type Gen object and give it
// a Double value.
Gen raw = new Gen(new Double(98.6));
// Cast here is necessary because type is unknown.
double d = (Double) raw.getob();
System.out.println("value: " + d);
// The use of a raw type can lead to runtime.
// exceptions. Here are some examples.
// The following cast causes a runtime error!
// int i = (Integer) raw.getob(); // runtime error
// This assigment overrides type safety.
strOb = raw; // OK, but potentially wrong
// String str = strOb.getob(); // runtime error
// This assignment also overrides type safety.
raw = iOb; // OK, but potentially wrong
// d = (Double) raw.getob(); // runtime error
}
}
Demonstrate a raw type.
class Gen<T> {
T ob; // declare an object of type T
Gen(T o) {
ob = o;
}
T getob() {
return ob;
}
}
class RawDemo {
public static void main(String args[]) {
Gen<Integer> iOb = new Gen<Integer>(88);
Gen<String> strOb = new Gen<String>("Generics Test");
Gen raw = new Gen(new Double(98.6));
double d = (Double) raw.getob();
System.out.println("value: " + d);
strOb = raw; // OK, but potentially wrong
raw = iOb; // OK, but potentially wrong
}
}
Java generic: Hierarchy argument
class Stats<T extends Number> {
T[] nums;
Stats(T[] o) {
nums = o;
}
double average() {
double sum = 0.0;
for (int i = 0; i < nums.length; i++)
sum += nums[i].doubleValue();
return sum / nums.length;
}
}
public class BoundsDemo {
public static void main(String args[]) {
Integer inums[] = { 1, 2, 3, 4, 5 };
Stats<Integer> iob = new Stats<Integer>(inums);
double v = iob.average();
System.out.println("iob average is " + v);
Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
Stats<Double> dob = new Stats<Double>(dnums);
double w = dob.average();
System.out.println("dob average is " + w);
}
}
the type argument for T must be either Number, or a class derived from Number.
class Stats<T extends Number> {
T[] nums;
Stats(T[] o) {
nums = o;
}
double average() {
double sum = 0.0;
for (int i = 0; i < nums.length; i++)
sum += nums[i].doubleValue();
return sum / nums.length;
}
}
class BoundsDemo {
public static void main(String args[]) {
Integer inums[] = { 1, 2, 3, 4, 5 };
Stats<Integer> iob = new Stats<Integer>(inums);
double v = iob.average();
System.out.println("iob average is " + v);
Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
Stats<Double> dob = new Stats<Double>(dnums);
double w = dob.average();
System.out.println("dob average is " + w);
}
}
T is a type parameter that will be replaced by a real type when an object of type Gen is created.
class Gen<T> {
T ob;
Gen(T o) {
ob = o;
}
T getob() {
return ob;
}
void showType() {
System.out.println("Type of T is " + ob.getClass().getName());
}
}
class GenDemo {
public static void main(String args[]) {
Gen<Integer> iOb;
iOb = new Gen<Integer>(88);
iOb.showType();
int v = iOb.getob();
System.out.println("value: " + v);
System.out.println();
Gen<String> strOb = new Gen<String>("Generics Test");
strOb.showType();
String str = strOb.getob();
System.out.println("value: " + str);
}
}