很多初学OO的同学们在抽象类、借口、继承一直学的不是很透彻,终其原因是因为木有理解~今天Xushine研究院给大家展示一个有意思的小程序。帮助大家理解OO~
一个家庭 相当于 一个空间,这个空间里 有 很多元素,比如 爱,爱这个抽象事物,可能有很多动作,接吻、交流,对于一个爱,必须有2个人物来实现,这个就是对象。
抽象类—爱 就是 每个人都可以继承的类,但是他本身 无法实现 对象,爱可以变成一个人吗? 不能(抽象类不可以实例化)
接口—每个人对于爱的表达 可以是:接吻,交流,ML,但是每个人 交流方式 和接吻方式一样吗?  你要是想拥有爱,肯定会接吻,肯会交流,肯定会ML,这就是与接口的约定,每个人的方式不一样,这个就是 多态。
普通类—对于一个拥有爱的男人,也就是你继承了 爱的元素,你就会有它的元素。这就是继承

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

namespace Family
{

    /* 2个年轻人,
     * 要想组成一个家庭必须经历 
     * Kiss -> talking -> Make love  
     * (因人而异顺序改变)
    */

    //爱是每个人都配拥有的
    abstract class Love   
    {
        //字段
        protected string boyName;
        protected string girlName;
        protected string girlBeautiful;
        protected int boyAge;
        protected int girlAge;
        //属性
        public string BoyName
        {
            get { return boyName; }
            set { boyName = value; }
        }
        public int BoyAge
        {
            get { return boyAge; }
            set { boyAge = (value >= 18) ? value : 0; }
        }

        public string GirlName
        {
            get { return girlName; }
            set { girlName = value; }
        }
        public int GirlAge
        {
            get { return girlAge; }
            set { girlAge = (value >= 18) ? value : 0; }
        }

        //爱的构造初始化
        public Love(string boyname, int boyage)//如果男人拥有爱
        {
            this.BoyName = boyname;
            this.BoyAge = boyage;

        }
        public Love(string girlname, int girlage, string description)//如果女人拥有爱
        {
            this.GirlName = girlname;
            this.GirlAge = girlage;
            this.girlBeautiful = description;
        }

    }
    //经历(接口)
    interface IExperience 
    {
        void Kiss();
        void Talking();
        void MakeLove();

    }

    class Boy : Love, IExperience
    {
        //男生的字段
        private decimal boySalay;
        //男生的属性收入
        public decimal BoySalay
        {
            get { return boySalay; }
            set 
            {
              
                    if (value >= 2000)
                    {
                        boySalay = value;
                    }
                    if (value < 2000)
                    {
                        Console.WriteLine("你的收入不够谈恋爱");
                    }
              
            }
        }
        public Boy(string boyname, int boyage, decimal salay)
            : base(boyname, boyage)
        {
            this.BoySalay = salay;
        }
        //接吻
        public void Kiss()
        {
            int status = 10;

            for (int i = 1; i < status; i++,status--)
            {
                Console.WriteLine("第{0}天:\\n我:我们接吻吧,你愿意吗?", i);
                Console.Write("你:");
                string str = Console.ReadLine();
                if (str.ToUpper() == "YES")
                {
                    Console.WriteLine();
                    Console.WriteLine("恭喜你");
                    return;
                }
                else if(str=="不准")
                {
                    Console.WriteLine("我:不放弃...");
                   
                }
                
                if (status <= 0)
                {
                    Console.WriteLine("我:良缘难再");
                }

            }
        
            
        }
        //交流
        public void Talking()
        {
            int status = 5;
            for (int i = 0; i < status; i++)
            {
                Console.WriteLine("我:你要分手吗“");
                Console.Write("你:");
                string str = Console.ReadLine();
                if (str.ToUpper() == "YES")
                {
                    Console.WriteLine("我:好吧,我是孤独的!");
                    return;
                }
                else if(str=="再说")
                {
                    Console.WriteLine("我:我懂你意思了。");
                    return;
                }
            }
        }

        //Making Love
        public void MakeLove()
        {
            Console.WriteLine("深夜....");
            Console.WriteLine("One Night,one boy and one girl......");
        }
    }

    public class Test
    {
        static void Main()
        {
            Console.WriteLine("[2个年轻人]\\n[要想组成一个家庭必须经历]\\n[Kiss -> talking -> Make love]\\n(因人而异顺序改变) \\n");
            Console.Write("恋爱进行时");
            for (int i = 0; i < 10; i++)
            {
                Console.Write(".");
                Thread.Sleep(500);
            }
            Console.WriteLine();
            Console.Write("某天,我们能谈恋爱吗?");
            for (int i = 0; i < 10; i++)
            {
                Console.Write(".");
                Thread.Sleep(500);
            }
            Console.WriteLine();

            Boy oneboy = new Boy("jack",20,2000);
            oneboy.Kiss();
            oneboy.Talking();
            oneboy.MakeLove();
        }
    }

}

 

 

评论被关闭。