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 @ 2022-08-20 16:13  坞中客  阅读(64)  评论(0)    收藏  举报