在Java中用于表达式的别名

huangapple go评论92阅读模式
英文:

Alias for expressions in java

问题

在Java中,您可以使用方法或lambda表达式来避免重复编写条件。以下是您提供的示例的Java代码:

  1. // 使用方法
  2. public class Main {
  3. public static void main(String[] args) {
  4. if (cond(2, 5, 8)) {
  5. // 条件成立时执行的代码
  6. }
  7. if (cond(3, 6, 9)) {
  8. // 条件成立时执行的代码
  9. }
  10. }
  11. public static boolean cond(int i, int j, int k) {
  12. return (i == 2) && (j == 5) && (k == 8);
  13. }
  14. }

或者,您还可以使用lambda表达式来实现类似的功能:

  1. public class Main {
  2. public static void main(String[] args) {
  3. Condition cond258 = (i, j, k) -> (i == 2) && (j == 5) && (k == 8);
  4. Condition cond369 = (i, j, k) -> (i == 3) && (j == 6) && (k == 9);
  5. if (cond258.check(2, 5, 8) || cond369.check(3, 6, 9)) {
  6. // 条件成立时执行的代码
  7. }
  8. }
  9. interface Condition {
  10. boolean check(int i, int j, int k);
  11. }
  12. }

这两种方法都可以帮助您避免重复编写条件,并使代码更加清晰可读。

英文:

I have a if condition in java that reoccurs at many places in the code. I want to avoid writing the whole condition again and again. In C, I could have done this with #define

  1. #define cond ((i==2) && (j==5) && (k==8))
  2. int main() {
  3. if(cond)
  4. }

How can I achieve the same in java? I can probably create another method that evaluates this condition -

  1. main() {
  2. if(cond())
  3. }
  4. cond() {
  5. return (i==2) && (j==5) && (k==8);
  6. }

but I wanted to know if I can avoid creating another function.


UPDATE -

I realized I should add more details/edit to support my argument. Lets say I have 2 conditions and I want to check both-

  1. #define cond258 ((i==2) && (j==5) && (k==8))
  2. #define cond369 ((i==3) && (j==6) && (k==9))

I can create 2 functions -

  1. cond258(i, j, k) {
  2. return (i==2) && (j==5) && (k==8);
  3. }
  4. cond369(i, j, k) {
  5. return (i==3) && (j==6) && (k==9);
  6. }

this doesn't look like a good approach to me. Both functions are doing sort of similar things so they should be converted to single function -

  1. cond(i, j, k, first, second, third) {
  2. return (i==first) && (j==second) && (k==third);
  3. }

but then that makes my if condition unreadable -

  1. if(cond(i, j, k, 2, 5, 8) || cond(i, j, k, 3, 6, 9))

so instead if I could have some aliases, I could simply write this as

  1. if(cond258 || cond369)

答案1

得分: 4

我相信你不能这样做,而不编写另一个函数,或者至少不建议/实际。为条件语句编写另一种方法实际上是在重构你的代码,分解条件表达式 在这种情况下。

英文:

I believe you can't do so without writing another function, or at least, not advisable/practical. Writing another method for conditional statement is actually refactoring your code, Decompose conditional in this case.

答案2

得分: 1

你可以像这样做。

  1. private static int i = 2;
  2. private static int j = 5;
  3. private static int k = 8;
  4. private static Supplier<Boolean> cond = () -> i == 2 && j == 5 && k == 8;
  5. public static void main(String[] args) {
  6. System.out.println(cond.get()); // 输出 true
  7. k = 11;
  8. System.out.println(cond.get()); // 输出 false
  9. }
  • 我将它们设为静态,以便可以在任何上下文中使用(静态或实例)。
  • 这些变量不能是局部变量,因为 Lambda 表达式中的局部值必须是有效 final。
  • 它们将与实例化包含它们的类的其他类共享。

这是一个使用实例字段的示例。

  1. int ii = 2;
  2. int jj = 5;
  3. int kk = 8;
  4. Supplier<Boolean> cond = () -> ii == 2 && jj == 5 && kk == 8;
  5. public static void main(String[] args) {
  6. ThisClass tc = new ThisClass();
  7. // 静态上下文,因此需要限定
  8. System.out.println(tc.cond.get()); // 输出 true
  9. tc.kk = 11;
  10. System.out.println(tc.cond.get()); // 输出 false
  11. tc.foo();
  12. }
  13. public void foo() {
  14. // 实例方法,因此 cond 和 kk 不需要限定
  15. kk = 8;
  16. System.out.println(cond.get()); // true
  17. }
英文:

You could do something like this.

  1. private static int i = 2;
  2. private static int j = 5;
  3. private static int k = 8;
  4. private static Supplier&lt;Boolean&gt; cond = ()-&gt;i == 2 &amp;&amp; j == 5 &amp;&amp; k == 8;
  5. public static void main(String[] args) {
  6. System.out.println(cond.get()); // prints true
  7. k = 11;
  8. System.out.println(cond.get()); // prints false
  9. }
  • I made them static so they could be used in any context (static or instance).
  • The variables can't be local since local values in a lambda must be effectively final.
  • And they will be shared with other classes that instantiate the class that contains them.

Here is an example using instance fields.

  1. int ii = 2;
  2. int jj = 5;
  3. int kk = 8;
  4. Supplier&lt;Boolean&gt; cond = () -&gt; ii == 2 &amp;&amp; jj == 5 &amp;&amp; kk == 8;
  5. public static void main(String[] args) {
  6. ThisClass tc = new ThisClass();
  7. // static context so they need to be qualified.
  8. System.out.println(tc.cond.get()); // prints true
  9. tc.kk = 11;
  10. System.out.println(tc.cond.get()); // prints false
  11. tc.foo();
  12. }
  13. public void foo() {
  14. // instance method so cond and kk do not need to be qualified
  15. kk = 8;
  16. System.out.println(cond.get()); // true
  17. }
  18. </details>
  19. # 答案3
  20. **得分**: 0
  21. 这是我的解决方法 -
  22. 创建了一个新的 Triplet -
  23. ```java
  24. public class Triplet<F, S, T> {
  25. public F first;
  26. public S second;
  27. public T third;
  28. @Override
  29. public boolean equals(Object obj) {
  30. if (obj instanceof Triplet<?, ?, ?>) {
  31. Triplet<?, ?, ?> triplet = (Triplet<?, ?, ?>) obj;
  32. return (this.first.equals(triplet.first) && this.second.equals(triplet.second) && this.third.equals(triplet.third));
  33. }
  34. else
  35. return false;
  36. }
  37. public Triplet (F first, S second, T third) throws NullPointerException {
  38. if(first==null|| second==null || third==null)
  39. throw new NullPointerException("无法使用 null 值创建对象,第一个、第二个或第三个值");
  40. this.first = first;
  41. this.second = second;
  42. this.third = third;
  43. }
  44. }

创建了静态的 triplets -

  1. public static final Triplet<Integer, Integer, Integer> triplet258 = new Triplet<Integer, Integer, Integer>(2, 5, 8);
  2. public static final Triplet<Integer, Integer, Integer> triplet369 = new Triplet<Integer, Integer, Integer>(3, 6, 9);

我的条件判断改为 -

  1. Triplet<Integer, Integer, Integer> myTriplet = new Triplet<Integer, Integer, Integer>(i, j, k);
  2. if(myTriplet.equals(triplet258) || myTriplet.equals(triplet369))
英文:

This is how I solved this -

created a new class Triplet -

  1. public class Triplet &lt;F, S, T&gt; {
  2. public F first;
  3. public S second;
  4. public T third;
  5. @Override
  6. public boolean equals(Object obj) {
  7. if (obj instanceof Triplet &lt;?,?,?&gt;) {
  8. Triplet &lt;?, ?, ?&gt; triplet = (Triplet &lt;?, ?, ?&gt;) obj;
  9. return (this.first.equals(triplet.first) &amp;&amp; this.second.equals(triplet.second) &amp;&amp; this.third.equals(triplet.third));
  10. }
  11. else
  12. return false;
  13. }
  14. public Triplet (F first, S second, T third) throws NullPointerException {
  15. if(first==null|| second==null || third==null)
  16. throw new NullPointerException(&quot;Can&#39;t create object with null values for first or second or third&quot;);
  17. this.first = first;
  18. this.second = second;
  19. this.third = third;
  20. }
  21. }

Created static triplets -

  1. public static final Triplet&lt;Integer, Integer, Integer&gt; triplet258 = new Triplet&lt;Integer, Integer, Integer&gt;(2, 5, 8);
  2. public static final Triplet&lt;Integer, Integer, Integer&gt; triplet369 = new Triplet&lt;Integer, Integer, Integer&gt;(3, 6, 9);

my if condition changes to -

  1. Triplet&lt;Integer, Integer, Integer&gt; myTriplet = new Triplet&lt;Integer, Integer, Integer&gt;(i, j, k);
  2. if(myTriplet.equals(triplet258) || myTriplet.equals(triplet369))

huangapple
  • 本文由 发表于 2020年9月10日 02:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63817495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定