C#中的类
一、类的定义
class Study //类名 { string name; //字段 public void setName(string AName) //方法 { this.name = AName; } public string getName() //方法 { return this.name; } }
二、传递参数
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Study { class Program { public enum TimeOfDay { Monring=0, Afternoon=1, Evening=2 } static void Greet(TimeOfDay timeOfDay) { switch (timeOfDay) { case TimeOfDay.Monring: { Console.WriteLine("早上好"); break; } case TimeOfDay.Afternoon: { Console.WriteLine("中午好"); break; } case TimeOfDay.Evening: { Console.WriteLine("晚上好"); break; } } } static void Main(string[] args) { /*TimeOfDay time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "Evening", true); Console.WriteLine((int)time); Console.ReadLine();*/ ushort[] mynum = {1,2,3,4,5,6,7,8,9 }; string name = "kiny"; Study eg = new Study(); byte param = 100; eg.setName(name); Console.WriteLine(eg.getName()); //传递一个字符串 Console.WriteLine(eg.Sum(mynum)); //传递一个数组(也是一个引用) //函数内改变了mynum[0]的值 Console.WriteLine(mynum[0]); //输出500 eg.ChangeParam(ref param); //传递一个引用 Console.WriteLine(param); //输出101 //传递一个类对象 Test myTest = new Test(); Console.WriteLine(eg.ParamObject(myTest)); //输出100 //C#里的变量定义了要就要初始化,在参数传递时就可以不初始化变量,out string out_name; Console.WriteLine(eg.ParamOut(out out_name)); //输出kiny Console.ReadLine(); } } class Study //类名 { string name; //字段 public void setName(string AName) //方法 { this.name = AName; } public string getName() //方法 { return this.name; } public ushort Sum(ushort[] number) { ushort sum = 0; for (int i = 0; i < number.Length -1; i++) { sum += number[i]; } number[0] = 500; return sum; } public void ChangeParam(ref byte Value) { Value += 1; } public string ParamObject(Test obj) { return obj.getAge().ToString(); } public string ParamOut(out string name) { return name = "kiny"; } } class Test { byte age = 100; public void setAge(byte Value) { this.age = Value; } public byte getAge() { return this.age; } } }
三、函数重载(与Delphi非常相似)
不同的参数类型,不同的参数个数,相同的函数名称。
public void setAge(byte Value)
{
this.age = Value;
}
public void setAge(string name,byte Value)
{
this.name = name;
this.age = Value;
}
public string setAge(string name,byte Value) //不是函数重载,函数重载不能用不同的返回类型区分。
{
this.name = name;
this.age = Value;
}
public string setAge(string name,rel byte Value) //不是函数重载,函数重载不能用不用rel out来区分。
{
this.name = name;
this.age = Value;
}
四、属性
private string strName;
public string Name
{
set //省略set即为只读属性
{
strName = value;
}
get //省略get即为只写属性
{
return strName;
}
}
五、构造函数
class Test
{
private Test()
{
//初始化数据
}
private Test(string name,byte age)
{
//构造函数重载
}
}
对于没有构造函数的类,编译器会自动加一个最基本的构造函数,即把所有引用设为null,number设为0,bool设为false。
六、静态构造函数
class Test
{
static Test()
{
//只运行一次的初始化
}
}
七、只读字段
public readonly byte MaxNumber;
只读字段和常量(const)的区别:只读字段允许在类的构造函数内赋值,但不允许在其他地方赋值。
八、类简单的例子
C#类继承
abstract class Human //抽象类 { public string name; public byte age; public Human() { name = null; age = 0; } public abstract string Speak(); } sealed class Man : Human //继承类 { private bool beard; private bool marry; public bool Beard //属性 { set { beard = value; } get { return beard; } } public bool Marry { set { marry = value; } get { return marry; } } public void setAge(byte Value) { base.age = Value; } public byte Age { set { setAge(value); } get { return base.age; } } public Man() //构造函数 { beard = false; marry = false; } public override string Speak() //方法 { return "helloworld"; } }
Delphi里的代码
program Project1; {$APPTYPE CONSOLE} uses SysUtils; type THuman = class private FName:string; FAge:Byte; public constructor Create(); function Speak():string;abstract; property Name:string read FName write FName; property Age:Byte read FAge write FAge; end; TMan = class(THuman) private FBeard:Boolean; FMarry:Boolean; procedure SetAge(Value:Byte); public constructor Create(); function Speak():string;override; property Beard:Boolean read FBeard write FBeard; property Marry:Boolean read FMarry write FMarry; end; { THuman } constructor THuman.Create; begin end; { TMan } constructor TMan.Create; begin end; procedure TMan.SetAge(Value: Byte); begin FAge := Value; end; function TMan.Speak: string; begin Result := 'HelloWorld'; end; begin { TODO -oUser -cConsole Main : Insert code here } end.