在Kotlin上,功能参数在定义方法时需要使用thire类型注释.
就我而言,我有两个来自界面的类.
interface Base{
fun method()
}
class DervA():Base{
fun override method(){
...
}
}
class DervB():Base{
fun override method(){
...
}
}
Run Code Online (Sandbox Code Playgroud)
并且,我希望从其他函数调用他们的方法
fun test_method(inst){
inst.method()
}
Run Code Online (Sandbox Code Playgroud)
但是,Kotlin编译器抱怨"值参数需要类型注释".
我应该为每个类定义"test_method"吗?
fun test_method_for_DervA(inst:DervA){
inst.method()
}
fun test_method_for_DervB(inst:DervB){
inst.method()
}
Run Code Online (Sandbox Code Playgroud)
你有更聪明的方法吗?
我想获取一系列值,例如Python中的range函数。
# Python range
a = range(0,1.0,0.01)
b = range(5,10,2)
Run Code Online (Sandbox Code Playgroud)
在Java中,希望得到List这样的结果。
// Java range
public class range{
private double start;
private double end;
private double step;
public range(double start,double end, double step) {
this.start = start;
this.end = end;
this.step = step;
}
public List<Double> asList(){
List<Double> ret = new ArrayList<Double>();
for(double i = this.start;i <= this.end; i += this.step){
ret.add(i);
}
return ret;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我认为当需要一定范围的Integer时,此代码可以计算出冗余。
您能用Java更聪明的方法或实现吗?