home commit with new plugins

This commit is contained in:
2026-06-21 09:51:55 +08:00
parent ebb9e385c5
commit 3d067fe75c
85 changed files with 10794 additions and 1443 deletions
+35 -16
View File
@@ -1,36 +1,55 @@
# 索引器
概念:
让对象像数组一样可以通过下标访问,方便程序编写
- 索引器(Indexer)可以让对象像数组一样通过 `[]` 访问
- 它很像属性,只是访问器带参数
- 一般用一个数组和索引器写法来实现按照下标访问成员
通常用法:
- 封装内部数组或集合
- 让类的使用方式更自然
语法:
```Csharp
访 this[ 1 2.....]
```csharp
访 this[]
{
get{} ====
set{}
get { }
set { }
}
```
案例:
```Csharp
class Person
```csharp
class Team
{
private string name;
priavte int age;
private Person[] friends;
public Person this[int index]
private string[] members = new string[3];
public string this[int index]
{
get{return friends[index];}
set{friends[index] = value;}
get
{
return members[index];
}
set
{
members[index] = value;
}
}
}
Person p1 = new Person();
p[0] = new Person();
Team team = new Team();
team[0] = "小明";
Console.WriteLine(team[0]);
```
补充:
- 索引器也可以只有 `get`,做成只读
- 索引器也可以有多个参数,比如 `this[int row, int col]`
注意:
1. 索引器写法里的名字固定是 `this`,不是自定义名字
2. 索引器本质上还是类成员,不是数组本身
3. `[]` 不是直接重载出来的,==而是通过索引器语法提供的==
4. 如果内部是真实数组或列表,通常还要自己考虑越界问题
#封装
#核心