using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 资源回收
{
class Program
{
static void Main(string[] args)
{
using (Telphone t1 = new Telphone())
{
t1.Open();
t1.Speak("hello");
t1.Bomb();
//t1.Dispose();
t1.Open();
t1.Speak("I am back!");
//t1.Close();//可以不close,Dispose中会自动帮我们Close
}
Console.ReadKey();
}
}
class Telphone : IDisposable
{
private TelphoneState state;
public void Open()
{
if (state == TelphoneState.Disposed)
{
throw new Exception("电话线已经被剪断,无法打开!");
}
state = TelphoneState.Open;