最近我正在阅读Spring Framework的源代码.我无法理解的东西在这里:
public Member getMember() {
// NOTE: no ternary expression to retain JDK <8 compatibility even when using
// the JDK 8 compiler (potentially selecting java.lang.reflect.Executable
// as common type, with that new base class not available on older JDKs)
if (this.method != null) {
return this.method;
}
else {
return this.constructor;
}
}
Run Code Online (Sandbox Code Playgroud)
此方法是类的成员org.springframework.core.MethodParameter.代码很容易理解,而评论很难.
注意:即使使用JDK 8编译器,也没有三元表达式来保持JDK <8的兼容性(可能选择
java.lang.reflect.Executable为通用类型,旧的JDK上没有新的基类)
if...else...在这种情况下使用三元表达和使用构造有什么区别?
我可以以某种方式在angularjs表达式中使用if-then-else构造(三元运算符),例如我有函数$ scope.isExists(item)必须返回bool值.我想要这样的东西,
<div ng-repeater="item in items">
<div>{{item.description}}</div>
<div>{{isExists(item) ? 'available' : 'oh no, you don't have it'}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用返回字符串的函数,我很有兴趣在表达式中使用if-then-else构造.谢谢.
javascript if-statement ternary-operator angularjs angularjs-ng-repeat
为了准备Oracle认证助理Java SE 8程序员1考试,我在官方学习指南中看到了关于三元表达的以下段落:
三元表达式评估
从Java 7开始,将仅在运行时评估三元运算符的右侧表达式之一.以类似于短路运算符的方式,如果三元运算符中的两个右手表达式之一执行副作用,则可能不会在运行时应用它.让我们用以下例子来说明这个原则:[...]
它表示只评估两个表达式中的一个,演示如下:
int y = 1;
int z = 1;
int a = y < 10 ? y++ : z++;
Run Code Online (Sandbox Code Playgroud)
在这里,只有y增量,但z没有,正如您所期望的那样.
我绊倒的是段落的开头(标记为黄色),其中显示"从Java 7开始,......".我用Java 1.6测试了相同的代码,但是我找不到行为上的差异.我希望Java 1.6只根据段落中给出的信息来评估这两个表达式.有没有人知道他们想用"从Java 7开始......"这么说?
编辑:为了避免混淆:归结为这个问题,因为他们编写了"从Java 7开始",从Java 6切换到Java 7时,三元运算符有什么变化吗?
根据此页面中的示例,我在下面提供了工作和非工作代码示例.
工作代码使用if声明:
if (!empty($address['street2'])) echo $address['street2'].'<br />';
Run Code Online (Sandbox Code Playgroud)
使用三元运算符的非工作代码:
$test = (empty($address['street2'])) ? 'Yes <br />' : 'No <br />';
// Also tested this
(empty($address['street2'])) ? 'Yes <br />' : 'No <br />';
Run Code Online (Sandbox Code Playgroud)
更新
在Brian的小费之后,我发现回声$test输出了预期的结果.以下作品就像一个魅力!
echo (empty($storeData['street2'])) ? 'Yes <br />' : 'No <br />';
Run Code Online (Sandbox Code Playgroud) 我是C#新手,我遇到了一个问题.处理三元运算符(? :)时,C#和Java之间存在差异.
在以下代码段中,为什么第4行不起作用?编译器显示错误消息there is no implicit conversion between 'int' and 'string'.第5行不起作用.两者List都是对象,不是吗?
int two = 2;
double six = 6.0;
Write(two > six ? two : six); //param: double
Write(two > six ? two : "6"); //param: not object
Write(two > six ? new List<int>() : new List<string>()); //param: not object
Run Code Online (Sandbox Code Playgroud)
但是,相同的代码适用于Java:
int two = 2;
double six = 6.0;
System.out.println(two > six ? two : six); //param: double
System.out.println(two > six ? two : …Run Code Online (Sandbox Code Playgroud) 我知道在php 5.3中,而不是使用这个冗余的三元运算符语法:
startingNum = startingNum ? startingNum : 1
Run Code Online (Sandbox Code Playgroud)
...我们可以在适用的情况下为我们的三元运算符使用简写语法:
startingNum = startingNum ?: 1
Run Code Online (Sandbox Code Playgroud)
我知道javascript中的三元运算符:
startingNum = startingNum ? startingNum : 1
Run Code Online (Sandbox Code Playgroud)
......但有速记吗?
多谢你们!
public string Source
{
get
{
/*
if ( Source == null ){
return string . Empty;
} else {
return Source;
}
*/
return Source ?? string.Empty;
}
set
{
/*
if ( Source == null ) {
Source = string . Empty;
} else {
if ( Source == value ) {
Source = Source;
} else {
Source = value;
}
}
*/
Source == value ? Source : value ?? string.Empty;
RaisePropertyChanged ( "Source" );
} …Run Code Online (Sandbox Code Playgroud) <?PHP
require __DIR__.'/c.php';
if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; }))
throw new Exception('Error');
$c();
?>
Run Code Online (Sandbox Code Playgroud)
Twitto使用从PHP 5.3开始提供的几个新功能:
在PHP 5.3 中,数字2对?:做什么?
另外,匿名函数是什么意思?那不是已经存在了一段时间的东西吗?
php ternary-operator language-construct conditional-operator php-5.3
我正在为嵌入式项目使用SDK.在这个源代码中,我发现了一些代码,至少我发现这些代码很奇怪.在SDK的许多地方都有这种格式的源代码:
#define ATCI_IS_LOWER( alpha_char ) ( ( (alpha_char >= ATCI_char_a) && (alpha_char <= ATCI_char_z) ) ? 1 : 0 )
#define ATCI_IS_UPPER( alpha_char ) ( ( (alpha_char >= ATCI_CHAR_A) && (alpha_char <= ATCI_CHAR_Z) ) ? 1 : 0 )
Run Code Online (Sandbox Code Playgroud)
这里使用三元运算符有什么不同吗?
是不是
#define FOO (1 > 0)
Run Code Online (Sandbox Code Playgroud)
同样的
#define BAR ( (1 > 0) ? 1 : 0)
Run Code Online (Sandbox Code Playgroud)
?
我尝试使用它来评估它
printf("%d", FOO == BAR);
Run Code Online (Sandbox Code Playgroud)
得到结果1,所以看起来它们是平等的.是否有理由像他们一样编写代码?
ternary-operator ×10
c# ×3
java ×3
javascript ×2
php ×2
.net ×1
angularjs ×1
boolean ×1
c ×1
c++ ×1
if-statement ×1
iif-function ×1
java-6 ×1
java-8 ×1
php-5.3 ×1