28-1 以对象为成员

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

namespace gonggu
{
    class Program
    {
        static void Main(string[] args)
        {
            Data birthday = new Data(1988,2,1,new Time(17,30,50));
            Console.WriteLine("我的生日是{0}年{1}月{2}日{3}",birthday.year,birthday.mouth,birthday.year,birthday.clock.toString24());
            
        }
    }
    class Data
    {
        public int year;
        public int mouth;
        public int day;
        public Time clock;
        public Data(int yearValue, int mouthValue, int dayValue, Time clockValue)
        {
            year = yearValue;
            mouth = mouthValue;
            day = dayValue;
            clock = clockValue;
        }
    }
}

  

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

namespace 狼与狮子
{
    class Program
    {
        static void Main(string[] args)
        {
            Lion lion = new Lion();
            Wolf[] wolf = new Wolf[8];//对象数组
            for (int i = 0; i < wolf.Length; i++)
            {
                wolf[i] = new Wolf();
            }
            Console.WriteLine("我们现在共有狼{0}只",Wolf.WolvesCount);
            for (int i = 0; i < wolf.Length; i++)
            {
                Console.WriteLine("狼{0}",i);
                wolf[i].Act();
            }
            lion.Counterattack();
        }
    }
    class Wolf
    {
        public static int WolvesCount = 0;

        public Wolf()//构造函数
        {
            WolvesCount++;
        }
        ~Wolf()//析构函数
        {
            WolvesCount--;
        }
        public void Act()
        {
            if (WolvesCount < 5)
                Console.WriteLine("狼被狮子打败");
            else
                Console.WriteLine("狮子获胜");
        }
    }
    class Lion
    {
        public void Counterattack()
        {
            if (Wolf.WolvesCount < 7)
                Console.WriteLine("狮子胜利");
            else
                Console.WriteLine("狼胜利了");
        }
    }
}

  

posted on 2013-05-04 22:22  杨柳清枫2012  阅读(143)  评论(0)    收藏  举报

导航