工业软件首先要求高可靠性、高可维护性。
作为工业软件的开发者,我们对语言重载的需求是:“不可或缺”。
没有重载几乎就无法开展大规模的工业软件编程项目,因而很难想象怎么用 Go 或 python 或者 javascript 编写高可靠性的应用程序。
而C# 的重载尤其优雅,其中的索引器(this)重载、运算符重载是以科学计算为核心的工程项目中大量使用的程序技术。
请阅读下面 this 的定义:
/// <summary>
/// 数独板(Sudoku Board)信息类
/// </summary>
public class Board
{
/// <summary>
/// 数字总数(N=M*M)
/// </summary>
public int N { get; set; } = 9;
/// <summary>
/// 小格子行列数
/// </summary>
public int M { get; set; } = 3;
/// <summary>
/// 所有节点(保存数字)
/// </summary>
public int[,] nodes { get; set; } = null;
public Board(int n)
{
N = n;
M = (int)Math.Sqrt(N);
nodes = new int[N, N];
Clear();
}
/// <summary>
/// 提取i,j节点的数字
/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
/// <returns></returns>
public int this[int i, int j]
{
set
{
nodes[i, j] = value;
}
get
{
return nodes[i, j];
}
}
/// <summary>
/// 清除(重置)
/// </summary>
public void Clear()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
nodes[i, j] = 0;
}
}
}
}
使用起来就非常舒服了。
Board x = new Board(9);
//获取第一个位置的数字;
int firstNumber = x[0,0];
----------------------------------------------------------------------------------
POWER BY TRUFFER.CN