泛型
泛型是所有类的抽象代表,一种占位符,表示存在一种类,但不确定具体是哪一个类。
例子
- 定容栈 FixedCapacityStackOfString—存储对象是字符串 FixedCapacityStack—泛型Item代表储存对象
类图
红框显示出二者的区别
实现:
//FixedCapacityStackOfString---存储对象是字符串
public class FixedCapacityStackOfString {
private String[] a;
private int N;
public FixedCapacityStackOfString(int cap)
{
a = new String[cap];
}
public boolean isEmpty()
{
return N == 0;
}
public int size()
{
return N;
}
public void push(String str)
{
a[N++] = str;
}
public String pop()
{
String str = a[--N];
a[N] = null;
return str;
}
}
//FixedCapacityStack---泛型 Item 代表储存对象
public class FixedCapacityStack<Item> {
private Item[] a;
private int N;
public FixedCapacityStack(int cap)
{
a =(Item[]) new Object[cap];//泛型数组的初始化只能通过这种方式
}
public boolean isEmpty()
{
return N==0;
}
public int size()
{
return N;
}
public void push(Item item)
{
a[N++] = item;
}
public Item pop()
{
Item i = a[--N];
a[N] = null;//防止对象游离(将对象设为空以后被引用指向的空间将会适时被回收)
return i;
}
}
使用
//书上的使用是通过控制台 数据以文本的方式输入 所以判断是否继续循环可以用 !Stdin.isEmpty()
//我的数据通过控制台输入 以“#”作为数据的结束标志
@Test
void testStackOfString() {
FixedCapacityStackOfString f = new FixedCapacityStackOfString(100);
String str = "";
while(!str.equals("#"))// # 号作为输入结束符
{
str = StdIn.readString();
if(!str.equals("-")&!str.equals("#"))
{
f.push(str);
}
else if(!f.isEmpty()&!str.equals("#")){
StdOut.print(f.pop() + " ");
}
}
StdOut.println();
StdOut.println("(the length of f is "+f.size()+")");
}
@Test
void testStackOfItem() {
FixedCapacityStack<String> f = new FixedCapacityStack<String>(100);
String str = "";
while(!str.equals("#"))// # 号作为结束符
{
str = StdIn.readString();
if(!str.equals("-")&!str.equals("#"))
{
f.push(str);
}
else if(!f.isEmpty()&!str.equals("#")){
StdOut.print(f.pop() + " ");
}
}
StdOut.println();
StdOut.println("(the length of f is "+f.size()+")");
}
区别部分
FixedCapacityStackOfString f = new FixedCapacityStackOfString(100);
FixedCapacityStack<String> f = new FixedCapacityStack<String>(100);
数据
to be or not - to be - - that - - - is #
结果
not be to that or be (the length of f is 2)
以泛型作为储存对象其所能储存的对象将不再局限于一种特定的类,而是所有类。
问题
Object 是所有类的基类,以它作为储存对象,同样能够满足 代表所有类的功能为什么需要泛型?
在代表作用上,两者有什么区别?
初步探索》》》
以Object作为储存对象
类图
Object 实现
public class FixedCapacityStackOfObject {
private Object[] a;
private int N;
public FixedCapacityStackOfObject(int cap)
{
a =new Object[cap];
}
public boolean isEmpty()
{
return N==0;
}
public int size()
{
return N;
}
public void push(Object item)
{
a[N++] = item;
}
public Object pop()
{
Object i = a[--N];
a[N] = null;
return i;
}
}
测试
@Test
void testStackOfObject() {
FixedCapacityStackOfObject f = new FixedCapacityStackOfObject(2);
String str = "";
while(!str.equals("#"))// # 号作为结束符
{
str = StdIn.readString();
if(!str.equals("-")&!str.equals("#"))
{
f.push(str);
}
else if(!f.isEmpty()&!str.equals("#")){
StdOut.print(f.pop() + " ");
}
}
StdOut.println();
StdOut.println("(the length of f is "+f.size()+")");
}
数据
to be or not - to be - - that - - - is #
结果
not be to that or be (the length of f is 2)
像这样 ,以Object 作为储存对象似乎也把“代表”工作做的很好嘛。
这里我只证明了Object可以作为所有类的代表!即代表作用