⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack2.java

📁 Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -