Utils类中的Predicates应该被提供为常量还是静态方法?(Predicates in Utils class should be provided as constants or as st

系统教程 行业动态 更新时间:2024-06-14 17:00:14
Utils类中的Predicates应该被提供为常量还是静态方法?(Predicates in Utils class should be provided as constants or as static method?)

最近,我一直在使用谓词和番石榴工具。 我创建了一个Utils.class,用于存储我在代码的不同部分使用的谓词。 因此,这个问题已经出现,我们(我和我的同事)没有就此达成一致。

将谓词放入实用类中的正确方法或“良好实践方式”是什么,常常用大写字母或静态方法来定义它。 以下我写一个例子:

public final class Utils { public static final Predicate<Element> IS_SPECIAL = new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; } }; public static Predicate<Element> isSpecial() { return new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; }}

顺便说一句,番石榴提供了一些预判谓词,它提供了它作为返回谓词的方法,但其他libreries也提供它们作为常量。

Lately, I have been working with predicates and guava utilities. I have created a Utils.class where I store some predicates that I use in different parts of the code. Thus this issue has come up and we (me and my workmates) do not get an agreement about it.

What is the right way or "good practice way" to put a predicate in a utility class?, as a constant defining it with capital letter or in a static method?. Following, I write an example:

public final class Utils { public static final Predicate<Element> IS_SPECIAL = new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; } }; public static Predicate<Element> isSpecial() { return new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; }}

By the way, guava offers some predifining predicates and it provides them as a method that returns predicates, but other libreries do the same offering them as constants.

更多推荐

本文发布于:2023-04-18 00:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4def1f92a1e0be4384df7e702be0f158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   静态   类中   方法   Predicates

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!