C#---高级|设计模式

【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#region old
public class Animal : MonoBehaviour {
    public string name;
    public Animal(string name) {
        this.name = name;
    }
    public void  Shout() {
        if (name == "鸟") {
            Debug.Log("吱吱吱");
        }
        else if (name=="猫") {
            Debug.Log("喵喵喵");
        }
    }
}
#endregion
#region New
public class NewAnimal {
    public string name;
    public NewAnimal(string name) {
        this.name = name;
    }
    public virtual void Shout() {

    }
}

public class Cat : NewAnimal {
    public Cat(string name) : base(name) { }
    public override void Shout()
    {
        base.Shout();
        Debug.Log("喵喵喵");
    }
}

public class Bird : NewAnimal
{
    public Bird(string name) : base(name) { }
    public override void Shout()
    {
        base.Shout();
        Debug.Log("吱吱吱");
    }
}

public class Dog : NewAnimal
{
    public Dog(string name) : base(name) { }
    public override void Shout()
    {
        base.Shout();
        Debug.Log("汪汪汪");
    }
}

#endregion
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SRPConsole : MonoBehaviour {

    #region Old
    // Use this for initialization
    void OldStart () {
        Animal bird=new Animal("鸟");
        bird.Shout();//吱吱吱
        Animal cat = new Animal("猫");
        cat.Shout();
    }
    #endregion

    #region New
    void Start()
    {
        Cat cat = new Cat("mimi");
        cat.Shout();
        Dog dog = new Dog("旺财");
        dog.Shout();

    }
    #endregion

}
 

 子类必须有自己的行为和特征

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#region Old
public class GameRole : MonoBehaviour {
    public string name;
    public GameRole(string name) {
        this.name = name;
    }
    public void Talk(GameRole otherGameRole) {
        Debug.Log(name+"正在和"+otherGameRole+"进行谈话。。。");
    }
    public virtual void Attack(GameRole otherGameRole) {
        Debug.Log(name+"正在攻击"+otherGameRole);
    }    
}
public class Hero : GameRole
{
    public Hero(string name) : base(name)
    {
    }
}


public class NPC : GameRole
{
    public NPC(string name) : base(name)
    {
    }
    public override void Attack(GameRole otherGameRole) {
        Debug.Log("当前角色不能攻击。。");
        throw new System.Exception();
    }
}
#endregion
#region New
public class NewGameRole {
    public string name;
    public NewGameRole(string name)
    {
        this.name = name;
    }
    public void Talk(NewGameRole otherGameRole)
    {
        Debug.Log(name + "正在和" + otherGameRole + "进行谈话。。。");
    }
}
public class CanAttackGameRole : NewGameRole
{
    public CanAttackGameRole(string name) : base(name)
    {
    }
    public void Attack(NewGameRole otherGameRole)
    {
        Debug.Log(name + "正在攻击" + otherGameRole);
    }
    public NewGameRole otherGameRole;
    public void BuyEquip() { }


}

public class NewNPC : NewGameRole
{
    public NewNPC(string name) : base(name)
    {
    }
}

#endregion
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LSPConsole : MonoBehaviour {
    #region old
    private void Awake()
    {
        Hero angel = new Hero("Angle");
        NPC shopNPC = new NPC("ShopNPC");
        //shopNPC.Attack(angel);
        GameRoleAttack(shopNPC); //问此时会不会抛出异常?
        //向上转型
        //GameRole gameRole = shopNPC;
    }
    public void GameRoleAttack(GameRole gameRole) {
        //调用的是父类
        gameRole.Attack(new GameRole("机器人"));
    }
    #endregion
    #region New
    //private void Start()
    //{
    //    NewNPC shopNPC = new NewNPC("ShopNPC");
        
    //    CanAttackGameRole hero = new CanAttackGameRole("德玛西亚");
    //    shopNPC.Talk(hero);
    //    hero.otherGameRole = shopNPC;
    //    hero.Attack(hero);
    //}
    #endregion
}
 

猜你喜欢

转载自blog.csdn.net/renwen1579/article/details/121907176