c#之多型性_繼承_重寫_虛擬函式_new覆蓋整合測試

wisdomone1發表於2012-03-12
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章