C#面向對象簡單機器人

類中 set get 可以直接簡寫
private只允許類中成員訪問
類中成員可以調用類中成員
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 機器人
{
    class Program
    {
        static void Main(string[] args)
        {
            機器人 r1 = new 機器人();
            r1.Name = "小Q";
            r1.sayhello();
            r1.Eat(8);
            while(true)
            {
                string str = Console.ReadLine();
                r1.Speak(str);
            }
            Console.ReadKey();
        }

        class 機器人
        {
            //private string name;
            //private int fulllevel;
            public string Name
            {
                set;
                get;
            }
            private int FullLevel
            {
                set;
                get;
            }
            public void sayhello()
            {
            Console.WriteLine("我叫 {0}",this.Name);
            }
            public void Eat(int foodcount)
            {
                if (FullLevel > 100)
                    return;
                else
                    FullLevel = FullLevel + foodcount;
            }

            public void Speak(string str)
            {
                if(FullLevel<=0)
                {
                    Console.WriteLine("餓死了,不說了");
                    return;
                }
                if(str.Contains("你好"))
                    Console.WriteLine("你好");
                else if(str.Contains("姓名")||str.Contains("名字")||str.Contains("name"))
                    this.sayhello();
                else if(str.Contains("女朋友"))
                    Console.WriteLine("年齡太小不考慮");
                else
                    Console.WriteLine("聽不懂");

                FullLevel--;
            }

        }
        
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章