上面我們介紹了對應用程序結構進行組織的方法,接下來我們舉一個綜合應用的例子。程序實現的功能非常簡單,但其中用到了我們講解的各方面的知識。在程序中,我們利用名字空間把應用程序功能進行分割,並且用到了在一個名字空間中包含多個類、在一個可執行文件中調用多個動態鏈接庫的方法。程序中還用到了異常處理、類的繼承、派生類對虛方法的重載、多態性的實現等概念,希望讀者能夠在閱讀程序時認真注意這些用法,來加深對基本概念的理解。
該程序是一個小游戲,游戲中隨機產生矩形、正方形、直角三角形、等腰直角三角形四種圖形。游戲開始給每個用戶1000分,然後由用戶自行押注,根據押注的多少和隨機產生的圖形面積計算用戶贏取的分數。程序中用到了名字空間System中提供的類Random,該類提供了用於產生一個隨機數的方法。
程序清單16-3:
//MyShape.cs---源文件,用於定義圖形類,作為其它圖形的基類
using System;
namespace MyShape
{
public class Shape
{
public virtual void Draw()
{;} //虛方法,用於圖形繪制
public virtual int GetArea(){
return 0; //虛方法,用於計算圖形面積
}
}
}
//rect.cs---源文件,用於定義矩形類和正方形類
using System;
namespace MyShape
{
public class Rectangle:Shape //定義矩形類
{
protected int a;
protected int b; //矩形的邊長
public Rectangle(int va,int vb)
{
a=va;
b=vb;
}
public override int GetArea() //重載虛方法,計算矩形面積
{
int area=a*b;
return area;
}
public overide void Draw() //重載虛方法,在屏幕上繪制矩形
{
Console.WriteLine("Rectangle:");
Console.WriteLine("* * * * *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* * * * *");
}
}
public class Square:Rectangle //定義正方形類
{
public Square(int va):base(va,va)
{;}
public override void Draw() //重載,繪制正方形
{
Console.WriteLine("Square");
Console.WriteLine("* * * * *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* * * * *");
}
}
}
//triangle.cs---源文件,用於三角形
using System;
namespace MyShape
{
//定義普通三角形,作為其它三角形的基類
public class Triangle:Shape
{
protected int a;
protected int b;
protected int c;
public Triangle(int va,int vb,int vc)
{
a=va;
b=vb;
c=vc;
}
public override int GetArea()
{
int s=(a+b+c)/2;
int area=(int)(Math.Sqrt(s*(s-a)*(s-b)*(s-c)));
return area;
}
}
//定義直角三角形
public class RectTriangle:Triangle
{
new protected int a;
new protected int b;
public RectTriangle(int va,int vb):
base(va,vb,(int)(Math.Sqrt(va*va+vb*vb)))
{
a=va;
b=vb;
}
public override int GetArea()
{
int area=(int)(a*b/2);
return area;
}
public override void Draw()
{
Console.WriteLine("RectTriangle");
Console.WriteLine("*");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* * *);
}
}
//定義等腰直角三角形
public class RectEqualTriangle:RectTriangle
{
new protected int a;
public RectEqualTriangle(int va):base(va,va)
{
a=va;
}
public override int GetArea()
{
int area=(int)(a*a/2);
return area;
}
public override void Draw()
{
Console.WriteLine("RectEqualTriangle");
Console.WriteLine("*");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* * * * *");
}
}
}
//Mymessage.cs----源文件,用於定義程序顯示的一些信息
using System;
namespace MyMessage
{
public class Message
{
public void Begin()
{
Console.WriteLine("*********** ***********");
Console.WriteLine("* * * *");
Console.WriteLine("********** * * **********");
Console.WriteLine("* * * * * *");
Console.WriteLine("* * * * * *");
Console.WriteLine("* * * * * *");
Console.WriteLine("* * * * * *");
Console.WriteLine("* * * * *");
Console.WriteLine("* * * *");
Console.WriteLine("* ** *");
Console.WriteLine("* * *");
Console.WriteLine("* SHAPE GAME *");
Console.WriteLine("**********************************");
}
public bool Ask()
{
Console.WriteLine("Press 0 to exit the game");
Console.WriteLine("Press any other key to continue the game");
Console.WriteLine();
int c=Console.Read();
if(c==48)
return false;
return true;
}
}
}
//client.cs---客戶程序
using System;
using MyShape;
using MyMessage;
class ClIEntTest
{
public static void Main()
{
int score=1000; //總分
int win; //每一局贏取的分數
int choice; //隨機獲得的圖形號
int bet; //每一局下的注
string s;
Shape sp=new Shape();
Random ran=new Random();
Message msg=new Message();
msg.Begin();
while(true)
{
if(!msg.Ask())
break;
Console.WriteLine("Your Score:{0}",score);
Console.WriteLine("Enter your bet:");
Console.ReadLine();
s=Console.ReadLine();
//如果押注的輸入不正確,進行異常處理,並默認下注為100分
try{
bet=s.ToInt32();
}
catch{
bet=100;
}
if(bet<score)
score-=bet;
else{
bet=score;
score=0;
}
Console.WriteLine("Remain Score:{0}",score);
win=0;
for(int i=0;i<3;i++)
{
choice=ran.Next()%4; //隨機數發生器
switch(choice)
{
case 0:
sp=new RectTriangle(5,4);
goto end;
case 1:
sp=new RectEqualTriangle(5);
goto end;
case 2:
sp=new Rectangle(5,4);
goto end;
case 3:
sp=new Square(5);
}
end:
//利用多態性,計算得分
sp.Draw();
win+=sp.GetArea()*(i+1)*bet/100;
Console.WriteLine("Your win:{0}",win);
}
score+=win;
Console.WriteLine("Your Score:{0}",scroe);
if(score<100)
{
Console.WriteLine("Your remain score is not enough to play");
break;
}
}
}
}
對源代碼進行編譯的命令為:
csc/target:library /out:MyShape.dll MyShape.cs rect.cs triangle.cs
csc/target:library /out:MyMessage.dll MyMessage.cs
csc/reference:MyShape.dll;MyMessage.dll clIEnt.cs