主题:[讨论]有人会java泛型Generic types吗?怎样继承的?
假如要写一个类去执行名叫PTList的类,该怎样写?我不是要完整答案,我只想知道基本结构,真的不太懂泛型。。请高手帮帮忙..谢谢。。
public class Stage1BasicTestDriver {
private static void testList(PTList<Integer> list,String testCase) {
System.out.println("size of " + testCase + " is " + list.size());
System.out.println("empty(ness) of " + testCase + " is " + list.isEmpty());
System.out.println("String form of " + testCase + " is " + list.toString());
System.out.println("contains(0) of " + testCase + " is " + list.contains(0));
System.out.println("contains(1) of " + testCase + " is " + list.contains(1));
System.out.println("contains(2) of " + testCase + " is " + list.contains(2));
System.out.println("contains(3) of " + testCase + " is " + list.contains(3));
System.out.println("contains(4) of " + testCase + " is " + list.contains(4));
System.out.println("indexOf(0) of " + testCase + " is " + list.indexOf(0));
System.out.println("indexOf(1) of " + testCase + " is " + list.indexOf(1));
System.out.println("indexOf(2) of " + testCase + " is " + list.indexOf(2));
System.out.println("indexOf(3) of " + testCase + " is " + list.indexOf(3));
System.out.println("indexOf(4) of " + testCase + " is " + list.indexOf(4));
System.out.println("lastIndexOf(0) of " + testCase + " is " + list.lastIndexOf(0));
System.out.println("lastIndexOf(1) of " + testCase + " is " + list.lastIndexOf(1));
System.out.println("lastIndexOf(2) of " + testCase + " is " + list.lastIndexOf(2));
System.out.println("lastIndexOf(3) of " + testCase + " is " + list.lastIndexOf(3));
System.out.println("lastIndexOf(4) of " + testCase + " is " + list.lastIndexOf(4));
try {
String mesg = "get(0) of " + testCase + " is " + list.get(0);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(0) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(1) of " + testCase + " is " + list.get(1);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(1) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(2) of " + testCase + " is " + list.get(2);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(2) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(3) of " + testCase + " is " + list.get(3);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(3) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(4) of " + testCase + " is " + list.get(4);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(4) of " + testCase + " throws IndexOutOfBoundsException");
}
}
public static void main(String[] args) {
String op = args[0];
PTList<Integer> list = new PTListImpl<Integer>();
if ( op.equals("new" ) ) testList(list,"new list");
list.add(0);
if ( op.equals("one" ) ) testList(list,"one item list");
list.add(1);
if ( op.equals("two" ) ) testList(list,"two item list");
list.add(2);
list.add(4);
if ( op.equals("four" ) ) testList(list,"four item list");
list.add(1);
list.add(0);
if ( op.equals("dups" ) ) testList(list,"list with duplicates");
}
}
---------------------------------------------------------
import java.util.Iterator;
public interface PTList<ElementType> extends ConstantPTList<ElementType> {
void add(ElementType newElement);
void add(int atPosition, ElementType newElement);
void clear();
Iterator<ElementType> iterator();
ElementType remove(int atPosition); ElementType set(int atPosition, ElementType newElement);
}
------------------------------------------------------
public interface ConstantPTList<ElementType> {
int size();
boolean isEmpty();
String toString();
boolean contains(ElementType possibleElement);
int indexOf(ElementType possibleElement);
int lastIndexOf(ElementType possibleElement);
ElementType get(int atPosition);
}
public class Stage1BasicTestDriver {
private static void testList(PTList<Integer> list,String testCase) {
System.out.println("size of " + testCase + " is " + list.size());
System.out.println("empty(ness) of " + testCase + " is " + list.isEmpty());
System.out.println("String form of " + testCase + " is " + list.toString());
System.out.println("contains(0) of " + testCase + " is " + list.contains(0));
System.out.println("contains(1) of " + testCase + " is " + list.contains(1));
System.out.println("contains(2) of " + testCase + " is " + list.contains(2));
System.out.println("contains(3) of " + testCase + " is " + list.contains(3));
System.out.println("contains(4) of " + testCase + " is " + list.contains(4));
System.out.println("indexOf(0) of " + testCase + " is " + list.indexOf(0));
System.out.println("indexOf(1) of " + testCase + " is " + list.indexOf(1));
System.out.println("indexOf(2) of " + testCase + " is " + list.indexOf(2));
System.out.println("indexOf(3) of " + testCase + " is " + list.indexOf(3));
System.out.println("indexOf(4) of " + testCase + " is " + list.indexOf(4));
System.out.println("lastIndexOf(0) of " + testCase + " is " + list.lastIndexOf(0));
System.out.println("lastIndexOf(1) of " + testCase + " is " + list.lastIndexOf(1));
System.out.println("lastIndexOf(2) of " + testCase + " is " + list.lastIndexOf(2));
System.out.println("lastIndexOf(3) of " + testCase + " is " + list.lastIndexOf(3));
System.out.println("lastIndexOf(4) of " + testCase + " is " + list.lastIndexOf(4));
try {
String mesg = "get(0) of " + testCase + " is " + list.get(0);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(0) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(1) of " + testCase + " is " + list.get(1);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(1) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(2) of " + testCase + " is " + list.get(2);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(2) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(3) of " + testCase + " is " + list.get(3);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(3) of " + testCase + " throws IndexOutOfBoundsException");
}
try {
String mesg = "get(4) of " + testCase + " is " + list.get(4);
System.out.println(mesg);
} catch(IndexOutOfBoundsException ex) {
System.out.println("get(4) of " + testCase + " throws IndexOutOfBoundsException");
}
}
public static void main(String[] args) {
String op = args[0];
PTList<Integer> list = new PTListImpl<Integer>();
if ( op.equals("new" ) ) testList(list,"new list");
list.add(0);
if ( op.equals("one" ) ) testList(list,"one item list");
list.add(1);
if ( op.equals("two" ) ) testList(list,"two item list");
list.add(2);
list.add(4);
if ( op.equals("four" ) ) testList(list,"four item list");
list.add(1);
list.add(0);
if ( op.equals("dups" ) ) testList(list,"list with duplicates");
}
}
---------------------------------------------------------
import java.util.Iterator;
public interface PTList<ElementType> extends ConstantPTList<ElementType> {
void add(ElementType newElement);
void add(int atPosition, ElementType newElement);
void clear();
Iterator<ElementType> iterator();
ElementType remove(int atPosition); ElementType set(int atPosition, ElementType newElement);
}
------------------------------------------------------
public interface ConstantPTList<ElementType> {
int size();
boolean isEmpty();
String toString();
boolean contains(ElementType possibleElement);
int indexOf(ElementType possibleElement);
int lastIndexOf(ElementType possibleElement);
ElementType get(int atPosition);
}