using System; using System.Collections.Generic; using System.Text;
namespace MyShape { //图形类作为其它图形的基类 public class Shape { //绘画方法,方法体为;不提供具体实现,用virtual在子类中重写此方法 public virtual void Draw() { ;//虚方法,用于图形绘制 } public virtual int GetArea() { return 0;//虚方法计算图形面积 } } }
using System; using System.Collections.Generic; using System.Text;
//经测试文件名称.cs与.cs文件中的命名空间的名字与类的名称无关系 namespace MyShape { //定义矩形及正方形类 public class Rectangle:Shape { protected int a; protected int b;//矩形边长 public Rectangle(int va, int vb) { a = va; b = vb; }
//继承父类shape重写父类方法draw public override void Draw() { Console.WriteLine("Rectangle"); Console.WriteLine("* * * * *"); Console.WriteLine("* *"); Console.WriteLine("* *"); Console.WriteLine("* * * * *"); } public override int GetArea() { int area = a * b; return area; }
}
//定义正方形类:继承矩形基类rectangle 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("* * * * *"); Console.WriteLine("Square"); }
} }
using System; using System.Collections.Generic; using System.Text;
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; } }
//定义直角三角形,继承三角形triangle public class RectTriangle : Triangle { new protected int a; new protected int b;//采用new会隐藏父类的同名成员 public RectTriangle(int va, int vb) : base(va, vb, (int)(Math.Sqrt(va * va + vb * vb))) //(Math.Sqrt(va*va+vb*vb)) 表示直角的对边 { a=va; b=vb; } public override int GetArea() {
//定义等腰直角三角形 public class RectEqualTriangle : RectTriangle { new protected int a;//由等腰直角三角形的2个直角边是相同的,故采用new声明一个新的a变量,隐藏父类RectTriangle的成员a与b public RectEqualTriangle(int va) : base(va, va) { a = va; }
using System; using System.Collections.Generic; using System.Text;
namespace MyMessage { class Message { public void Begin() { Console.WriteLine("****** ******"); Console.WriteLine("* * * *"); Console.WriteLine(" shape game "); Console.WriteLine("****************"); }
//控制玩此游戏,按键的行为,按0退出,其它键直接玩 public bool Ask() { Console.WriteLine("按0退出游戏"); Console.WriteLine("按非0键继续玩游戏"); Console.WriteLine(); int c = Console.Read();//从read读取一个字符 if (c == 48) return false; return true; } } }
using System; using System.Collections.Generic; using System.Text;
using MyShape; namespace MyMessage { class Program { static void Main(string[] args) { int score = 1000;//总分,初始玩游戏前每个用户1000分 int win;//玩每局游戏赢取的分数 int choice;//随机得到的图形号 int bet;//每一局玩游戏下的注 string s;//用于存储从标准输入读取的BET赌注 Shape sp = new Shape();//实例一个图形类 Random ran = new Random();//实例一个随机类,用于生成图形号 Message msg = new Message(); Console.ReadKey(); msg.Begin();//绘制图形信息 //开始用while进行循环 while (true) { //如果按键0直接退出退出while循环 if (!msg.Ask()) break; //如下console代码是上述if break不发生才会执行,否则直接就break了 Console.WriteLine("your score:{0}",score);//玩游戏前每个人的分数显示 Console.WriteLine("输入你的赌注:");//赌注bet与分数score有关系 Console.WriteLine(); s = Console.ReadLine();//从标准输入流读取下一行字符,这是为了读取下的赌注bet //对上面获取的赌注进行判断,如输入不正确,进行异常处理,设置赌注为100分 try { bet = Convert.ToInt32(s); }