unity基础学习十三,C#高级属性:属性(Property)和索引器C# 索引器(Indexer)

unity基础学习十三,C#高级属性:属性(Property)和索引器C# 索引器(Indexer)

C# 中的属性和索引器

在C#中,属性(Properties)和索引器(Indexers)是两种用于访问对象成员的机制。它们提供了对类、结构或接口的封装,并允许开发者以更直观和灵活的方式操作数据。

属性(Properties)

属性是类、结构或接口的成员,可以用来读取或设置其内部状态。属性通常由一个 get 访问器和/或一个 set 访问器组成。

基本语法

public class Person
{
    private string name;

    // 属性
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

自动实现的属性

对于简单的属性,可以使用自动实现的属性(Auto-implemented properties),编译器会自动生成一个私有字段来存储属性值。

public class Person
{
    // 自动实现的属性
    public string Name { get; set; }
}

只读和只写属性

  • 只读属性:只有 get 访问器。
  • 只写属性:只有 set 访问器。
public class Person
{
    private int age;

    // 只读属性
    public int Age
    {
        get { return age; }
    }

    // 只写属性
    public void SetAge(int value)
    {
        age = value;
    }
}

索引器(Indexers)

索引器允许类、结构或接口像数组一样被索引。索引器通过 this 关键字定义,可以接受一个或多个参数。

基本语法

public class IndexedNames
{
    private string[] namelist = new string[size];
    static public int size = 10;

    // 索引器
    public string this[int index]
    {
        get
        {
            if (index >= 0 && index <= size - 1)
                return namelist[index];
            else
                return "";
        }
        set
        {
            if (index >= 0 && index <= size - 1)
                namelist[index] = value;
        }
    }

    public IndexedNames()
    {
        for (int i = 0; i < size; i++)
        {
            namelist[i] = "N. A.";
        }
    }

    static void Main(string[] args)
    {
        IndexedNames names = new IndexedNames();
        names[0] = "Zara";
        Console.WriteLine(names[0]); // 输出: Zara
    }
}

重载索引器

索引器可以被重载,允许使用不同的参数类型。

public class IndexedNames
{
    private string[] namelist = new string[size];
    static public int size = 10;

    // 索引器
    public string this[int index]
    {
        get { return namelist[index]; }
        set { namelist[index] = value; }
    }

    // 重载的索引器
    public int this[string name]
    {
        get
        {
            for (int i = 0; i < size; i++)
            {
                if (namelist[i] == name)
                    return i;
            }
            return -1;
        }
    }

    static void Main(string[] args)
    {
        IndexedNames names = new IndexedNames();
        names[0] = "Zara";
        Console.WriteLine(names["Zara"]); // 输出: 0
    }
}

总结

  • 属性:用于读取和设置类的内部状态,通常使用 getset 访问器。
  • 索引器:允许类像数组一样被索引,通过 this 关键字定义。

通过合理使用属性和索引器,可以提高代码的可读性和可维护性。

Read more

🚀Zeek.ai一款基于 Electron 和 Vite 打造的跨平台(支持 Windows、macOS 和 Linux) AI 浏览器

🚀Zeek.ai一款基于 Electron 和 Vite 打造的跨平台(支持 Windows、macOS 和 Linux) AI 浏览器

是一款基于 Electron 和 Vite 打造的跨平台(支持 Windows、macOS 和 Linux) AI 浏览器。 集成了 SearXNG AI 搜索、开发工具集合、 市面上最流行的 AI 工具门户,以及代码编写和桌面快捷工具等功能, 通过模块化的 Monorepo 架构,提供轻量级、可扩展且高效的桌面体验, 助力 AI 驱动的日常工作流程。

By Ne0inhk
超快速,使用ChatGPT编写回归和分类算法

超快速,使用ChatGPT编写回归和分类算法

本文将使用一些 ChatGPT 提示,这些提示对于数据科学家在工作时非常重要。 微信搜索关注《Python学研大本营》,加入读者群,分享更多精彩 以下是一些示例ChatGPT 提示的列表以及数据科学家的响应。 ChatGPT 提示 为决策树回归算法生成 python 代码。 下面是使用scikit-learn在 Python 中进行决策树回归的示例代码: import numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor # Generate random data rng = np.random.default_rng() x = 5 * rng.random(100) y = np.sin(x) + 0.

By Ne0inhk
力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

993.二叉树的堂兄弟节点 难度:简单 题目: 在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。 如果二叉树的两个节点深度相同,但 父节点不同 ,则它们是一对堂兄弟节点。 我们给出了具有唯一值的二叉树的根节点 root ,以及树中两个不同节点的值 x 和 y 。 只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true 。否则,返回 false。 示例: 示例 1: 输入:root = [1,2,3,4], x = 4, y = 3 输出:false

By Ne0inhk