是否有类似ActionScript3';s";加上;JAVA中的关键字?
在从AS3切换到JAVA的过程中,我发现有一些细微之处我没有注意到,其中一个就是关键字。 JAVA中是否有类似的东西(6或更少) 我正在分析一个对象的值,并希望编写如下内容:是否有类似ActionScript3';s";加上;JAVA中的关键字?,java,actionscript-3,keyword,Java,Actionscript 3,Keyword,在从AS3切换到JAVA的过程中,我发现有一些细微之处我没有注意到,其中一个就是关键字。 JAVA中是否有类似的东西(6或更少) 我正在分析一个对象的值,并希望编写如下内容: /** Example in AS3 **/ function FancyParsingContructorInAS3(values:Object):void { with (values) { this.x = x; // functionally equivalent to `this.x
/** Example in AS3 **/
function FancyParsingContructorInAS3(values:Object):void {
with (values) {
this.x = x; // functionally equivalent to `this.x = values.x;`
this.y = y;
this.width = width;
this.height = height;
}
}
with
语句似乎有两个用例
1) 在特定上下文中更改此的值
public class App {
public void stuff() {
//this refers to the current instance of App
final Object obj = new Object();
with(obj) {
//this refers to obj
}
}
}
Java中没有这样的东西。您只需将方法代码放入对象
2) 从另一个类
导入方法,以便可以使用它们
public static void main(String[] args) throws Exception {
System.out.println(Math.max(Math.PI, 3));
}
可以更改,在java中使用导入静态
。这允许将另一个类的静态成员导入当前类
。添加以下三个导入语句:
import static java.lang.System.out;
import static java.lang.Math.max;
import static java.lang.Math.PI;
会允许你改为写作
public static void main(String[] args) throws Exception {
out.println(max(PI, 3));
}
您可以看到,这使得代码的可读性有所降低。在out
、max
和PI
的来源地并不特别明显。在我看来,你应该远离这些“细节”,因为它们会降低代码的可读性。而且速度会慢得多!哇,那太可怕了,但很迷人!看起来Kotlin
实现了此功能,以匹配操作脚本的执行方式@Blake它不仅仅是Kotlin添加的扩展函数,它允许他们用
实现。我不知道它在ActionScript中是如何工作的,但在Kotlin中,它只是一个实用方法,它将lambda作为传入对象的扩展函数来执行。事实上,它是Kotlin中6个“范围函数”之一。