在User类中添加两个私有的成员变量,用来表示生日和年龄,并设计相应的属性完成对其的访问,要求生日是只写属性,并能判断出如果用户输入的生日年份小于1900年,或当前年龄小于3岁,给出输入错误提示信息,

public class User
       {
           private string name;
           private string sex;
           private DateTime birthday;
           private int age;
           public string Name
           {
               get { return name; }
               set { name = value; }
           }
           public string Sex
           {
               get { return sex; }
               set
               {
                   if (value == "男" || value == "女")
                   {
                       sex = value;
                   }
                   else
                   {
                       Console.WriteLine("乖乖输入不合适哦");
                   }
               }
           }
           public DateTime Birthday
           {
               set
               {
                   if (value.Year <= 1900 || DateTime.Now.Year - value.Year <= 3)
                   {
                       Console.WriteLine("乖乖输入不合理哦");
                   }
                   else
                   {
                       birthday = value;
                   }
               }
           }
             public int Age
             {
                 get
                 {
                     age=DateTime .Now.Year-birthday.Year;
                     return age;
                 }
             }
       }
        class Program
        {
            static void Main(string[] args)
            {
               User sheep=new User();
                sheep.Name="糕羊";
                sheep.Sex="女";
                sheep.Birthday=Convert.ToDateTime("2003-1-1");
                Console.WriteLine("姓名:"+sheep.Name+"\n性别:"+sheep.Sex+"\n年龄:"+sheep.Age);
                Console.ReadLine();

            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值