stack2.java

来自「Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想」· Java 代码 · 共 62 行

JAVA
62
字号
package chapter11;
import java.util.Vector;
import chapter7.*;
public class Stack2<T extends Animal> 
{
	private  int stackSize = 100; //设置栈的大小
	Vector <T> stack =  new  Vector<T>();
	public Stack2(int size)
	{
		stack.setSize(size);
	}
	//压栈
	public  boolean push(T o)
	{
		if(stack.size() >= stackSize)
		{
			System.out.println("栈溢出");
			return false;
		}
		else
		{
		   return stack.add(o);
		}
	}
	//出栈
	public  T pop()
	{
		if(!isEmpty())
		{
		    T o  = stack.lastElement();
		    stack.remove(o);
		    return o;
		}
		else
		{
			System.out.println("栈空");
			return null;
		}
	}
//	判定栈是否为空
	public boolean isEmpty()
	{
		return stack.isEmpty();
	}
	
	 public static void main(String args[])
 	{
		/*以下内容应无法执行,只好屏蔽
		Stack2<String>  strStack ;
		strStack = new Stack2<String>(10);
		strStack.push(" Generics Class");
		strStack.push(" am");
		strStack.push("I ");
		System.out.println(strStack.pop() + strStack.pop() + strStack.pop() );
		*/
		Stack2<Duck> duckStack = new Stack2<Duck>(5);
		duckStack.push(new Duck());
		Duck duck = duckStack.pop();
		duck.layEgg();
 	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?