[Unity 代码写法整理]嵌套判断问题(二)

博客讨论了嵌套判断的优化方法。介绍了无需条件判断、以多数服从少数的实现方式,将实现和调用函数分离,摆脱条件判断对代码可读性的影响。还提及多层嵌套判断是降阶问题,可将维度降至一维,并给出用逻辑运算符细化条件、封装条件成对象等降维方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4.前面三种都是需要if else switch case去判断的,现在写一种不需要条件判断的。思想是以多数服从少数,多数包含少数的实现,少数调用。文字上难以理解,直接上代码比较好。还是防具和武器播放不同动画的栗子,这里假如武器种类多,防具数目比较少,也就是武器是多数,防具是少数

public class ArmorBase
{
    public virtual void PlayAnimation()
    {
        //for override
    }
}

public interface IWeapon
{
    void PlayLeatherAnimation();
    void PlayChainAnimation();
    void PlayPlateAnimation();
}

//装备系统,作为一个单例,
public class EquipmentSystem : Singleton<EquipmentSystem>
{
    private IWeapon currentWeapon;
    private ArmorBase currentArmor;

    public void SetWeapon(IWeapon weapon)
    {
        currentWeapon = weapon;
    }

    public void SetArmor(ArmorBase armor)
    {
        currentArmor = armor;
    }

    public void PlayAnimation()
    {
        currentArmor.PlayAnimation();
    }
}

//只写Leather,Chain和Plate同理
public class Leather : ArmorBase
{
    public override void PlayAnimation()
    {
        EquipmentSystem.Instance.currentWeapon.PlayLeatherAnimation();
    }
}

//只写个Sword,其他武器同理
public class Sword : IWeapon
{
    public void PlayLeatherAnimation()
    {
        //拿剑穿皮甲的动画
    }

    public void PlayChainAnimation()
    {
        //拿剑穿锁甲的动画
    }

    public void PlayPlateAnimation()
    {
        //拿剑穿板甲的动画
    }
}

int Main()
{
    EquipmentSystem.Instance.SetWeapon(new Sword());
    EquipmentSystem.Instance.SetArmor(new Leather());
    EquipmentSystem.Instance.PlayAnimation();
}

其实这个跟第一种方法还是有点像的,只不过武器类一个函数转化为三个函数,不过还是有区别的。第一种方法实现和调用是同一个函数,而这种方式,实现和调用的函数分离开来,而且摆脱了条件判断对代码可读性的影响,更加容易定位出现的问题。

==============================

5.如果嵌套判断不止两层,有多层怎么办。其实这个问题就跟拧魔方一样,是一个降阶问题。最理想的情况,是要将嵌套判断的维度下降到一维。

if(X1)
{
    if(Y1)
    {
        if(Z1)
        {

        }
    }
}

这种判断如果多了,会很痛苦。

if(X1 && Y1 && Z1)
{
    
}

这样就将三维的判断降低到一维。

==============================

6.嵌套判断降低维度的一些方法

一种是用&&和||去细化判断条件,不过判断条件越多就越长越乱。更合理的办法是通过将判断条件封装成对象,然后写一个Utils类或者重写运算符的方式,把判断的条件创建一个模板对象。栗子如下:

public class StudentConditionClass
{
    public int age;
    public float height;
    
    public StudentConditionClass(int ageArg,float heightArg)

    //重载运算符
    public static bool operator ==(StudentConditionClass studen1, StudentConditionClass student2)
    {
        return student1.age == student2.age && student1.height == student2.height;
    }   

    //重载运算符
    public static bool operator !=(StudentConditionClass studen1, StudentConditionClass student2)
    {
        return !(student1.age == student2.age && student1.height == student2.height);
    }  
}

public class JudgeUtils
{
    //写一个Utils类去判断
    public static bool IsStudentEqual(StudentConditionClass studen1,StudentConditionClass student2)
    {
        return student1.age == student2.age && student1.height == student2.height;
    }
}

int Main()
{
    StudentConditionClass student = new StudentConditionClass(18,183.1f);
    //判断模板1
    StudentConditionClass studentTemplete1 = new StudentConditionClass(18,183.1f);
    //判断模板2
    StudentConditionClass studentTemplete2 = new StudentConditionClass(19,190f);

    //重载运算符判断
    if(student == studentTemplete1)
    {
        //XXX
    }
    //Utils判断
    else if(JudgeUtils.IsStudentEqual(studen,studentTemplete2))
    {
        //XXX
    }
}

这样的话判断条件会简化,也更易读。不过会有多余内存占用,毕竟每一个用于判断的模板也是对象。所以最终使用的时候还是要根据情况选择最优的方法判断。

==============================

OK嵌套判断的理解就告一段落了,欢迎讨论。下一周开始写一些跟设计模式有关的代码写法了~

Unity 中,`string.Format` 是一种用于字符串格式化的强大工具。它允许开发者通过占位符来动态构建字符串内容。以下是关于 `string.Format` 的一些常见用法以及可能遇到的问题及其解决方案。 ### 使用方法 #### 基本语法 `string.Format` 方法接受一个格式化字符串作为第一个参数,并将其余参数替换到指定的位置上[^4]。 ```csharp string result = string.Format("The value is {0}", 42); // 输出: The value is 42 ``` #### 多个参数 可以传递多个参数并按顺序替换它们。 ```csharp string formattedString = string.Format("{0} plus {1} equals {2}", 2, 3, 5); // 输出: 2 plus 3 equals 5 ``` #### 数字格式化 支持对数值进行特定格式的转换,比如货币、百分比等。 ```csharp decimal price = 9.78m; string output = string.Format("Price: {0:C}", price); // 货币格式 // 输出: Price: $9.78 (具体显示取决于当前文化设置) ``` #### 文化敏感性 不同的地区有不同的日期时间或数字表示方式,可以通过指定 `IFormatProvider` 来调整行为。 ```csharp CultureInfo culture = new CultureInfo("fr-FR"); string dateOutput = string.Format(culture, "{0:d}", DateTime.Now); // 法语环境下的日期格式 ``` ### 可能遇到的问题及解决办法 #### 错误一:索引超出范围异常 如果提供的参数少于格式串中定义的数量,则会抛出此错误。 **解决方法**: 确保传入足够的参数匹配所有的 `{}` 占位符[^5]。 #### 错误:不正确的占位符使用 例如忘记加花括号或者嵌套不当都会引发问题。 **示例修复前后的对比** ```csharp // 错误写法 string wrongExample = string.Format("Value:{", 1); // 正确写法 string correctExample = string.Format("Value:{{}}", 1); // 显示 Value:{} string anotherCorrectOne = string.Format("Value:{0}", 1); // 显示 Value:1 ``` #### 性能考虑 频繁调用 `string.Format` 对性能有一定影响,在循环内部应谨慎使用,可改用 `StringBuilder` 提高效率[^6]。 ### 结论 综上所述,掌握好 `string.Format` 的基本规则和注意事项有助于更高效地编写代码,同时减少潜在错误的发生几率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值