中的方法参数内工作我在开发中获得了这个注意。为什么三元运算符不能在java
为什么三元运算符不在方法参数内工作?这里显然是InputStream
或(else)String
。
class A{
public static boolean opAlpha(InputStream inputStream) {
// Do something
return true;
}
public static boolean opAlpha(String arg) {
// Do something else
return true;
}
public static void main(String[] args) throws Exception {
boolean useIsr = true;
InputStream inputStream = null;
String arg = null;
// boolean isTrue = useIsr ? A.opAlpha(inputStream): A.opAlpha(arg); // This is OK.
boolean isTrue = A.opAlpha(useIsr ? inputStream : arg); // This is not. (Error : The method opAlpha(InputStream) in the type A is not applicable for the arguments (Object))
}
}
这里没有操作员重载。 – EJP
还有我通常的迂腐的评论:它不是三元运算符,它是[*条件运算符*](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls- 15.25)。确实,它是一个**三元操作符(一个接受三个操作数的操作符),它确实是Java中唯一的一个,但理论上在某些时候可能会有其他操作符...... :-) –
Thanks T.J.明白了你的观点! – namalfernandolk