c#之多态性_继承_重写_虚函数_new覆盖集成测试

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()
        {
            
            {
                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;//由等腰直角三角形的2个直角边是相同的,故采用new声明一个新的a变量,隐藏父类RectTriangle的成员a与b
        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("* * * * *");
        }
    }
}




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);
                }

                catch
                {
                    bet = 100;//进行异常处理,设置赌注为100分
                }

                //下赌注后对原有的分数进行处理,可能下赌注大于或小于分数,进行相应处理
                if(bet                {
                    score-=bet;//如果下的赌注                }
                else //说明下的赌注>分数,这是不合理,赌注最多=分数
                {
                    bet=score;
                    score=0;//此时分数用光了,全用于赌注了
                }

                //下赌注后显示最新的分数是多少
                Console.WriteLine("余下的分数:{0}",score);

                //下了赌注及更新最新的分数,下来开始根据随机选取的图形及图形面积生成玩此游戏可以赢取的分数win
                win=0;//对win进行初始化为0

                //三种可能图形情况,分别为:
                for(int i=0;i<3;i++)
                {
                    choice = ran.Next()%4;//next返回一个非负的随机数
                    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;//采用goto 直接跳转到代码块end
                        case 3:
                            sp = new Square(5);//正方形
                            goto end;

                    }

                end: //end代码块与上面的goto对应匹配,end后跟:冒号
                    //下面为end代码块的具体内容
                    //通过多态性计算win得分
                    //end实现计算win得分及绘画draw
                    sp.Draw();//这就是多态性,因为在子类对draw方法进行了不同的重写.是用父类调用,才会产生多态性
                    win+=sp.GetArea()*(i+1)*bet/100;
                    Console.WriteLine("你的win得分是{0}",win);

                }

                //上述玩完游戏一把,下了赌注bet,更新了score,有了新的得分win,就要再次更新score(因为score与bet及win有关)
                score += win;
                Console.WriteLine("玩完游戏后你的分数是score",score);

                //根据最终得到的分数score进行不同的处理
                if (score < 100)
                {
                    Console.WriteLine("你的余下分数不足以再玩游戏了.停手吧,哈哈");
                    //提示上述信息后,直接break
                    break;
                }
                Console.ReadKey();
            }

        }
    }
}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-718377/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9240380/viewspace-718377/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值