给定函数foo:
fun foo(m: String, bar: (m: String) -> Unit) {
bar(m)
}
Run Code Online (Sandbox Code Playgroud)
我们可以做的:
foo("a message", { println("this is a message: $it") } )
//or
foo("a message") { println("this is a message: $it") }
Run Code Online (Sandbox Code Playgroud)
现在,假设我们有以下功能:
fun buz(m: String) {
println("another message: $m")
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将"buz"作为参数传递给"foo"?就像是:
foo("a message", buz)
Run Code Online (Sandbox Code Playgroud) 我相信我们都知道setUp(@Before)将在任何测试方法之前执行,而tearDown(@After)将在测试方法之后执行.
我们也知道Junit将为每个测试方法创建一个Test实例.
我的问题是,我们可以将setUp方法内容移动到类Constructor并删除setUp方法吗?是否有任何特定的理由保持setUp方法?
给予以下Kotlin类:
class Foo {
public fun bar(i: Int = 0): Int = 2 * i
}
Run Code Online (Sandbox Code Playgroud)
如何在没有java/groovy代码的任何参数的情况下调用'bar'函数?
def f = new Foo()
f.bar() //throws: java.lang.IllegalArgumentException: Parameter specified as non-null contains null
Run Code Online (Sandbox Code Playgroud) 根据Java Annotation API:
RetentionPolicy.CLASS注释将由编译器记录在类文件中,但在运行时不需要由VM保留.
RetentionPolicy.RUNTIME注释将由编译器记录在类文件中,并在运行时由VM保留,因此可以反射性地读取它们.
我正在寻找"CLASS"保留政策的样本.当我们需要使用此策略而不是RUNTIME策略时.
我需要检查传递给Windows批处理文件的参数是否为数字.
我发现下面的答案是使用带有正则表达式的"findstr"命令:https: //superuser.com/questions/404338/check-for-only-numerical-input-in-batch-file
我确实尝试过这个解决方案,但它没有用.(至少在Windows 7中不起作用).
测试场景:
AA #NOT A VALID NUMBER
A1 #NOT A VALID NUMBER
1A #NOT A VALID NUMBER
11 #A VALID NUMBER
Run Code Online (Sandbox Code Playgroud) 我有一个maven模块,有2个配置文件profile-a和profile-b
profile-a可以独立使用,但profile-b应该与profile-a一起运行
mvn install -P profile-a // valid
mvn install -P profile-a,profile-b // valid
mvn install -P profile-b // INVALID
Run Code Online (Sandbox Code Playgroud)
反正是为了确保用户不能只使用profile-b安装模块?如果profile-b单独使用,还是自动激活配置文件-a?
我有一个以下格式的二进制文件:
[N bytes identifier & record length] [n1 bytes data]
[N bytes identifier & record length] [n2 bytes data]
[N bytes identifier & record length] [n3 bytes data]
Run Code Online (Sandbox Code Playgroud)
如你所见,我有不同长度的记录.在每个记录中,我有N个字节固定,其中包含和id以及记录中的数据长度.
这个文件很大,可以包含3百万条记录.
我想通过应用程序打开此文件,让用户浏览和编辑记录.(插入/更新/删除记录)
我的初步计划是从原始文件创建和索引文件,并为每条记录保留下一个和上一个记录地址,以便轻松地前后导航.(某种链表,但文件不在内存中)
是否有库(java库)来帮助我实现这个要求?
您认为有用的任何推荐或经验?
-----------------编辑-------------------------------- --------------
感谢您的指导和建议,
更多信息:
原始文件及其格式不受我的控制(它是第三方文件),我无法更改文件格式.但我必须阅读它,让用户浏览记录并编辑其中一些(插入新记录/更新现有记录/删除记录),最后将其保存回原始文件格式.
你还推荐使用DataBase而不是普通的索引文件吗?
-----------------第二次编辑------------------------------- ---------------
更新模式下的记录大小是固定的.它表示更新(编辑)的记录与原始记录的长度相同,除非用户删除记录并创建不同格式的另一条记录.
非常感谢
鉴于以下groovy功能:
def foo(List<String> params, Closure c) {...}
Run Code Online (Sandbox Code Playgroud)
方法调用将是:
foo(['a', 'b', 'c']) { print "bar" }
Run Code Online (Sandbox Code Playgroud)
但我想在函数调用中摆脱括号(List).就像是:
foo('a', 'b') { print "bar" }
Run Code Online (Sandbox Code Playgroud)
我无法将list参数更改为varargs,因为varargs只能是函数中的最后一个参数(这里的闭包是最后一个参数).
有什么建议吗?
有没有办法在以下示例中删除变量"i",仍然可以访问正在打印的项目的索引?
def i = 0;
"one two three".split().each {
println ("item [ ${i++} ] = ${it}");
}
Run Code Online (Sandbox Code Playgroud)
===============编辑================
我发现一种可能的解决方案是使用"eachWithIndex"方法:
"one two three".split().eachWithIndex {it, i
println ("item [ ${i} ] = ${it}");
}
Run Code Online (Sandbox Code Playgroud)
如果有其他解决方案,请告诉我.
我有跟随build.gradle的构建错误.
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.7
version = '1.0'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.foo.group:my-artifact:0.0.1-final'
}
Run Code Online (Sandbox Code Playgroud)
组,工件和版本都是正确的.我已经尝试了maven项目并且构建成功,但在gradle项目中它给了我以下错误:
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':testRuntime'.
> Could not find org.foo.group:my-artifact:0.0.1-final.
Required by:
:my-gradle-project:1.0
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get …Run Code Online (Sandbox Code Playgroud) java ×3
groovy ×2
kotlin ×2
maven ×2
annotations ×1
batch-file ×1
binaryfiles ×1
closures ×1
dependencies ×1
file-io ×1
gradle ×1
junit ×1
junit3 ×1
junit4 ×1
maven-2 ×1
profiles ×1