二维数组如何映射到一维数组

重载运算符
1、算术运算符
2、关系运算符, < 和 > 成对重载

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace m1w4d2_indexes
  7. {
  8. #region 索引器-李索
  9. //索引器可以让我们通过不同的索引号返回对应类型的多个属性
  10. //索引器适用于除自动属性以外的所有属性特性
  11. //索引号可以是任意类型
  12. //索引器在通过类对象来访问和赋值 变量(类对象)[索引号]
  13. //访问修饰 返回类型 this[索引号,索引号,索引号,索引号......]
  14. //{ get {return} set {value} }
  15. //用索引器访问一个怪物类中的不同字段
  16. class Monster
  17. {
  18. public Monster(int attack, int defend, int health, int speed, string name)
  19. {
  20. this.attack = attack;
  21. this.defend = defend;
  22. this.health = health;
  23. this.speed = speed;
  24. this.name = name;
  25. }
  26. int attack;
  27. int defend;
  28. int health;
  29. int speed;
  30. string name;
  31. //public string this[int indexa, int indexb]
  32. //{
  33. // get { return ""; }
  34. // set { }
  35. //}
  36. public string this[int index]
  37. {
  38. get
  39. {
  40. string str = "";
  41. switch (index)
  42. {
  43. case : str = attack.ToString(); break;
  44. case : str = defend.ToString(); break;
  45. case : str = health.ToString(); break;
  46. case : str = speed.ToString(); break;
  47. case : str = name; break;
  48. }
  49. return str;
  50. }
  51. set
  52. {
  53. switch (index)
  54. {
  55. case : attack = int.Parse(value); break;
  56. case : defend = int.Parse(value); break;
  57. case : health = int.Parse(value); break;
  58. case : speed = int.Parse(value); break;
  59. case : name = value; break;
  60. }
  61. }
  62. }
  63. }
  64. #endregion
  65. #region 多维度的索引号
  66. //索引号可以任意维度
  67. //用一个MyArray类去模拟一个二维数组
  68. class MyArray
  69. {
  70. public MyArray(int x, int y)
  71. {
  72. //这个放元素的一维数组多长
  73. //array = new int[10];
  74. array = new int[x * y];//一维数组的长度
  75. Length = new int[];
  76. Length[] = x;
  77. Length[] = y;
  78. }
  79. int[] Length;
  80. int[] array;
  81. public int GetLength(int index)
  82. {
  83. if (index > Length.Length)
  84. {
  85. throw new IndexOutOfRangeException();//抛异常
  86. }
  87. return Length[index];
  88. }
  89. public int this[int x, int y]
  90. {
  91. get
  92. {
  93. //我给你一个二维的下标,你如何映射到一维数组上
  94. return array[x + y * GetLength()];
  95. }
  96. set
  97. {
  98. array[x + y * GetLength()] = value;
  99. }
  100. }
  101. }
  102. #endregion
  103. #region 索引器-沈军
  104. class Unity1803
  105. {
  106. // C# 单例模式 (单独的实例)
  107. private static Unity1803 instance = null;
  108. public static Unity1803 Instance
  109. {
  110. get
  111. {
  112. if (instance == null) instance = new Unity1803();
  113. return instance;
  114. }
  115. }
  116. //静态构造函数什么时候调用
  117. //当我们访问静态成员的时候,先调用且只调用一次
  118. //当我们创建对象的时候,先调用且只调用一次
  119. //可以对静态字段做初始化使用的,静态的成员才会加载到内存中
  120. //static Unity1803()//静态构造函数,不能有访问修饰符和参数
  121. //{
  122. // Console.WriteLine("调用静态构造函数");
  123. //}
  124. public int Count { private set; get; } //
  125. Student[] students = new Student[]; // null
  126. private Unity1803()
  127. {
  128. students[] = new Student("蔡浩", , );
  129. Count++;
  130. students[] = new Student("江灿荣", , );
  131. Count++;
  132. students[] = new Student("贺益民", , );
  133. Count++;
  134. }
  135. public Student this[int index]
  136. {
  137. get
  138. {
  139. return students[index];
  140. }
  141. set
  142. {
  143. if (students[index] == null) Count++;
  144. students[index] = value;
  145. }
  146. }
  147. public Student this[string name]
  148. {
  149. get
  150. {
  151. for (int i = ; i < Count; i++)
  152. {
  153. if (students[i].Name == name)
  154. {
  155. return students[i];
  156. }
  157. }
  158. return null;
  159. }
  160. set
  161. {
  162. students[Count] = new Student(name, , );
  163. }
  164. }
  165. }
  166. class Student
  167. {
  168. public string Name { get; set; }
  169. public int Age { get; set; }
  170. public int CSharpScore { get; set; }
  171. public Student(string name, int age, int score)
  172. {
  173. this.Name = name;
  174. this.Age = age;
  175. this.CSharpScore = score;
  176. }
  177. public override string ToString()
  178. {
  179. return "Name:" + Name + ", Age :" + Age + ", Score :" + CSharpScore;
  180. }
  181. public static bool operator >(Student lfs, int score)
  182. {
  183. return lfs.CSharpScore > score;
  184. }
  185. public static bool operator <(Student lfs, int score)
  186. {
  187. return lfs.CSharpScore < score;
  188. }
  189. public static bool operator true(Student s)
  190. {
  191. return s.CSharpScore >= ;
  192. }
  193. public static bool operator false(Student s)
  194. {
  195. return s.CSharpScore < ;
  196. }
  197. // 隐式类型转换 把字符串隐式的转换成Student类型
  198. public static implicit operator Student(string name)
  199. {
  200. Student s = new Student(name, , );
  201. return s;
  202. }
  203. // 显式类型转换 把Student类型强转成字符串
  204. public static explicit operator string(Student s)
  205. {
  206. return s.Name;
  207. }
  208. }
  209. #endregion
  210. class Program
  211. {
  212. static void Main(string[] args)
  213. {
  214. #region 索引器-李索
  215. Monster monster = new Monster(, , , , "哥布林");
  216. for (int i = ; i < ; i++)
  217. {
  218. Console.WriteLine(monster[i]);
  219. }
  220. Console.WriteLine();
  221. //把一个策划文档的数据切割成对应的字符数组
  222. //序列化
  223. string dataTitle = "attack,defend,health,speed,name";
  224. string data = "100,15,500,1,蠕虫皇后";
  225. string[] datas = data.Split(',');
  226. for (int i = ; i < ; i++)
  227. {
  228. monster[i] = datas[i];
  229. }
  230. for (int i = ; i < ; i++)
  231. {
  232. Console.WriteLine(monster[i]);
  233. }
  234. #endregion
  235. #region 多维度的索引号
  236. //索引号可以任意维度
  237. //用一个MyArray类去模拟一个二维数组
  238. MyArray myArray = new MyArray(, );
  239. for (int x = ; x < myArray.GetLength(); x++)
  240. {
  241. for (int y = ; y < myArray.GetLength(); y++)
  242. {
  243. myArray[x, y] = x * + y;
  244. }
  245. }
  246. for (int y = ; y < myArray.GetLength(); y++)
  247. {
  248. for (int x = ; x < myArray.GetLength(); x++)
  249. {
  250. Console.Write(myArray[x, y] + "\t");
  251. }
  252. Console.WriteLine();
  253. }
  254. #endregion
  255. #region 索引器-沈军
  256. // 属性 操作字段
  257. // 索引器 对于操作集合的一种封装
  258. //Unity1803 unity1803 = new Unity1803();
  259. //unity1803[2] = new Student("沈天宇", 20, 95);
  260. for (int i = ; i < Unity1803.Instance.Count; i++)
  261. {
  262. if (Unity1803.Instance[i])
  263. {
  264. Console.WriteLine(Unity1803.Instance[i]); // .ToString()方法
  265. }
  266. }
  267. Unity1803.Instance[Unity1803.Instance.Count] = "丁超"; // 隐式类型转换
  268. string name = (string)Unity1803.Instance[]; // 显式类型转换
  269. //Console.WriteLine(unity1803["沈天宇"]);
  270. //Unity1803 _unity1803 = new Unity1803();
  271. //Console.WriteLine(Unity1803.Count);
  272. //Unity1803.instance = new Unity1803(); // 只读的
  273. //Unity1803 u1 = new Unity1803(); // 不允许的
  274. //Console.WriteLine(Unity1803.Instance[0]);
  275. // 隐式转换 显式转换
  276. int num1 = ;
  277. byte num2 = ;
  278. num1 = num2; // 隐式类型转换
  279. num2 = (byte)num1; // 显式类型转换
  280. #endregion
  281. }
  282. }
  283. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace m1w4d2_operator
  7. {
  8. #region 练习1
  9. //通过重载运算符,我们可以给自定类型,增加计算功能
  10. struct Point2D
  11. {
  12. public int x, y;
  13. public Point2D(int x, int y)
  14. {
  15. this.x = x;
  16. this.y = y;
  17. }
  18. public static Point2D Up = new Point2D(, -);
  19. public static Point2D Down = new Point2D(, );
  20. public static Point2D Left = new Point2D(-, );
  21. public static Point2D Right = new Point2D(, );
  22. //返回类型?,函数名(operator 运算符)?参数?
  23. //public static i = i + 1;
  24. //两个参数,任意类型
  25. //返回类型,任意类型
  26. //算数运算符 是双目运算符,参数必须是两个,任意类型,返回类型,任意类型
  27. public static Point2D operator +(Point2D a, Point2D b)
  28. {
  29. return new Point2D(a.x + b.x, a.y + b.y);
  30. }
  31. public static Point2D operator -(Point2D a, Point2D b)
  32. {
  33. return new Point2D(a.x - b.x, a.y - b.y);
  34. }
  35. //关系运算符 是双目运算符,参数必须是两个,任意类型,返回类型是bool
  36. public static bool operator ==(Point2D a, Point2D b)
  37. {
  38. return a.x == b.x && a.y == b.y;
  39. }
  40. public static bool operator !=(Point2D a, Point2D b)
  41. {
  42. return !(a == b);
  43. }
  44. public override string ToString()
  45. {
  46. return string.Format("[x:{0},y{1}]", x, y);
  47. }
  48. }
  49. #endregion
  50. #region 练习2
  51. class Item
  52. {
  53. public static string style = "剑刀盾斧药";
  54. public Item(string name, int value)
  55. {
  56. this.name = name;
  57. this.value = value;
  58. }
  59. string name;
  60. int value;
  61. public override string ToString()
  62. {
  63. return string.Format("{0}:value:{1}", name, value);
  64. }
  65. }
  66. class Monster
  67. {
  68. public static Random roll = new Random();
  69. public int attack;
  70. public string name;
  71. public int speed;
  72. public Monster(int attack, int speed, string name)
  73. {
  74. this.attack = attack;
  75. this.name = name;
  76. this.speed = speed;
  77. }
  78. public static Item operator +(Monster a, Monster b)
  79. {
  80. string name = a.name.Substring(a.name.Length / ) + b.name.Substring(b.name.Length / ) + Item.style[roll.Next(, Item.style.Length)];
  81. int value = (a.attack + b.attack) * + a.speed + b.speed;
  82. if (name[name.Length - ] == '屎')
  83. {
  84. value /= ;
  85. }
  86. return new Item(name, value);
  87. }
  88. public override string ToString()
  89. {
  90. return string.Format("{0}:attack:{1},speed:{2}", name, attack, speed);
  91. }
  92. }
  93. #endregion
  94. class Program
  95. {
  96. static void Main(string[] args)
  97. {
  98. #region 练习1
  99. //老位置
  100. Point2D oldPosition = new Point2D(, );
  101. //position.x += dir.x;
  102. //新位置 是老位置 向上走一步
  103. Console.WriteLine(oldPosition);
  104. Point2D newPosition = oldPosition + Point2D.Up;
  105. Console.WriteLine(newPosition);
  106. //请问,新位置在老位置的哪个方向
  107. string dirStr = "";
  108. Point2D pointDir = newPosition - oldPosition;
  109. if (pointDir == Point2D.Up) dirStr = "上";
  110. else if (pointDir == Point2D.Down) dirStr = "下";
  111. else if (pointDir == Point2D.Left) dirStr = "左";
  112. else if (pointDir == Point2D.Right) dirStr = "右";
  113. Console.WriteLine("你的前进方向是{0}", dirStr);
  114. #endregion
  115. #region 练习2
  116. Monster a = new Monster(, , "哥布林");
  117. Monster b = new Monster(, , "索尼克");
  118. Console.WriteLine(a);
  119. Console.WriteLine(b);
  120. Item item = a + b;
  121. Console.WriteLine(item);
  122. #endregion
  123. }
  124. }
  125. }

C#学习笔记(十六):索引器和重载运算符的更多相关文章

  1. python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

  2. (C/C++学习笔记) 十六. 预处理

    十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...

  3. python 学习笔记十六 django深入学习一 路由系统,模板,admin,数据库操作

    django 请求流程图 django 路由系统 在django中我们可以通过定义urls,让不同的url路由到不同的处理函数 from . import views urlpatterns = [ ...

  4. MySQL学习笔记十六:锁机制

    1.数据库锁就是为了保证数据库数据的一致性在一个共享资源被并发访问时使得数据访问顺序化的机制.MySQL数据库的锁机制比较独特,支持不同的存储引擎使用不同的锁机制. 2.MySQL使用了三种类型的锁机 ...

  5. Struts2学习笔记(十)——自定义拦截器

    Struts2拦截器是基于AOP思想实现的,而AOP的实现是基于动态代理.Struts2拦截器会在访问某个Action之前或者之后进行拦截,并且Struts2拦截器是可插拔的:Struts2拦截器栈就 ...

  6. Java基础学习笔记十六 集合框架(二)

    List List接口的特点: 它是一个元素存取有序的集合.例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的. 它是一个带有索引的集合,通过索引就可以精 ...

  7. 文件的上传Commons FileUpload(web基础学习笔记十六)

    一.表单设置 <form action="<%=request.getContextPath()%>/jsp/admin/doAdd.jsp" enctype=& ...

  8. JavaScript权威设计--CSS(简要学习笔记十六)

    1.Document的一些特殊属性 document.lastModified document.URL document.title document.referrer document.domai ...

  9. SharpGL学习笔记(十六) 多重纹理映射

    多重纹理就把多张贴图隔和在一起.比如下面示例中,一个表现砖墙的纹理,配合一个表现聚光灯效果的灰度图,就形成了砖墙被一个聚光灯照亮的效果,这便是所谓的光照贴图技术. 多重纹理只在OpenGL扩展库中才提 ...

随机推荐

  1. BUG笔记:Android原生浏览器不认负百分数margin致Foundation Orbit往右滑动动画出错

    一看这标题就知道无比蛋疼了是不?至少我从来不用安卓自带的浏览器... 发现这个bug的场景:万恶的Foundation,它的滚动图片插件Orbit在安卓自带浏览器下手指从左往右滑动时动画仍旧表现为从右 ...

  2. mysql 用户与权限

    1.用户 1)创建用户   "create user '用户'@'host' identified by '密码';" 在5.7以后的版本中要求密码包含至少一个大写字母,一个小写字 ...

  3. 网络密钥交换协议——Diffie-Hellman

    Diffie-Hellman算法是一种交换密钥的算法. 它是眼下比較经常使用的密钥交换算法. 这样的算法的优点是能让两台计算机在不安全的网络环境中完毕密钥的交换. 下面是整个算法的过程.当中红色字体表 ...

  4. OC convertRect

    举个例子: redView = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; redView.backgroundColor ...

  5. 一起做RGB-D SLAM (2)

    第二讲 从图像到点云 本讲中,我们将带领读者,编写一个将图像转换为点云的程序.该程序是后期处理地图的基础.最简单的点云地图即是把不同位置的点云进行拼接得到的. 当我们使用RGB-D相机时,会从相机里读 ...

  6. list的*运算使用过程中遇到的问题

    目的: 想生成一个[[],[],[]] 这样的列表, 所以就 [[]]*3 这样做了,但是这样做会有问题,这样list中的三个list其实是同一个list. 例如:a=[[]]*3,然后a[0].ap ...

  7. SpringMyBatisDay02

    全篇概述:IOC    DI     参数值注入      注解依赖注入 1.Spring IOCIOC 全称Inversion Of Control,被翻译成控制反转控制反转指:程序中对象的获取方式 ...

  8. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  9. VS2010/MFC编程入门之五十一(图形图像:GDI对象之画刷CBrush)

    上一节中鸡啄米主要讲的是画笔CPen的用法,前面也说了,GDI对象中最常用的就是画笔和画刷,本节就讲讲画刷CBrush. 鸡啄米依然是通过实例的方式来说明画刷的用法.此实例要实现的功能是,对话框上有一 ...

  10. form表单提交 type="submit"

    <form action=""  method="post" onsubmit="return validte()"> < ...