C#中对类的字段、属性、方法的简单示例

using System;

using System.Collections.Generic;

using System.Linq; using System.Text;

namespace C字段属性方法

{    

class Program    

{        

static void Main(string[] args)        

{            

School school1 = new School();            

school1.SchoolName="清华";//为SchoolName属性赋值            

school1.Schooladdress="圆明园路";//为Schooladdress属性赋值            

school1.SchoolArea(4000);//为SchoolArea方法赋值,当值超出范围时默认为0            

Console.WriteLine(school1.SchoolName);            

Console.WriteLine(school1.Schooladdress);            

Console.WriteLine(school1.GetSchoolArea());            

Console.ReadKey();        

}        

class School        

{            

private string schoolname;//为schoolname定义一个属性SchoolName            

public string SchoolName            

{//判断是否为真                

set {                    

if (value == "清华大学")                    

{                        

schoolname = value;                    

}                    

else {                        

schoolname = "aaaaaaa";                    

}                

}                

get {//取值                    

return schoolname;                

}            

}            

private string schooladdress;            

// 为schooladdress字段定义一个属性            

public string Schooladdress            

{                

get { return schooladdress; }                

set { schooladdress = value; }            

}

private double schoolarea;

public void SchoolArea(double area) //建立方法为字段schoolarea赋值set            

{                

if (area>10&&area<1000)                

{                    

schoolarea = area;                

}            

}            

public double GetSchoolArea() //返回字段schoolarea的值get            

{                

return schoolarea;            

}

private void zhaosheng()

{                        

}            

private void kaixue()

{                        

}        

}    

}

}

posted @ 2013-05-27 19:51  郑寒松  阅读(423)  评论(0编辑  收藏  举报