@SpyBean
fromorg.springframework.boot.test.mock.mockito.SpyBean
和@Spy
from和有什么不一样org.mockito.Spy
?
使用@SpyBean
而不是@Spy
使我的测试失败。
根据以下源代码,似乎常规lambda可以与扩展lambdas互换.
fun main(args: Array<String>) {
val numbers = listOf(1, 2, 3)
filter(numbers, predicate)
filter(numbers, otherPredicate)
println("PREDICATE: ${predicate} " +
"\nOTHERPREDICATE: ${otherPredicate} " +
"\nEQUALITY: ${predicate==otherPredicate}")
}
val predicate : Int.() -> Boolean = {this % 2 != 0}
val otherPredicate : (Int) -> Boolean = {it % 2 != 0}
fun filter(list: List<Int>, predicate:(Int) -> Boolean) {
for(number in list){
if(predicate(number)){
println(number)
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出(我关心),如下:
PREDICATE: kotlin.Int.() -> kotlin.Boolean
OTHERPREDICATE: (kotlin.Int) -> kotlin.Boolean
EQUALITY: false
Run Code Online (Sandbox Code Playgroud)
问题是为什么这些lambdas可以互换?不应该是不同的东西?编译器是否在做一些"聪明"的事情?
在以下源代码中
fun main(args: Array<String>) {
println("Hello, world!")
val mutableIntList = mutableListOf(1, 2, 3)
addInt(4, mutableIntList) // No compile-time error
addAnotherInt(5, mutableIntList) // Compile-time error
println(mutableIntList)
}
fun <T: Number> addInt(item:T,
list:MutableList<in T>){
list.add(item)
}
fun <T: Number> addAnotherInt(item:T,
list:MutableList<in Number>){
list.add(item)
}
Run Code Online (Sandbox Code Playgroud)
的功能addInt
和addAnotherInt
采取作为自变量的逆变MutableList
的Number
.但是在main
功能上,一行正常编译而另一行则不正常.
我还检查了这些函数生成的java代码,它们看起来完全相同.
可能是什么功能之间的差异addInt
和addAnotherInt
?
我试图理解反思.我有以下代码:
fun main(args: Array) {
println(lengthL1())
println(lengthL2(s))
println(lengthL1.get()) // Error
println(lengthL2.get(s)) // Error
println(lengthNL1.get())
println(lengthNL2.get(s))
println(lengthNL1())
println(lengthNL2(s))
}
val s = “1234”
val lengthL1: () -> Int = s::length
val lengthL2: (String) -> Int = String::length
val lengthNL1 = s::length
val lengthNL2 = String::length
Run Code Online (Sandbox Code Playgroud)
为什么我get
在声明lambda时不能调用(参见错误注释)?有什么区别lengthL1
和lenghtNL1
?
根据清单12.3中的Java Concurrency in Practice书,我们可以使用以下示例代码测试并发代码:
void testTakeBlocksWhenEmpty() {
final BoundedBuffer<Integer> bb = new BoundedBuffer<Integer>(10);
Thread taker = new Thread() {
public void run() {
try {
int unused = bb.take();
fail(); // if we get here, it’s an error
} catch (InterruptedException success) { }
}
};
try {
taker.start();
Thread.sleep(LOCKUP_DETECT_TIMEOUT);
taker.interrupt();
taker.join(LOCKUP_DETECT_TIMEOUT);
assertFalse(taker.isAlive());
} catch (Exception unexpected) {
fail();
}
}
Run Code Online (Sandbox Code Playgroud)
假设执行了以下步骤:
taker
线程启动。bb.take()
成功返回,我们只需要一点点fail()
方法就可以运行。interrupt()
方法。catch
障碍中taker
。因此,我们目前处于困境,但实际上测试方法失败了。它失败了,我们从不知道。
这是正确的吗?如果是,我们该如何解决?
我试图理解函数的正确使用(运行、使用、让、也应用)。假设我们有以下初始代码(我将它用于测试目的):
con = urlGet.openConnection() as HttpURLConnection
con.readTimeout = 10000
con.connectTimeout = 2000
con.requestMethod = "GET"
con.doInput = true
con.connect()
inst = con.inputStream
Run Code Online (Sandbox Code Playgroud)
根据这张图片,我将其修改为:
con = urlGet.openConnection() as HttpURLConnection
inputStream = con.run {
readTimeout = 10000
connectTimeout = 2000
requestMethod = "GET"
doInput = true
// Start the query
connect()
inputStream
}
Run Code Online (Sandbox Code Playgroud)
但根据我发现的一些指南,我认为我在那里做多项“工作”。
所以,我觉得这更正确:
con = urlGet.openConnection() as HttpURLConnection
con.apply {
readTimeout = 10000
connectTimeout = 2000
requestMethod = "GET"
doInput = true
} …
Run Code Online (Sandbox Code Playgroud) 我有一个示例项目。build.gradle(项目)是:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
id "org.jlleitschuh.gradle.ktlint" version '10.3.0' apply true
}
subprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent
// Optionally configure plugin
ktlint {
debug = true
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
build.gradle(应用程序)是:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id …
Run Code Online (Sandbox Code Playgroud) 假设我们有以下内容:
val person = "Bill"
Run Code Online (Sandbox Code Playgroud)
有人可以解释这两者之间的区别:
val kClass1 = person.javaClass.kotlin
Run Code Online (Sandbox Code Playgroud)
VS
val kClass2 = person::class
Run Code Online (Sandbox Code Playgroud)
当我应该打电话给那个而不是另一个?
任何源代码示例将不胜感激.
除了Java继承是该语言的基本特征之外,我还有一些问题.
以下是我的测试示例的来源:
class MyClass{
public void say(String t){
System.out.println("Hello MyClass "+t);
}
public void print(MyClass t){
System.out.println("MyClass is printed");
}
public void anotherPrint(int i){
System.out.println("MyClass is printed again");
}
}
class MyClass2 extends MyClass{
public void say(String t){
System.out.println("Hello MyClass2 "+t);
}
public void print(MyClass2 t){
System.out.println("MyClass2 is printed");
}
public void anotherPrint(double i){
System.out.println("MyClass2 is printed again");
}
}
public class HelloWorld{
public static void main(String []args){
MyClass klass = new MyClass2();
klass.say("h"); //Question 1 (Prints: "Hello MyClass2 h") …
Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关的说明blockingSubscribe()
,subscribe()
但是我既不能写也找不到示例来查看它们之间的区别。看来这两种方式都是相同的。有人可以提供这两个示例,最好是用Java。
根据第二个文档表(http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html),没有标识符的成员对子类不可见.
但是,当我运行以下示例代码时,会打印"1"(b的内容)!
class Class1{
private int a=0;
int b=1;
protected int c=2;
public int d=3;
}
class Class2 extends Class1{ }
public class HelloWorld{
public static void main(String []args){
Class2 klass=new Class2();
System.out.println(klass.b);
}
}
Run Code Online (Sandbox Code Playgroud)
如果无法从子类访问没有访问修饰符的成员,为什么在此示例中打印它?
它应该抛出一个错误,就像私有访问修饰符一样,不应该吗?
我正在尝试运行这个例子
fun main() {
val mlist: MutableList<String> = mutableListOf<String>()
val mapp: MutableMap<Int, MutableList<String>> = mutableMapOf(1 to mlist)
println(mapp)
mapp[1].add("a") // Correct is mapp[1]?.add("a")
println(mapp)
}
Run Code Online (Sandbox Code Playgroud)
编译器正在抱怨 Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type MutableList<String>?
.
尽管我已经定义了MutableList<String>
为什么编译器会说有关MutableList<String>?
.
什么编译器比我更懂?!我在这里缺少什么?
据我所知,双数字在内存中保留了64位.浮点数保留32位.
我尝试了以下方法:
double result=0.1f+0.3f;
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
结果它打印出来0.4000000059604645
.来自下一个存储器块的数据(32个第一位和下一个32位)?
还有别的吗?
kotlin ×8
java ×6
lambda ×2
android ×1
blocking ×1
concurrency ×1
double ×1
generics ×1
githooks ×1
gradle ×1
inheritance ×1
ktlint ×1
methods ×1
overriding ×1
reflection ×1
rx-java2 ×1
spring ×1
spy ×1
testing ×1
unit-testing ×1