没有赋值的 Java 三元
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15977031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Ternary without Assignment
提问by James Oravec
Is there a way to do a java ternary operation without doing an assignment or way to fake the assingment?
有没有办法在不进行赋值或伪造赋值的情况下进行 java 三元运算?
I like how succinct ternary code looks when doing a bunch of if/then/elses.
我喜欢在执行一堆 if/then/elses 时简洁的三元代码的外观。
I'm hoping to be able to call one of two void functions based on a boolean algebra statement.
我希望能够基于布尔代数语句调用两个 void 函数之一。
Something like:
就像是:
(bool1 && bool2) ? voidFunc1() : voidFunc2();
(bool1 && bool2) ? voidFunc1() : voidFunc2();
My functions are of return type void
, so if there is a way to fake this in an assignment to make it work, then I"m okay with that... I would like to see how to do it though :)
我的函数是返回类型void
,所以如果有一种方法可以在赋值中伪造它以使其工作,那么我可以接受...不过我想看看如何做:)
采纳答案by Deepak Bala
Nope you cannot do that. The spec says so.
不,你不能那样做。该规范是这么说的。
The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions.
The first expression must be of type boolean or Boolean, or a compile-time error occurs.
It is a compile-time error for either the second or the third operandexpression to be an invocation of a voidmethod.
条件运算符具有三个操作数表达式。? 出现在第一个和第二个表达式之间,并且 : 出现在第二个和第三个表达式之间。
第一个表达式必须是 boolean 或 Boolean 类型,否则会发生编译时错误。
第二个或第三个操作数表达式调用void方法是编译时错误。
[EDIT]
[编辑]
Since you asked about reflection, here's a solution. I'm not recommending this. I'm posting it only because you asked.
既然你问了反射,这里有一个解决方案。我不推荐这个。我发布它只是因为你问了。
public class MyCall
{
public void a(){System.out.println("a");}
public void b(){System.out.println("b");}
public static void main(String... args)
{
new MyCall().go();
}
public void go()
{
Class<? extends MyCall> class1 = this.getClass();
Method aMethod = class1.getMethod("b", null);
Method bMethod = class1.getMethod("a", null);
Object fake = false ? aMethod.invoke(this, null) : bMethod.invoke(this, null);
Object fake2 = true ? aMethod.invoke(this, null) : bMethod.invoke(this, null);
}
}
At the end of the day you've got to ask yourself if being succint improves your code's readability (think for-each loop). None of these solutions improve the code's readability IMHO. If I were you I'd rather go with this.
在一天结束时,您必须问自己简洁是否可以提高代码的可读性(想想 for-each 循环)。恕我直言,这些解决方案都没有提高代码的可读性。如果我是你,我宁愿选择这个。
if(condition)
a();
else
b();
I'm actually forincluding braces even when loops only contain a single line, but since you're going after crisp code, the snippet above should do.
我实际上是为了包含大括号,即使循环只包含一行,但是由于您要使用清晰的代码,因此上面的代码段应该可以。
回答by Ajay S
No, you can't do this like this.
不,你不能这样做。
You can prefer this style if do not like make it more statements.
如果不喜欢做更多的陈述,你可以更喜欢这种风格。
if(bool1 && bool2) voidFunc1(); else voidFunc2();
In ternary operator, Operands are required to be non-void expressions; i.e. they must produce some actual value.
在三元运算符中,操作数必须是非空表达式;即他们必须产生一些实际价值。
回答by Alex Stamper
If you really-really want to use ternany operation, then there is one hack. BUT this is very bad code, intended only for showing abilities of language. I would never recommend to put this code in production or even show to your friends.
如果您真的真的想使用 ternany 操作,那么有一个技巧。但这是非常糟糕的代码,仅用于展示语言能力。我永远不会建议将此代码投入生产,甚至不会向您的朋友展示。
int dummy = (bool1 && bool2) ? new Object(){
public int hashCode() {
yourFunction1();
// ...
yourFunctionN();
return 0;
};
}.hashCode() : new Object(){
public int hashCode() {
yourAnotherFunction1();
// ...
yourAnotherFunctionN();
return 0;
};
}.hashCode();
回答by Stephen C
Is there a way to do a java ternary operation without doing an assignment or way to fake the assignment?
有没有办法在不进行赋值或伪造赋值的情况下进行 java 三元运算?
OK, so when you write a statement like this:
好的,所以当你写这样的语句时:
(bool1 && bool2) ? voidFunc1() : voidFunc2();
there are two distinct problems with the code:
代码有两个明显的问题:
The 2nd and 3rd operands of a conditional expression1cannot be calls to void methods. Reference: JLS 15.25.
An expression is not a statement, unless it is either and assignment expression OR a method call OR a object creation. Reference: JLS 14.8.
In fact, the second of these problems is a syntaxerror and I would expect any mainstream Java compilers to report it instead of the first problem. The first problem would only reveal itself if you did something like this:
事实上,这些问题中的第二个问题是语法错误,我希望任何主流 Java 编译器都会报告它而不是第一个问题。如果你做这样的事情,第一个问题只会出现:
SomeType dummy = (bool1 && bool2) ? voidFunc1() : voidFunc2();
or
或者
gobble((bool1 && bool2) ? voidFunc1() : voidFunc2());
where gobble
is a method that does nothing ... except "consume" the value of its argument.
wheregobble
是一个什么都不做的方法......除了“消耗”其参数的值。
AFAIK, there is no context in which the original expression is acceptable.
AFAIK,没有可以接受原始表达的上下文。
1 - "Conditional expression" is the primary term used for this construct in the Java Language Specification. It is called the "ternary conditional operator" in Oracle Java Tutorial.
1 - “条件表达式”是 Java 语言规范中用于此构造的主要术语。它在 Oracle Java 教程中称为“三元条件运算符”。