码迷,mamicode.com
首页 > Windows程序 > 详细

C#里类的get和set方法编写和调用

时间:2016-12-03 12:06:17      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:c#   void   编写   blog   div   pre   span   new   turn   

using System;
class Date
{
    int day;
    int month;
    int year;
    public int Day{
        get {
            return day; 
        }
        set {
            day = value; 
        }
    }
    

    public int Month{
        get { 
            return month;
        }
        set {
            month = value; 
        }
    }
    

    public int Year{
        get { 
            return year;
        }
        set {
            year = value;
        }
    }
    

    public bool IsLeapYear(int year)
    {
        return year%4== 0 ? true: false; 
    }
    public void SetDate (int day, int month, int year)
    {
        this.day = day;
        this.month = month;
        this.year = year;
    }
}

方法的调用

class User
{
  public static void Main()
  { 
        Date date = new Date(); 
        date.Day = 27; 
        date.Month = 6; 
        date.Year = 2003;
        Console.WriteLine
        ("Date: {0}/{1}/{2}", date.Day, date.Month, date.Year);
    }
}

 

C#里类的get和set方法编写和调用

标签:c#   void   编写   blog   div   pre   span   new   turn   

原文地址:http://www.cnblogs.com/zyh1994/p/6128106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!