2019-10-17 19일차 get,set

Console Programming/C# Console 2019. 10. 17. 11:18

Program.cs

namespace Syntax02
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

 

App.cs

using System;
namespace Syntax02
{
    public class App
    {
        public App()
        {
            Date date = new Date();
            int month = date.Month;     //초기값을 get을 통해 가져옴
            Console.WriteLine(month);
            date.Month = 11;            //date.month에 11이라는 값을 넣음
            month = date.Month;         //month에 date.month를 가져와 넣음
            Console.WriteLine(month);

        }
    }
}

 

Date.cs

namespace Syntax02
{
    public class Date
    {
        private int month = 7;      //초기값이 7

        public int Month
        {
            get
            {
                return this.month;
            }
            set
            {
                if ((value > 0) && (value < 13))
                {
                    this.month = value;
                }
            }
        }
    }
}

 

 

: