黑马程序员_创建类、调用类

namespace 创建类_调用类
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();//创建一个。
            p1.Name = "Tom";
            p1.Age = 18;
            p1.Height = 180;
            p1.SayHello();
            Console.ReadKey();
        }
    }
    class Person
    {   
        /// <summary>
        /// public修饰的,谁都可以访问。private修饰的,只有在本类中才可以访问。
        /// 修饰字段的用private,而不能用public.因为get set可以进行非法值得判断。
        /// 属性开头字母大写,字段开头字母小写。
        /// </summary>
        private int height;
        public int Height
        {
            get//取值
            {
                return this.height;
            }
            set//赋值
            {
                this.height = value; //value代表用户赋值过来的值
            }   
        }

        private int age;
        public int Age//Age没有保存数据,都存在age中了。被调用的是Age
        {
            get//取值
            {
                return this.age;
            }
            set//赋值
            {
                this.age = value; //value代表用户赋值过来的值
            }
        }

        private string name;
        public string Name
        {
            get//取值
            {
                return this.name;
            }
            set//赋值
            {
                this.name = value; //value代表用户赋值过来的值
            }
        }

        public void SayHello()
        {
            //this是用来调用本类的成员。
            Console.WriteLine("你好!我叫{0},我的年龄是{1}岁,我身高{2}cm",this.name,this.age,this.height);
        }
    }
}

 

创建类、调用类的联系:

namespace 练习聊天机器人
{
    class Program
    {
        static void Main(string[] args)
        {
            机器人 r1 = new 机器人();
            r1.Name = "T300";
            r1.Eat(6);

            机器人 r2 = new 机器人();
            r2.Name = "G500";
            r2.Eat(8);
            
            Console.WriteLine("请选择机器人,1-T300,2-G500");
            机器人 r;
            string s=Console.ReadLine();

            if (s == "1")
            {
                r = r1;
            }
            else if (s == "2")
            {
                r = r2;
            }
            else
            {
                Console.WriteLine("输入错误");
                Console.ReadKey();
                return;
            }
  
            r.SayHello();
            while (true)
            {
                string str = Console.ReadLine();
                r.Speak(str);
            }
        }
    }
    class 机器人
    {
        public string Name
        {
            get;
            set;
        }
        private int FullLevel//饥饿程度
        {
            get;
            set;
        }

        public void SayHello()
        {
            Console.WriteLine("我叫{0}",Name);
        }

        public void Eat(int foodCount)
        {
            if (FullLevel>100)
            {
                return;
            }
            FullLevel = FullLevel + foodCount;
        }

        public void Speak(string str)
        {
            if (FullLevel <= 0)
            {
                Console.WriteLine("饿死了,不说了");
            }
            else if (str.Contains("名字") || str.Contains("姓名"))
            {
                this.SayHello();
            }
            else if (str.Contains("女朋友"))
            {
                Console.WriteLine("年龄小,不考虑");
            }
            else
            {
                Console.WriteLine("听不懂!");
            }
            FullLevel--;
        }
    }
}

 

posted @ 2014-04-02 16:16  雨亭天亮  阅读(137)  评论(0)    收藏  举报