C#高级编程学习笔记和代码

上传人:博****1 文档编号:499208690 上传时间:2023-12-04 格式:DOCX 页数:29 大小:107.41KB
返回 下载 相关 举报
C#高级编程学习笔记和代码_第1页
第1页 / 共29页
C#高级编程学习笔记和代码_第2页
第2页 / 共29页
C#高级编程学习笔记和代码_第3页
第3页 / 共29页
C#高级编程学习笔记和代码_第4页
第4页 / 共29页
C#高级编程学习笔记和代码_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《C#高级编程学习笔记和代码》由会员分享,可在线阅读,更多相关《C#高级编程学习笔记和代码(29页珍藏版)》请在金锄头文库上搜索。

1、1.索引器 索引器就是一类特殊的属性,通过它们你就可以像引用数组一样访问对象元素的功能。显然,这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件或者抽象某些有限资源等,能让类具有类似数组的行为当然也是非常有用的。本文就会引领你设置类来采用索引器。 索引器的定义如下所示: 修饰符 数据类型 this int index 访问函数体代码 自己写的一个类的简单的索引:namespace InterfaceTest class indexx public string nn = ; public int dd= 0; public indexx(string name, int

2、id) nn=name; dd = id; class sindex private indexx Try = new indexx10; public indexx thisint index get if (index = 0 | index = 0 | index 10) Tryindex = value; public class app public static void Main() sindex sin = new sindex(); sin1 = new indexx(qym, 4312); Console.WriteLine(name is 0 and id is 1, s

3、in1.nn, sin1.dd); Console.ReadLine(); 2.重载运算符重载运算符的格式为:Public static 返回类型 operator 运算符(参数表)示例代码:namespace InterfaceTest public class Reload public int x, y; public Reload() x = y = 0; public Reload(int i,int j) this.x = i; this.y = j; public static Reload operator +(Reload rr, Reload dd) Reload nn =

4、 new Reload(); nn.x = rr.x + dd.x; nn.y = rr.y + dd.y; return nn; public static Reload operator +(Reload rr, int d) Reload nn = new Reload(); nn.x = rr.x + d; nn.y = rr.y + d; return nn; public class Run public static void Main() Reload xx = new Reload(23, 43); Reload yy = new Reload(); int g = 10;

5、/ Reload uu = new Reload(); / Reload ii = new Reload(); Reload uu = xx + yy; Reload ii = xx + g; Console.WriteLine(xx+yys x=0 and y=1, uu.x, uu.y); Console.WriteLine(xx+gs x=0 and y=1, ii.x, ii.y); Console.ReadLine(); 3.多态性 C#的多态性是指当对不同类的对象执行相同的方法时,系统能根据不同类的对象正确辨别调用各对象所属类的相应方法,从而产生不同的结果。 多态性是通过”虚方法重

6、载”来实现在程序中调用相应对象所属类中的方法,而不是调用基类的方法。虚方法重载就是将基类的某个方法在其派生类中重新定义,而方法名和方法的参数都不改变。虚方法即加修饰符virtual,派生类中用override进行覆盖。 非虚方法真正执行的功能是编译时的对象所属的类中的方法;虚方法面向运行时它实际所属的类的对象,也即虚方法真正执行的功能是运行时的实际对象所属的类中的方法。 New关键字用于派生类对象希望调用派生类中定义的与其基类同名,但作用效果不同的方法;虚方法用于自动实现派生类对象调用自己类中的与基类同名、但作用效果不同的方法。 示例代码:namespace InterfaceTest cla

7、ss VirtualTest public class Employeee public string name; public long idcard; public double salary; public double increase; public Employeee(string n, long i, double m) name = n; idcard = i; salary = m; public void Print() Console.WriteLine(姓名:0, name); Console.WriteLine(身份证号:0, idcard); Console.Wri

8、teLine(基本工资:0, salary); Console.WriteLine(增加工资额:0, increase); public virtual void Raise(double percent) increase = percent * salary; public class WorkEmp : Employeee public WorkEmp(string i, long j, double k) : base(i, j, k) public override void Raise(double percent) Console.WriteLine(n普通员工工资增加数额计算:

9、); base.increase = base.salary * percent; public class BachelorEmp : Employeee public BachelorEmp(string i, long j, double k) : base(i, j, k) public override void Raise(double percent) Console.WriteLine(n本科员工工资增加数额计算:); base.Raise(percent); base.increase = base.increase*2; public class MasterEmp : E

10、mployeee public MasterEmp(string i, long j, double k) : base(i, j, k) public override void Raise(double percent) Console.WriteLine(n硕士员工工资增加数额计算:); base.Raise(percent); base.increase = base.increase * 3; public class Application public static void Main() Employeee jia = new Employeee(sasa,0,0); WorkEmp aa = new WorkEmp(qym, 54371221, 2000); jia = aa; jia.Raise(0.05); jia.Print(); BachelorEmp bb = new BachelorEmp(qym, 54371221, 2000); jia = bb; jia.Raise(0.05);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 文学/艺术/历史 > 人文/社科

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号