编写基于条件的 if 语句的优雅方式

发布于 2024-11-30 03:09:48 字数 481 浏览 1 评论 0原文

我试图根据一组标准在列表中查找对象。基本逻辑如下所示

for (objectx obj : list)
{
    if (object.property1 > criteria1)
        //accept object
    else (object.property1 == criteria1)
    {
        if (object.property2 > criteria2)
            //accept object
        else (object.property2 == criteria2)
        {
            if (object.property3 > criteria3)
                  ... etc
        } 
    }                                  
}

有没有一种优雅的方法来简化这个混乱?

I'm trying to find an object in a list based on a set of criteria. The basic logic looks like this

for (objectx obj : list)
{
    if (object.property1 > criteria1)
        //accept object
    else (object.property1 == criteria1)
    {
        if (object.property2 > criteria2)
            //accept object
        else (object.property2 == criteria2)
        {
            if (object.property3 > criteria3)
                  ... etc
        } 
    }                                  
}

Is there an elegent way to simplify this mess?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妥活 2024-12-07 03:09:49
boolean accept = false;
for (int i = 0; i < object.numProps(); i++) {
   if (object.prop[i] > criteria[i]) {
       accept = true;
       break;
   } else if (object.prop[i] < criteria[i]) 
       break;
   // loop continues only if object.prop[i] == criteria[i]
}
boolean accept = false;
for (int i = 0; i < object.numProps(); i++) {
   if (object.prop[i] > criteria[i]) {
       accept = true;
       break;
   } else if (object.prop[i] < criteria[i]) 
       break;
   // loop continues only if object.prop[i] == criteria[i]
}
梦一生花开无言 2024-12-07 03:09:49

我将创建一个方法来检查所有属性,然后在循环中您可以简单地说:

for (objectx obj : list)
{
    if(checkProperties(obj))
    {
        //Do Stuff
    }
}

I would create a method that checks all of the properties then inside your loop you can simply say:

for (objectx obj : list)
{
    if(checkProperties(obj))
    {
        //Do Stuff
    }
}
剪不断理还乱 2024-12-07 03:09:49

添加以您的业务逻辑命名的方法可能会有所帮助。然后阅读代码应该像英语一样读起来,而实际的比较在其他地方。这也让思考变得更容易。

Adding methods named after your business logic may help. Then reading the code should read like english and the actual comparisons are somewhere else. It makes it easier to think about too.

娇俏 2024-12-07 03:09:49

如果您想做像我之前那样的事情,那会有所帮助。我想像你一样使用 NHibernate 搜索数据库中的对象。我创建了查询对象来收集用户输入的每个条件。当我在查询对象上设置相关属性时,我添加了这样的条件。

public bool Archive
    {
        set
        {
            if(value)
            {
                criteria.Add(Restrictions.Eq("Archive", true));
            }
        }
    }

    public bool IsFavorite
    {
        set
        {
            if (value)
            {
                criteria.Add(Restrictions.Eq("IsFavorite", true));
            }
        }
    }

如果你只想过滤内存中的对象。您还可以使用规范模式

If you want to do something like I did before that can help. I wanted to search for objects in database with NHibernate like you did. I created Query object to collect every criteria entered by user . And When I set the related property on Query object I added this criteria like this.

public bool Archive
    {
        set
        {
            if(value)
            {
                criteria.Add(Restrictions.Eq("Archive", true));
            }
        }
    }

    public bool IsFavorite
    {
        set
        {
            if (value)
            {
                criteria.Add(Restrictions.Eq("IsFavorite", true));
            }
        }
    }

If you just want to filter objects in memory. You can also use Specification Pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文