using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class DPState : MonoBehaviour { //枚举出所有状态 //note1 如果状态过多的话就需要一直用if-else if来实现 所以需要才用状态模式 //public enum PersonState //{ // EatMeals, // Work, // Sleep //} //public PersonState personState; // Use this for initialization void Start () { //personState = PersonState.Work; //if(personState == PersonState.Work) //{ // Debug.Log("我们正在工作"); //}else if(personState == PersonState.EatMeals) //{ // Debug.Log("我们正在吃饭"); //} //else //{ // Debug.Log("我们正在睡觉"); //} Context context = new Context(); //IState state = new EatMeals(context); context.SetState(new EatMeals(context)); context.Handle(); } } public interface IState { void Handle(); } //当前游戏环境下的 数据、状态、变量 public class Context //状态机 { private IState mState; //当前状态 public void SetState(IState state) { mState = state; } public void Handle() { mState.Handle(); //当前状态下需要执行的方法 } } public class EatMeals : IState { private Context mContext; //状态类下构造一个状态方法 把当前状态给传递进去 public EatMeals(Context context) { mContext = context; } public void Handle() { Debug.Log("我们正在吃饭"); } } public class Work : IState { private Context mContext; //状态类下构造一个状态方法 把当前状态给传递进去 public Work(Context context) { mContext = context; } public void Handle() { Debug.Log("我们正在工作"); } } public class Sleep : IState { private Context mContext; //状态类下构造一个状态方法 把当前状态给传递进去 public Sleep(Context context) { mContext = context; } public void Handle() { Debug.Log("我们正在睡觉"); } }
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class IOP : MonoBehaviour { // Use this for initialization void Start () { //左边声明 右边实例化 //note1 多态:构造一个方法 都继承了同一个接口 接口内的变量就可通过方法名来调用 //note5 用父类声明+子类构造(实例化),即可调用父类对象中定义的公有的成员变量和方法 BaseHero myHero = new Leblanc(); //myHero.SkillQ(); //myHero.hp = 10; } } //面向接口编程 //note2 接口里面已有的方法一定要实现,没有的方法可以另外扩展不受影响 //note3 接口内只能有方法不能有字段,字段可用基类实现 public interface IHero { //void SkillQ(); //void SkillW(); //void SkillE(); //void SkillR(); } public class BaseHero : IHero { //public int hp; //public void SkillE() //{ // Debug.Log("玩家按下E键"); //} //public virtual void SkillQ() //{ // Debug.Log("玩家按下Q键"); //} //public void SkillR() //{ // Debug.Log("玩家按下R键"); //} //public void SkillW() //{ // Debug.Log("玩家按下W键"); //} } //可让基类继承接口,然后人物方法继承基类。 public class Leblanc : BaseHero { public void SkillA() { throw new NotImplementedException(); } //note4 父类与子类的方法名相同时 加new=重写 public new void SkillE() { Debug.Log("幻影锁链"); } //note5 父类方法名是virtual时 子类方法直接定义为override既可实现重写 public void SkillQ() { Debug.Log("恶意魔印"); } public void SkillR() { Debug.Log("故技重施"); } public void SkillW() { Debug.Log("魔影迷踪"); } } public class Zed : IHero { public void SkillE() { Debug.Log("禁奥义 鬼斩"); } public void SkillQ() { Debug.Log("禁奥义 诸刃"); } public void SkillR() { Debug.Log("禁奥义 瞬狱影杀阵"); } public void SkillW() { Debug.Log("禁奥义 分身"); } }
有点乱 来不及整理了 晚上自己做了汤圆 以后再编辑(又一个坑
原文:https://www.cnblogs.com/LinawZ/p/12285801.html