本文共 900 字,大约阅读时间需要 3 分钟。
using System;namespace ConsoleApplication15{ class Program { class Person { //标准实现的属性 private int _age;//初始值为0 public int Age { get { return _age; } set { if (value < 0 || value > 130) { Console.WriteLine("设置的年龄有误!"); return; } _age = value; } } //自动实现的属性 public string Name { get; set; } //自动属性也可以有不同的访问权限,如: //public string Name { get; private set; } } static void Main(string[] args) { Person p = new Person(); p.Age = 280; p.Name = "小王"; Console.WriteLine("{0}今年{1}岁。", p.Name, p.Age); Console.ReadKey(); } }}
转载地址:http://ntdaf.baihongyu.com/