Unity热更新方案C#Like(三)-详解支持的C#特性:类

C#Like是Unity的热更方案,使用纯C#语言写出可以热更新的代码,就像可以在所有平台使用DLL(动态链接库)文件一样.遵从KISS设计原则,让用户轻松构建或升级成Unity的热更新项目.

简介

本篇主要介绍C#Like支持的C特性:类.

  • C#Like免费版功能:只能继承于接口(当然继承于LikeBehaviour是特殊处理,其他的基类一律不支持),另外支持分部partial.
  • C#Like完整版功能:能继承于接口和类;支持分部partial;支持构造函数(支持this/base);支持析构函数;支持虚函数(abstract/virtual/override);支持多重继承;

C#Like免费版:

下面做一个接口IAnimal,然后做一个Mammals继承于接口IAnimal,通过这1个接口和3个类来说明类的功能:

using UnityEngine;  
using CSharpLike.Subclass;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
        /// 测试非热更代码的类.  
        /// </summary>  
        void TestClass()  
        {  
            Debug.LogError("示范类: 你不能在C#Like免费版里类继承/虚函数/析构函数/构造函数(this/base) (在完整版已支持).强烈推荐升级到完整版: ");  
            // 我们提供以下方案给免费用户:  
            // 析构函数/构造函数(this/base)/虚函数: 使用普通函数手动调用  
            // 类继承/虚函数: 不使用,就像C语言甚至没有类的概念也一样可以做任何事,更何况你可使用类和继承接口.  
            // 导致的后果是你的代码没有那么面向对象,编码风格变成面向过程.  
            //测试类  
            Mammals cow = new Mammals(4, 4, "cow");  
            Debug.Log("cow:" + cow.GetInfo());  
            //测试接口  
            IAnimal bull = new Mammals(0, 4, "bull");  
            bull.female = false;  
            Debug.Log("bull:female=" + bull.female +  
            ", CanUseTool=" + bull.CanUseTool());  
        }  
    }  
}
using UnityEngine;  
  
namespace CSharpLike  
{  
    /// <summary>  
    /// 测试类  
    /// </summary>  
    namespace Subclass  
    {  
        //接口  
        public interface IAnimal  
        {  
            int feet { get; set; }  
            bool female { get; set; }  
            bool CanUseTool();  
        }  
        //哺乳动物  
        public class Mammals : IAnimal  
        {  
            public int breasts;  
            public string name;  
  
            public int feet { get; set; }  
            public bool female { get; set; }  
  
            public Mammals(int breasts, int feet, string name)  
            {  
                Debug.Log("Mammals(" + breasts + "," + feet + "," + name + ")");  
                this.breasts = breasts;  
            }  
  
            public string GetInfo()  
            {  
                return "Mammals:" + name + " with " + feet + " feet and " + breasts + " breasts";  
            }  
            public bool CanUseTool()  
            {  
                return false;  
            }  
        }  
    }  
    /// <summary>  
    /// 这个命名空间是用于测试和Subclass区分的  
    /// </summary>  
    namespace SubclassEx  
    {  
        /// <summary>  
        /// 测试CSharpLike.Subclass.Mammals同类名不同命名空间用  
        /// </summary>  
        public class Mammals  
        {  
            public int breasts = 2;  
            public int eyes = 2;  
            public string TestNameSpace()  
            {  
                return "Mammals:breasts:" + breasts + ", eyes:" + eyes;  
            }  
        }  
        public class Toys  
        {  
            public string name;  
            public Toys(string name)  
            {  
                this.name = name;  
            }  
        }  
    }  
    /// <summary>  
    /// 测试调用完整类名(含命名空间)方式调用热更代码  
    /// </summary>  
    namespace SubclassEx2  
    {  
        public class TestNamespace  
        {  
            public static string GetTestString()  
            {  
                return "Test string for namespace";  
            }  
        }  
    }  
}  

C#Like完整版:

下面做一个接口IAnimal,然后做一个抽象基类Animal继承于IAnimal, 然后做一个普通类Mammals继承于Animal,最后普通类Human继承于Mammals. 通过这1个接口和3个类来说明类的功能

using UnityEngine;  
using CSharpLike.Subclass;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
        /// 测试非热更代码的类.  
        /// </summary>  
        void TestClass()  
        {  
            Debug.LogError("Test class:");  
            //测试类  
            Mammals cow = new Mammals(4, 4, "cow");  
            Debug.Log("cow:" + cow.GetInfo());  
            //测试接口  
            IAnimal bull = new Mammals(0, 4, "bull");  
            bull.female = false;  
            Debug.Log("bull:female=" + bull.female +  
            ", CanUseTool=" + bull.CanUseTool());  
            //测试构造函数,注意构造函数执行顺序  
            Human adam = new Human("Adam", false);  
            Debug.Log("adam:" + adam.GetInfo());  
            Debug.Log("adam:GetTypeString=" + adam.GetTypeString());  
            Debug.Log("adam:GetBaseTypeString=" + adam.GetBaseTypeString());  
            //测试构造函数  
            Human eve = new Human("Eve");  
            Debug.Log("eve:" + eve.GetInfo()  
            + ", CanUseTool=" + eve.CanUseTool());  
            //测试析构函数,C#内存管理自动析构,注意析构函数执行顺序  
        }  
    }  
} 
using UnityEngine;  
  
namespace CSharpLike  
{  
    /// <summary>  
    /// 这个命名空间是用于测试和Subclass区分的  
    /// </summary>  
    namespace SubclassEx  
    {  
        /// <summary>  
        /// 测试CSharpLike.Subclass.Mammals同类名不同命名空间用  
        /// </summary>  
        public class Mammals  
        {  
            public int breasts = 2;  
            public int eyes = 2;  
            public string TestNameSpace()  
            {  
                return "Mammals:breasts:" + breasts + ", eyes:" + eyes;  
            }  
        }  
        public class Toys  
        {  
            public string name;  
            public Toys(string name)  
            {  
                this.name = name;  
            }  
        }  
    }  
    /// <summary>  
    /// 测试调用完整类名(含命名空间)方式调用热更代码  
    /// </summary>  
    namespace SubclassEx2  
    {  
        public class TestNamespace  
        {  
            public static string GetTestString()  
            {  
                return "Test string for namespace";  
            }  
        }  
    }  
}  
using UnityEngine;  
namespace CSharpLike  
{  
    /// <summary>  
    /// 测试类  
    /// </summary>  
    namespace Subclass  
    {  
        //接口  
        public interface IAnimal  
        {  
            int feet { get; set; }  
            bool female { get; set; }  
            bool CanUseTool();  
        }  
        //抽象类:动物  
        public abstract class Animal : IAnimal//测试继承接口  
        {  
            public int feet { get; set; }  
            public string name;  
            public bool female { get; set; }  
            public Animal(int feet, string name)  
            {  
                Debug.Log("Animal(" + feet + "," + name + ")");  
                this.feet = feet;  
                this.name = name;  
                female = true;  
            }  
            ~Animal()  
            {  
                Debug.Log("~Animal():" + name);  
            }  
            public virtual string GetInfo()  
            {  
                return "Animal:" + name + " with " + feet + " feet. " + clothes;  
            }  
            public abstract bool CanUseTool();//测试关键字'abstract'  
            public virtual string GetTypeString()//测试关键字'virtual'  
            {  
                return "Animal";  
            }  
            public virtual string clothes  
            {  
                get  
                {  
                    return "naked";  
                }  
            }  
        }  
        //哺乳动物  
        public class Mammals : Animal//继承于动物  
        {  
            public int breasts;  
            public Mammals(int breasts, int feet, string name)  
                : base(feet, name)  
            {  
                Debug.Log("Mammals(" + breasts + "," + feet + "," + name + ")");  
                this.breasts = breasts;  
            }  
            ~Mammals()//测试析构函数  
            {  
                Debug.Log("~Mammals():" + name);  
            }  
            public override string GetInfo()//测试关键字'override'  
            {  
                return "Mammals:" + name + " with " + feet + " feet and "  
                    + breasts + " breasts," + clothes;  
            }  
            public override bool CanUseTool()  
            {  
                return false;  
            }  
            public override string GetTypeString()  
            {  
                return "Mammals";  
            }  
        }  
        //人类  
        public class Human : Mammals//继承于哺乳动物  
        {  
            public Human(string name, bool female)//测试构造函数  
                : base(2, 2, name)//测试构造函数中的关键字'base'  
            {  
                Debug.Log("Human(" + name + "," + female + ")");  
                this.female = female;//test 'this' keyword  
            }  
            public Human(string name)//测试构造函数  
                : this(name, true)//测试构造函数中的关键字'this'  
            {  
                Debug.Log("Human(" + name + ")");  
            }  
            ~Human()//测试析构函数  
            {  
                Debug.Log("~Human():" + name);  
            }  
            public override string GetInfo()  
            {  
                return "Human:" + name + " with " + feet + " feet and " + breasts  
                    + " breasts, and female = " + female + "," + clothes;  
            }  
            public override bool CanUseTool()//测试关键字'override'  
            {  
                return true;  
            }  
            public override string GetTypeString()  
            {  
                return "Human";  
            }  
            public string GetBaseTypeString()  
            {  
                return base.GetTypeString();//测试类中的关键字'base'  
            }  
            public override string clothes  
            {  
                get  
                {  
                    return female ? "dress" : "suit";//测试自定义'get/set'访问器  
                }  
            }  
        }  
    }  
}  

 本系列文章导读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C#Like

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值