using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _61
{
class PropertyTest
{
private int age;
PropertyTest(int age)
{
this.age = age;
}
public int Age
{
get
{
return this.age;
}
set
{
if (value > 0 && value < 100)
{
this.age = value;
}
}
}
}
class ReadProperty
{
private string name;
ReadProperty(string name)
{
this.name = name;
}
public string Name
{
get
{
return this.name;
}
}
}
class Program
{
static void Main(string[] args)
{
PropertyTest propertyTest=new PropertyTest(23);
propertyTest.Age = 33;
Console.WriteLine("年龄为:{0}", propertyTest.Age);
ReadProperty readProperty=new ReadProperty("张三丰");
Console.WriteLine("姓名为:{0}", readProperty.Name);
Console.ReadLine();
}
}
}
c#中不是可以声明属性吗?这个例子是我看的演示程序,为什么运行不出来呢,麻烦高手给看看~~
构造需要是Public的
public PropertyTest(int age)
{
this.age = age;
}