1.索引器
索引器就是一类特殊的属性,通过它们你就可以像引用数组一样访问对象元素的功能。显然,这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件或者抽象某些有限资源等,能让类具有类似数组的行为当然也是非常有用的。本文就会引领你设置类来采用索引器。
索引器的定义如下所示:
[修饰符] 数据类型 this [int index] {访问函数体代码}
自己写的一个类的简单的索引:
namespace InterfaceTest {
classindexx {
publicstring nn = \; publicint dd= 0;
public indexx(string name, int id) {
nn=name; dd = id; } }
classsindex {
privateindexx[] Try = newindexx[10]; publicindexxthis[int index] { get
{
if (index >= 0 || index < 10) return Try[index]; elsereturnnull; } set
{
if (index >= 0 || index < 10)
Try[index] = value; } } }
publicclassapp {
publicstaticvoid Main() {
sindex sin = newsindex();
sin[1] = newindexx(\, 4312);
Console.WriteLine(\, sin[1].nn, sin[1].dd); Console.ReadLine(); } } }
2.重载运算符
重载运算符的格式为:
Public static 返回类型 operator 运算符(参数表){} 示例代码:
namespace InterfaceTest {
publicclassReload {
publicint x, y;
public Reload() { x = y = 0; } public Reload(int i,int j) { this.x = i; this.y = j; }
publicstaticReloadoperator +(Reload rr, Reload dd) {
Reload nn = newReload(); nn.x = rr.x + dd.x; nn.y = rr.y + dd.y; return nn; }
publicstaticReloadoperator +(Reload rr, int d) {
Reload nn = newReload(); nn.x = rr.x + d; nn.y = rr.y + d; return nn; }
}
publicclassRun {
publicstaticvoid Main() {
Reload xx = newReload(23, 43); Reload yy = newReload(); int g = 10;
// Reload uu = new Reload(); // Reload ii = new Reload(); Reload uu = xx + yy; Reload ii = xx + g;
Console.WriteLine(\, uu.x, uu.y); Console.WriteLine(\, ii.x, ii.y); Console.ReadLine(); } } }
3.多态性
C#的多态性是指当对不同类的对象执行相同的方法时,系统能根据不同类的对象正确辨别调用各对象所属类的相应方法,从而产生不同的结果。
多态性是通过”虚方法重载”来实现在程序中调用相应对象所属类中的方法,而不是调用基类的方法。虚方法重载就是将基类的某个方法在其派生类中重新定义,而方法名和方法的参数都不改变。虚方法即加修饰符virtual,派生类中用override进行覆盖。
非虚方法真正执行的功能是编译时的对象所属的类中的方法;虚方法面向运行时它实际所属的类的对象,也即虚方法真正执行的功能是运行时的实际对象所属的类中的方法。 New关键字用于派生类对象希望调用派生类中定义的与其基类同名,但作用效果不同的方法;虚方法用于自动实现派生类对象调用自己类中的与基类同名、但作用效果不同的方法。 示例代码:
namespace InterfaceTest {
classVirtualTest { }
publicclassEmployeee {
publicstring name; publiclong idcard; publicdouble salary; publicdouble increase;
public Employeee(string n, long i, double m) {
name = n; idcard = i; salary = m; }
publicvoid Print() {
Console.WriteLine(\姓名:{0}\, name); Console.WriteLine(\身份证号:{0}\, idcard); Console.WriteLine(\基本工资:{0}\, salary); Console.WriteLine(\增加工资额:{0}\, increase); }
publicvirtualvoid Raise(double percent) {
increase = percent * salary; } }
publicclassWorkEmp : Employeee {
public WorkEmp(string i, long j, double k) : base(i, j, k) { }
publicoverridevoid Raise(double percent) {
Console.WriteLine(\普通员工工资增加数额计算:\); base.increase = base.salary * percent; } }
publicclassBachelorEmp : Employeee {
public BachelorEmp(string i, long j, double k) : base(i, j, k) { }
publicoverridevoid Raise(double percent) {
Console.WriteLine(\本科员工工资增加数额计算:\);
base.Raise(percent);
base.increase = base.increase*2; } }
publicclassMasterEmp : Employeee {
public MasterEmp(string i, long j, double k) : base(i, j, k) { }
publicoverridevoid Raise(double percent) {
Console.WriteLine(\硕士员工工资增加数额计算:\); base.Raise(percent);
base.increase = base.increase * 3; } }
publicclassApplication {
publicstaticvoid Main() {
Employeee jia = newEmployeee(\,0,0); WorkEmp aa = newWorkEmp(\, 54371221, 2000); jia = aa; jia.Raise(0.05); jia.Print();
BachelorEmp bb = newBachelorEmp(\, 54371221, 2000); jia = bb; jia.Raise(0.05); jia.Print();
MasterEmp cc = newMasterEmp(\, 54371221, 2000); jia = cc; jia.Raise(0.05); jia.Print();
Console.ReadLine(); } } }