没有合适的资源?快使用搜索试试~ 我知道了~
首页C#高级编程学习笔记和代码.docx
资源详情
资源评论
资源推荐
1.索引器
索引器就是一类特殊的属性,通过它们你就可以像引用数组一样访问对象元素的功能。
显然,这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件
或者抽象某些有限资源等,能让类具有类似数组的行为当然也是非常有用的。本文就会引
领你设置类来采用索引器。
索引器的定义如下所示:
[修饰符] 数据类型 this [int index] {访问函数体代码}
自己写的一个类的简单的索引:
namespace InterfaceTest
{
class indexx
{
public string nn = "";
public int dd= 0;
public indexx(string name, int id)
{
nn=name;
dd = id;
}
}
class sindex
{
private indexx[] Try = new indexx[10];
public indexx this[int index]
{
get
{
if (index >= 0 || index < 10)
return Try[index];
else return null;
}
set
{
if (index >= 0 || index < 10)
Try[index] = value;
}
}
}
public class app
{
public static void Main()
{
sindex sin = new sindex();
sin[1] = new indexx("qym", 4312);
Console.WriteLine("name is {0} and id is {1}", sin[1].nn, sin[1].dd);
Console.ReadLine();
}
}
}
2.重载运算符
重载运算符的格式为:
Public stac 返回类型 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 = 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;
// Reload uu = new Reload();
// Reload ii = new Reload();
Reload uu = xx + yy;
Reload ii = xx + g;
Console.WriteLine("xx+yy's x={0} and y={1}", uu.x, uu.y);
Console.WriteLine("xx+g's x={0} and y={1}", ii.x, ii.y);
Console.ReadLine();
}
}
}
3.多态性
C#的多态性是指当对不同类的对象执行相同的方法时,系统能根据不同类的对象正确辨
别调用各对象所属类的相应方法,从而产生不同的结果。
多态性是通过”虚方法重载”来实现在程序中调用相应对象所属类中的方法,而不是调用
基类的方法。虚方法重载就是将基类的某个方法在其派生类中重新定义,而方法名和方法
的参数都不改变。虚方法即加修饰符 virtual,派生类中用 override 进行覆盖。
非虚方法真正执行的功能是编译时的对象所属的类中的方法;虚方法面向运行时它实际
所属的类的对象,也即虚方法真正执行的功能是运行时的实际对象所属的类中的方法。
New 关键字用于派生类对象希望调用派生类中定义的与其基类同名,但作用效果不同的
方法;虚方法用于自动实现派生类对象调用自己类中的与基类同名、但作用效果不同的方
法。
示例代码:
namespace InterfaceTest
{
class 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.WriteLine("基本工资:{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 普通员工工资增加数额计算:");
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 : Employeee
{
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);
jia.Print();
MasterEmp cc = new MasterEmp("qym", 54371221, 2000);
jia = cc;
jia.Raise(0.05);
jia.Print();
Console.ReadLine();
}
}
}
剩余28页未读,继续阅读
老魔_Lomo
- 粉丝: 2
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 27页智慧街道信息化建设综合解决方案.pptx
- 计算机二级Ms-Office选择题汇总.doc
- 单链表的插入和删除实验报告 (2).docx
- 单链表的插入和删除实验报告.pdf
- 物联网智能终端项目设备管理方案.pdf
- 如何打造品牌的模式.doc
- 样式控制与页面布局.pdf
- 武汉理工Java实验报告(二).docx
- 2021线上新品消费趋势报告.pdf
- 第3章 Matlab中的矩阵及其运算.docx
- 基于Web的人力资源管理系统的必要性和可行性.doc
- 基于一阶倒立摆的matlab仿真实验.doc
- 速运公司物流管理模式研究教材
- 大数据与管理.pptx
- 单片机课程设计之步进电机.doc
- 大数据与数据挖掘.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1