《精通C#》索引器与重载操作符(11.1-11.2)
1.索引器方法结构大致为<modifier><return type> this [argument list],它可以在接口中定义: 在为接口声明索引器的时候,记住声明只是表示索引器的存在。你只需要提供恰当的访问函数即可,不必包括范围修饰符。以下代码把索引器声明为接口IImplementMe的一部分:
interface IImplementMe { string this[int index] { get; set; }
相应实现的类则必须为IimplementMe的索引器具体定义get和set访问函数。
索引器可以称之为有参数的属性,它与属性的区别在于属性名称不能相同,不可重载,索引器可以。属性可以使static的,但是索引器必须是实例化的。在我看来索引器的作用在于可以改变现有的一些索引器的索引方式,比如数组的所以方式是以int作为下标,查询相应数据,但是我可以重写索引,使数组的索引以string进行。
例:
namespace Study
{
class Program
{
static void Main(string[] args)
{
ScoreIndex s = new ScoreIndex();
s["张三", 1] = 90;
s["张三", 2] = 100;
s["张三", 3] = 80;
s["李四", 1] = 60;
s["李四", 2] = 70;
s["李四", 3] = 50;
Console.WriteLine("张三课程编号为1的成绩为:" + s["张三",1]);
Console.WriteLine("张三的所有成绩为:");
ArrayList temp;
temp = s["张三"];
foreach (IndexClass b in temp)
{
Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score);
}
Console.ReadKey();
}
}
class IndexClass
{
private string _name;
private int _courseid;
private int _score;
public IndexClass(string _name, int _courseid, int _score)
{
this._name = _name;
this._courseid = _courseid;
this._score = _score;
}
public string Name
{
get { return _name; }
set { this._name = value; }
}
public int CourseID
{
get { return _courseid; }
set { this._courseid = value; }
}
public int Score
{
get { return _score; }
set { this._score = value; }
}
}
class ScoreIndex
{
private ArrayList arr;
public ScoreIndex()
{
arr = new ArrayList();
}
public int this[string _name, int _courseid]
{
get
{
foreach (IndexClass a in arr)
{
if (a.Name == _name && a.CourseID == _courseid)
{
return a.Score;
}
}
return -1;
}
set
{
arr.Add(new IndexClass(_name, _courseid, value)); //arr["张三",1]=90
}
}
//重载索引器
public ArrayList this[string _name]
{
get
{
ArrayList temp = new ArrayList();
foreach (IndexClass b in arr)
{
if (b.Name == _name)
{
temp.Add(b);
}
}
return temp;
}
}
}
}
2.操作符重载:
操作符重载是将现有的一些操作符的功能进行改变,例如两个类相加,相减,相比较等,它提供Operaor关键字进行重载,必须与static关键字联合使用。例:
namespace ContainerAndOperator
{
/// <summary>
/// 索引器,此类已成为容器类,索引器大部分使用在容器类中
/// 使用IEnumerable与GetEnumerator()目的在于使这个类可以进行foreach
/// </summary>
public class ContainerClass : IEnumerable
{
private ArrayList people = new ArrayList();
public Person this[int index]//Person为返回的值类型,int为输入的索引值的类型,this用来定义索引器
{
get { return (Person)people[index]; }
set { people.Insert(index, value); }
}
public IEnumerator GetEnumerator()
{
return people.GetEnumerator();
}
}
public class Person
{
public string name { get; set; }
public string Id { get; set; }
public int Age { get; set; }
/// <summary>
/// 重构二元操作符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static Person operator +(Person p1,Person p2)
{
return new Person(p1.name + "-" + p2.name, p1.Id + "-" + p2.Id, p1.Age + p2.Age);
}
public static string operator -(Person p1, Person p2)
{
return p1.name+"-"+p2.Id;
}
public Person(){}
public Person(string n,string i,int a)
{
name = n;
Id = i;
Age = a;
}
}
}
namespace ContainerAndOperator
{
class Program
{
static void Main(string[] args)
{
ContainerClass con = new ContainerClass();
con[0] = new Person("homer", "11", 11);
con[1] = new Person("homer1", "12", 12);
con[2] = new Person("homer2", "13", 13);
Console.WriteLine(con[2].name);
foreach(Person s in con)
{
Console.WriteLine(s.name);
}
Console.WriteLine("*******************************");
Person p3 = con[1] + con[2];
Console.WriteLine(p3.name);
Console.WriteLine(con[1] - con[2]);
Console.ReadLine();
}
}
}
因为有时候我们可能需要对两个类进行对比,已完成一些特定的功能,但是,原生的操作符不支持的情况下,我们就可以重载操作符。
《精通C#》索引器与重载操作符(11.1-11.2)的更多相关文章
- C#学习笔记(十六):索引器和重载运算符
二维数组如何映射到一维数组 重载运算符 1.算术运算符 2.关系运算符, < 和 > 成对重载 using System; using System.Collections.Generic ...
- C#索引器3 重载
7.索引器 重载 public class Demo { private Hashtable name = new Hashtable(); public string this[int index ...
- C#索引器-有参属性
总结 只要类中有类似于属性的元素就应创建索引器,此属性代表的不是一个值,而是值的集合,其中每一个项由一组参数标识. 这些参数可以唯一标识应引用的集合中的项. 索引器延伸了属性的概念,索引器中的一个成员 ...
- C#索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- C# 索引器使用总结
1.索引器(Indexer): 索引器允许类或者结构的实例按照与数组相同的方式进行索引.索引器类似于属性,不同之处在于他们的访问采用参数. 最简单的索引器的使用 /// <summary> ...
- C#索引器一
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- 【C#】索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- C# 索引器 学习
转载原地址: http://www.cnblogs.com/lxblog/p/3940261.html 1.索引器(Indexer): 索引器允许类或者结构的实例按照与数组相同的方式进行索引.索引器类 ...
- C#索引器的作用及使用
1. 作用: 可以使得类和实例能够像数组那样使用一样,又称为带参属性 2. 区分 (1)索引器与数组的比较: 索引器的索引值不受类型限制.用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值 ...
随机推荐
- Js 拖动效果
<!DOCTYPE html> <html> <head> <meta charset="utf8"> <title>j ...
- Python导入Scipy子模块时出错
导入Scipy子模块时报错,出现的问题都是提示 61 from numpy._distributor_init import NUMPY_MKL # requires numpy+mklNo mod ...
- mysql共享表空间转独立表空间
使用innodb_export_import.py脚本: https://github.com/thecpaneladmin/innodb-tools 安装MySQL-python模块: shell ...
- Logstash学习-plugin安装
Usage: bin/logstash-plugin [OPTIONS] SUBCOMMAND [ARG] ... Parameters: SUBCOMMAND subcommand [ARG] .. ...
- 山东省第七届ACM省赛------Julyed
Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...
- js正则表达式
正则表达式分析页面:https://regexper.com/ 可以很清楚的分析正则,加深理解 var reg=/\bis\b/; 'He is a boy.This is a dog.Where i ...
- electron打包发布
1.全局安装electron npm install electron -g 在cmd 直接输入 electron 直接启electron 2.编写第一个Electron应用 在任何地方,建立一个ap ...
- Xcode8 适配iOS10时遇见的一些问题
1.证书管理 用Xcode8打开工程后,比较明显的就是下图了,这个是苹果的新特性,可以帮助我们自动管理证书.建议大家勾选这个Automatically manage signing(Ps.但是在bea ...
- 【转载】我眼中的Oracle性能优化
我眼中的Oracle性能优化 大家对于一个业务系统的运行关心有如下几个方面:功能性.稳定性.效率.安全性.而一个系统的性能有包含了网络性能.应用性能.中间件性能.数据库性能等等. 今天从数据库性能的角 ...
- kali安装java1.7
1.先去这里下载你需要的版本 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 我 ...