静态字段小猫

来源:互联网 发布:斯维尔算量软件多少钱 编辑:程序博客网 时间:2024/04/25 04:07

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Cat
    {
        int a = 0;
        static int CatNum = 0;
        private string name;
        public void setCat(string str)
        {
            CatNum++;
            name = str;
            a = CatNum;
         }
        public void HowManyCats()
        { Console.Write("这是{0},这是我养的第{1}只猫,", name,a);
          Console.WriteLine("当前共有{0}只猫", CatNum);

        }
    }

    class Program
    {
        static void Main()
        {
            Cat myCat1 = new Cat();
            Cat myCat2 = new Cat();
            myCat1.setCat("小白");
            myCat1.HowManyCats();

            myCat2.setCat("小黑");
            myCat2.HowManyCats();

            myCat1.HowManyCats();
            Console.ReadKey();
       
        }
    }

}

0 0
原创粉丝点击