C#学习笔记(十六):索引器和重载运算符


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



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace m1w4d2_indexes
{
#region 索引器-李索
//索引器可以让我们通过不同的索引号返回对应类型的多个属性
//索引器适用于除自动属性以外的所有属性特性
//索引号可以是任意类型
//索引器在通过类对象来访问和赋值 变量(类对象)[索引号]
//访问修饰 返回类型 this[索引号,索引号,索引号,索引号......]
//{ get {return} set {value} }
//用索引器访问一个怪物类中的不同字段
class Monster
{
public Monster(int attack, int defend, int health, int speed, string name)
{
this.attack = attack;
this.defend = defend;
this.health = health;
this.speed = speed;
this.name = name;
}
int attack;
int defend;
int health;
int speed;
string name;
//public string this[int indexa, int indexb]
//{
// get { return ""; }
// set { }
//}
public string this[int index]
{
get
{
string str = "";
switch (index)
{
case : str = attack.ToString(); break;
case : str = defend.ToString(); break;
case : str = health.ToString(); break;
case : str = speed.ToString(); break;
case : str = name; break;
}
return str;
}
set
{
switch (index)
{
case : attack = int.Parse(value); break;
case : defend = int.Parse(value); break;
case : health = int.Parse(value); break;
case : speed = int.Parse(value); break;
case : name = value; break;
}
}
}
}
#endregion
#region 多维度的索引号
//索引号可以任意维度
//用一个MyArray类去模拟一个二维数组
class MyArray
{
public MyArray(int x, int y)
{
//这个放元素的一维数组多长
//array = new int[10];
array = new int[x * y];//一维数组的长度
Length = new int[];
Length[] = x;
Length[] = y;
}
int[] Length;
int[] array;
public int GetLength(int index)
{
if (index > Length.Length)
{
throw new IndexOutOfRangeException();//抛异常
}
return Length[index];
}
public int this[int x, int y]
{
get
{
//我给你一个二维的下标,你如何映射到一维数组上
return array[x + y * GetLength()];
}
set
{
array[x + y * GetLength()] = value;
}
}
}
#endregion
#region 索引器-沈军
class Unity1803
{
// C# 单例模式 (单独的实例)
private static Unity1803 instance = null;
public static Unity1803 Instance
{
get
{
if (instance == null) instance = new Unity1803();
return instance;
}
}
//静态构造函数什么时候调用
//当我们访问静态成员的时候,先调用且只调用一次
//当我们创建对象的时候,先调用且只调用一次
//可以对静态字段做初始化使用的,静态的成员才会加载到内存中
//static Unity1803()//静态构造函数,不能有访问修饰符和参数
//{
// Console.WriteLine("调用静态构造函数");
//}
public int Count { private set; get; } //
Student[] students = new Student[]; // null
private Unity1803()
{
students[] = new Student("蔡浩", , );
Count++;
students[] = new Student("江灿荣", , );
Count++;
students[] = new Student("贺益民", , );
Count++;
}
public Student this[int index]
{
get
{
return students[index];
}
set
{
if (students[index] == null) Count++;
students[index] = value;
}
}
public Student this[string name]
{
get
{
for (int i = ; i < Count; i++)
{
if (students[i].Name == name)
{
return students[i];
}
}
return null;
}
set
{
students[Count] = new Student(name, , );
}
}
}
class Student
{
public string Name { get; set; }
public int Age { get; set; }
public int CSharpScore { get; set; }
public Student(string name, int age, int score)
{
this.Name = name;
this.Age = age;
this.CSharpScore = score;
}
public override string ToString()
{
return "Name:" + Name + ", Age :" + Age + ", Score :" + CSharpScore;
}
public static bool operator >(Student lfs, int score)
{
return lfs.CSharpScore > score;
}
public static bool operator <(Student lfs, int score)
{
return lfs.CSharpScore < score;
}
public static bool operator true(Student s)
{
return s.CSharpScore >= ;
}
public static bool operator false(Student s)
{
return s.CSharpScore < ;
}
// 隐式类型转换 把字符串隐式的转换成Student类型
public static implicit operator Student(string name)
{
Student s = new Student(name, , );
return s;
}
// 显式类型转换 把Student类型强转成字符串
public static explicit operator string(Student s)
{
return s.Name;
}
}
#endregion
class Program
{
static void Main(string[] args)
{
#region 索引器-李索
Monster monster = new Monster(, , , , "哥布林");
for (int i = ; i < ; i++)
{
Console.WriteLine(monster[i]);
}
Console.WriteLine();
//把一个策划文档的数据切割成对应的字符数组
//序列化
string dataTitle = "attack,defend,health,speed,name";
string data = "100,15,500,1,蠕虫皇后";
string[] datas = data.Split(',');
for (int i = ; i < ; i++)
{
monster[i] = datas[i];
}
for (int i = ; i < ; i++)
{
Console.WriteLine(monster[i]);
}
#endregion
#region 多维度的索引号
//索引号可以任意维度
//用一个MyArray类去模拟一个二维数组
MyArray myArray = new MyArray(, );
for (int x = ; x < myArray.GetLength(); x++)
{
for (int y = ; y < myArray.GetLength(); y++)
{
myArray[x, y] = x * + y;
}
}
for (int y = ; y < myArray.GetLength(); y++)
{
for (int x = ; x < myArray.GetLength(); x++)
{
Console.Write(myArray[x, y] + "\t");
}
Console.WriteLine();
}
#endregion
#region 索引器-沈军
// 属性 操作字段
// 索引器 对于操作集合的一种封装
//Unity1803 unity1803 = new Unity1803();
//unity1803[2] = new Student("沈天宇", 20, 95);
for (int i = ; i < Unity1803.Instance.Count; i++)
{
if (Unity1803.Instance[i])
{
Console.WriteLine(Unity1803.Instance[i]); // .ToString()方法
}
}
Unity1803.Instance[Unity1803.Instance.Count] = "丁超"; // 隐式类型转换
string name = (string)Unity1803.Instance[]; // 显式类型转换
//Console.WriteLine(unity1803["沈天宇"]);
//Unity1803 _unity1803 = new Unity1803();
//Console.WriteLine(Unity1803.Count);
//Unity1803.instance = new Unity1803(); // 只读的
//Unity1803 u1 = new Unity1803(); // 不允许的
//Console.WriteLine(Unity1803.Instance[0]);
// 隐式转换 显式转换
int num1 = ;
byte num2 = ;
num1 = num2; // 隐式类型转换
num2 = (byte)num1; // 显式类型转换
#endregion
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace m1w4d2_operator
{
#region 练习1
//通过重载运算符,我们可以给自定类型,增加计算功能
struct Point2D
{
public int x, y;
public Point2D(int x, int y)
{
this.x = x;
this.y = y;
}
public static Point2D Up = new Point2D(, -);
public static Point2D Down = new Point2D(, );
public static Point2D Left = new Point2D(-, );
public static Point2D Right = new Point2D(, );
//返回类型?,函数名(operator 运算符)?参数?
//public static i = i + 1;
//两个参数,任意类型
//返回类型,任意类型
//算数运算符 是双目运算符,参数必须是两个,任意类型,返回类型,任意类型
public static Point2D operator +(Point2D a, Point2D b)
{
return new Point2D(a.x + b.x, a.y + b.y);
}
public static Point2D operator -(Point2D a, Point2D b)
{
return new Point2D(a.x - b.x, a.y - b.y);
}
//关系运算符 是双目运算符,参数必须是两个,任意类型,返回类型是bool
public static bool operator ==(Point2D a, Point2D b)
{
return a.x == b.x && a.y == b.y;
}
public static bool operator !=(Point2D a, Point2D b)
{
return !(a == b);
}
public override string ToString()
{
return string.Format("[x:{0},y{1}]", x, y);
}
}
#endregion
#region 练习2
class Item
{
public static string style = "剑刀盾斧药";
public Item(string name, int value)
{
this.name = name;
this.value = value;
}
string name;
int value;
public override string ToString()
{
return string.Format("{0}:value:{1}", name, value);
}
}
class Monster
{
public static Random roll = new Random();
public int attack;
public string name;
public int speed;
public Monster(int attack, int speed, string name)
{
this.attack = attack;
this.name = name;
this.speed = speed;
}
public static Item operator +(Monster a, Monster b)
{
string name = a.name.Substring(a.name.Length / ) + b.name.Substring(b.name.Length / ) + Item.style[roll.Next(, Item.style.Length)];
int value = (a.attack + b.attack) * + a.speed + b.speed;
if (name[name.Length - ] == '屎')
{
value /= ;
}
return new Item(name, value);
}
public override string ToString()
{
return string.Format("{0}:attack:{1},speed:{2}", name, attack, speed);
}
}
#endregion
class Program
{
static void Main(string[] args)
{
#region 练习1
//老位置
Point2D oldPosition = new Point2D(, );
//position.x += dir.x;
//新位置 是老位置 向上走一步
Console.WriteLine(oldPosition);
Point2D newPosition = oldPosition + Point2D.Up;
Console.WriteLine(newPosition);
//请问,新位置在老位置的哪个方向
string dirStr = "";
Point2D pointDir = newPosition - oldPosition;
if (pointDir == Point2D.Up) dirStr = "上";
else if (pointDir == Point2D.Down) dirStr = "下";
else if (pointDir == Point2D.Left) dirStr = "左";
else if (pointDir == Point2D.Right) dirStr = "右";
Console.WriteLine("你的前进方向是{0}", dirStr);
#endregion
#region 练习2
Monster a = new Monster(, , "哥布林");
Monster b = new Monster(, , "索尼克");
Console.WriteLine(a);
Console.WriteLine(b);
Item item = a + b;
Console.WriteLine(item);
#endregion
}
}
}
C#学习笔记(十六):索引器和重载运算符的更多相关文章
- python3.4学习笔记(十六) windows下面安装easy_install和pip教程
python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...
- (C/C++学习笔记) 十六. 预处理
十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...
- python 学习笔记十六 django深入学习一 路由系统,模板,admin,数据库操作
django 请求流程图 django 路由系统 在django中我们可以通过定义urls,让不同的url路由到不同的处理函数 from . import views urlpatterns = [ ...
- MySQL学习笔记十六:锁机制
1.数据库锁就是为了保证数据库数据的一致性在一个共享资源被并发访问时使得数据访问顺序化的机制.MySQL数据库的锁机制比较独特,支持不同的存储引擎使用不同的锁机制. 2.MySQL使用了三种类型的锁机 ...
- Struts2学习笔记(十)——自定义拦截器
Struts2拦截器是基于AOP思想实现的,而AOP的实现是基于动态代理.Struts2拦截器会在访问某个Action之前或者之后进行拦截,并且Struts2拦截器是可插拔的:Struts2拦截器栈就 ...
- Java基础学习笔记十六 集合框架(二)
List List接口的特点: 它是一个元素存取有序的集合.例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的. 它是一个带有索引的集合,通过索引就可以精 ...
- 文件的上传Commons FileUpload(web基础学习笔记十六)
一.表单设置 <form action="<%=request.getContextPath()%>/jsp/admin/doAdd.jsp" enctype=& ...
- JavaScript权威设计--CSS(简要学习笔记十六)
1.Document的一些特殊属性 document.lastModified document.URL document.title document.referrer document.domai ...
- SharpGL学习笔记(十六) 多重纹理映射
多重纹理就把多张贴图隔和在一起.比如下面示例中,一个表现砖墙的纹理,配合一个表现聚光灯效果的灰度图,就形成了砖墙被一个聚光灯照亮的效果,这便是所谓的光照贴图技术. 多重纹理只在OpenGL扩展库中才提 ...
随机推荐
- Jsoup爬虫解析
需要下载jsoup-1.8.1.jar包 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQue ...
- python 基础 列表生成式 生成器
列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式 举个例子,要生成list [1, 2, 3, 4, 5, 6, 7, ...
- 为什么*p++等于*(p++)?
你要先搞懂i++与++i的区别.i++是先赋值再自增,对于指针也是一样的.所以*p++是先取值,然后p再自增.加个括号还是一样的,*(p++)括号里面的内容还是p++,所以还是要先取值然后p再自增. ...
- [py]django强悍的数据库接口(QuerySet API)-增删改查
django强悍的数据库接口(QuerySet API) 4种方法插入数据 获取某个对象 filter过滤符合条件的对象 filter过滤排除某条件的对象- 支持链式多重查询 没找到排序的 - 4种方 ...
- [py]一些搜集到的问题
过滤爬虫爬取下来的关键字 v1,来不及了,先上车 content = ['哈士奇', '二哈', '哈士奇图片','哈士奇图片', '哈士奇美丽价格', '哈士奇是个大傻逼', '猫咪图片', '猫咪 ...
- [wx]自然数学规律
有趣的数学规律 椭圆 双曲线 抛物线都叫圆锥曲线 它们跟圆锥有着怎样的关系? 他们都是圆锥与平面在不同姿势下交配的产物. 参考 椭圆 抛物线 小结 e: 离线率 P: 任意一点 F: 焦点 准线: 一 ...
- selenium webdriver 截屏操作
有时候我们需要进行截屏操作,特别是遇到一些比较重要的页面信息(出现错误)或者出现不同需要进行对比时, 我们就需要对正在处理的页面进行截屏! 未经作者允许,禁止转载! package test_wait ...
- Lucene简介和创建索引初步
Lucene的使用 在全文索引工具中,都是由这样三部分组成 1:索引部分 2:分词部分 3:搜索部分
- distinct count
实验:查询一个column的无重复记录,需要知道有多少条记录,并显示记录. 统计记录用count(*)函数,无重复记录distinct,以emp表为例. (1)先查询无重复记录 [@more@] SQ ...
- c语言的字符串拷贝函数的精简
#include <stdio.h>#include <string.h>void str_cpy(char * to, char *from){ while ((*to ...