傻瓜先生DIM 2019-04-13 11:48 采纳率: 100%
浏览 1366
已结题

C#测试类怎么写 (急用)???

求C#的测试类(急用)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program
{
    enum Sexkind { Male, Female }
    class Human
    {
        private string no;
        public string No
        {
            get { return no; }
            set
            {

                int y = int.Parse(no.Substring(0, 4));
                if (no == null || no.Length != 12)


                    throw new Exception("长度不到十二位");
                else if
                    (y > DateTime.Now.Year || y < DateTime.Now.Year - 100)
                    throw new Exception("年份错误");
                else
                    no = value;
            }
        }
        public string Name { set; get; }
        public Sexkind Sex { set; get; }
        public int count { set; get; }
        public int Year
        {
            get { return int.Parse(no.Substring(0, 4)); }
        }

        public string Info
        {
            get { return No + "\t" + Name + "\t" + Sex; }
        }
        public Human()
        {
            count++;
        }
        public Human(string no, string name, Sexkind sex)
        {
            count++;
            No = no;
            Name = name;
            Sex = sex;
        }
        public void Show()
        {
            Console.WriteLine(Info);
        }
        public void SayHello()
        {
            Console.WriteLine("Hello, human!");
        }
        public delegate void State(object sender, string message);
        public event State OnIdle;
        public event State OnWork;

        static void Main(string[] args)
        {
            Human hu = new Human("201902211122", "哇哈哈", Sexkind.Male);
            hu.Show();
            hu.SayHello();



        }
    }
}

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-04-13 11:53
    关注
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Q756552
    {
        class Human
        {
            private string no;
            public string No
            {
                get { return no; }
                set
                {
                    no = value;
                    int y = int.Parse(no.Substring(0, 4));
                    if (no == null || no.Length != 12)
                        throw new Exception("长度不到十二位");
                    else if
                        (y > DateTime.Now.Year || y < DateTime.Now.Year - 100)
                        throw new Exception("年份错误");
                }
            }
            public string Name { set; get; }
            public Sexkind Sex { set; get; }
            public static int count { set; get; }
            public int Year
            {
                get { return int.Parse(no.Substring(0, 4)); }
            }
    
            public string Info
            {
                get { return No + "\t" + Name + "\t" + Sex; }
            }
            public Human()
            {
                count++;
            }
            public Human(string no, string name, Sexkind sex)
            {
                count++;
                No = no;
                Name = name;
                Sex = sex;
            }
            public void Show()
            {
                Console.WriteLine(Info);
            }
            public void SayHello()
            {
                Console.WriteLine("Hello, human!");
            }
            public delegate void State(object sender, string message);
            public event State OnIdle;
            public event State OnWork;
    
            public void DoWork()
            {
                if (OnWork != null)
                    OnWork(this, "I am working");
            }
        }
    
        enum Sexkind { Male, Female }
    
        class Program
        {
    
            static void Main(string[] args)
            {
                Human hu = new Human("201902211122", "哇哈哈", Sexkind.Male);
                hu.SayHello();
                hu.Show();
                hu.OnWork += new Human.State(hu_OnWork);
                hu.DoWork();
                Console.WriteLine(hu.Info);
                Human hu1 = new Human();
                Console.WriteLine(Human.count);
            }
    
            static void hu_OnWork(object sender, string message)
            {
                Console.WriteLine("the work says: " + message);
            }
        }
    }
    
    
    评论

报告相同问题?