26属性和构造函数

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

namespace leidejs
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat kitty = new Cat();//创建kitty实例。
            kitty.name = "kitty";
            kitty.Age=5;
            Console.WriteLine("{0}",kitty.Age);

            kitty.Meow();
            kitty.MineCountValue();
            kitty.MineCountValue();

        }
    }
    class Cat
    {
        //public 公共的。
        //private 私有的。
        public string name;//字段
        private int age;
        private int MineCount;
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (value < 0)
                    age = 0;
                else
                    age=value;
            }
        }
        /*
        public void SetAge(int ageValue)
        {
            if (ageValue < 0)
                age = 0;
            else
                age = ageValue;
        }
        public int GetAge()
        {
            return age;
        }
         */

        private void Hello()//私有方法在仅在类的内部使用
        {
            Console.WriteLine("大家好,我是{0}",name);
        }
        public void Meow()
        {
            Hello();
            Console.WriteLine("喵喵...");
        }
        public void MineCountValue()
        {
            MineCount++;
            Hello();
            Console.WriteLine("我抓了{0}只老鼠",MineCount);
        }
    }
}

  

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

namespace leidejs
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat kitty = new Cat("kitty",5);//创建kitty实例。
            //kitty.name = "kitty";
           // kitty.Age=5;
            Console.WriteLine("{0}我{1}了",kitty.name,kitty.Age);

            kitty.Meow();
            kitty.MineCountValue();
            kitty.MineCountValue();

        }
    }
    class Cat
    {
        //public 公共的。
        //private 私有的。
        public string name;//字段
        private int age;
        private int MineCount;
        public Cat(string nameValue, int ageValue)//带参数的构成函数
        {
            name = nameValue;
            age = ageValue;
        }
        public Cat()//无参函数
        {
            name = "";
            age = 0;
        }
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (value < 0)
                    age = 0;
                else
                    age=value;
            }
        }
        /*
        public void SetAge(int ageValue)
        {
            if (ageValue < 0)
                age = 0;
            else
                age = ageValue;
        }
        public int GetAge()
        {
            return age;
        }
         */

        private void Hello()//私有方法在仅在类的内部使用
        {
            Console.WriteLine("大家好,我是{0}",name);
        }
        public void Meow()
        {
            Hello();
            Console.WriteLine("喵喵...");
        }
        public void MineCountValue()
        {
            MineCount++;
            Hello();
            Console.WriteLine("我抓了{0}只老鼠",MineCount);
        }
    }
}

  

posted on 2013-05-03 21:27  杨柳清枫2012  阅读(155)  评论(0)    收藏  举报

导航