我正在玩Java 8,以了解如何作为一等公民的功能.我有以下代码段:
package test;
import java.util.*;
import java.util.function.*;
public class Test {
public static void myForEach(List<Integer> list, Function<Integer, Void> myFunction) {
list.forEach(functionToBlock(myFunction));
}
public static void displayInt(Integer i) {
System.out.println(i);
}
public static void main(String[] args) {
List<Integer> theList = new ArrayList<>();
theList.add(1);
theList.add(2);
theList.add(3);
theList.add(4);
theList.add(5);
theList.add(6);
myForEach(theList, Test::displayInt);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是使用方法引用displayInt将方法传递给方法myForEach.编译器会产生以下错误:
src/test/Test.java:9: error: cannot find symbol
list.forEach(functionToBlock(myFunction));
^
symbol: method functionToBlock(Function<Integer,Void>)
location: class Test
src/test/Test.java:25: error: method myForEach in class Test cannot be applied …Run Code Online (Sandbox Code Playgroud) 我花了一些时间阅读Typescript语言规范,并对内部和外部模块之间的区别感到有些困惑.以下是直接从规范中进行的描述:
内部模块(第9.2.2节)是其他模块的本地或导出成员(包括全局模块和外部模块).使用指定其名称和正文的ModuleDeclarations声明内部模块.具有多个标识符的名称路径等同于一系列嵌套的内部模块声明.
外部模块(第9.4节)是使用外部模块名称引用的单独加载的代码体.外部模块被编写为包含至少一个导入或导出声明的单独源文件.此外,可以使用全局模块中的AmbientModuleDeclarations声明外部模块,该模块直接将外部模块名称指定为字符串文字.这将在第0节中进一步描述.
根据我的理解,我认为外部模块对应于打字稿文件,而不包含简单地导出一组类型和/或变量的模块定义.从另一个打字稿文件,我可以简单的导入在外部模块foo.ts与import foo = module("foo");
有人可以向我解释外部和内部模块之间的意图吗?
我在typescript中定义了以下接口:
interface MyInterface {
() : string;
}
Run Code Online (Sandbox Code Playgroud)
该接口简单地引入了一个不带参数的调用签名并返回一个字符串.如何在类中实现此类型?我尝试过以下方法:
class MyType implements MyInterface {
function () : string {
return "Hello World.";
}
}
Run Code Online (Sandbox Code Playgroud)
编译器一直告诉我
类'MyType'声明接口'MyInterface'但没有实现它:Type'MyInterface'需要一个调用签名,但Type'MyType'缺少一个
如何实现呼叫签名?