Unity-拓展方法

拓展方法:在不破坏原始类的情况下,添加成员方法,只能添加成员方法。
一般用于向已经封装好的程序集中添加新的方法

using UnityEngine;

public class Test : MonoBehaviour {
    void Start() {
        People people = new People();
        // 调用拓展方法
        people.Eat();
    }
}

// 人类
class People {
    public void Run() {
        Debug.LogError("跑步");
    }

    public void PlayBasketball() {
        Debug.LogError("打篮球");
    }
}

// 人类的拓展类
 static  class PeopleExpand {
    public static void Eat(this People people) {
        Debug.LogError("吃饭");
    }
}
posted @   坞中客  阅读(62)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示