<span style="background-color: rgb(204, 204, 204);">namespace 类的基本应用
{
public class People
{
string user_Name;
int user_Age;
public People(string name,int age)
{
user_Name=name;
user_Age=age;
}
public string Name //属性的作用是能将类中的变量当作属性使用
{
get
{
return user_Name;
}
set
{
user_Name = value;
}
}
public void GetName(People people)
{
Console.WriteLine("此人的姓名为:" + people.user_Name);
}
public static void GetAge(People people)
{
Console.WriteLine("此人的年龄为:"+people.user_Age);
}
}
class Program
{
static void Main(string[] args)
{
People peo_man = new People("张三", 15);
peo_man.GetName(peo_man);
//peo_man.GetAge(poe_man); //错误,对于静态函数,直接用类名调用即可
People.GetAge(peo_man);
Console.WriteLine("名字为:" + peo_man.Name);
}
}
}</span><span style="background-color: rgb(255, 204, 255);">
</span>
<span style="background-color: rgb(204, 204, 204);">
</span>
<span style="background-color: rgb(204, 204, 204);">1、属性的使用很方便,属性创作的关键点是要与类的成员变量产生关系,它能使变量当作类的属性就行调用。</span>
<span style="background-color: rgb(204, 204, 204);">2、静态函数的使用,不需要创建对象就可以进行函数的调用。</span>