Reputation: 421
I try to make create multi dirs and files more easier with follwoing code:
package ro.ex;
class File {
public interface Lamb {
public void call(Class c);
}
public static void tap(Lamb l) {
l.call(File.class);
}
public static void mkdir(String path) {
}
public static void main(String[] args) {
File.tap((f) -> {
f.mkdir("dir");
f.mkdir("dir2");
});
}
}
but in f.mkdir("dir")
, intellij idea raise can't resolve method 'mkdir'
my question is: how to change code to make code in main block work well
Upvotes: 0
Views: 140
Reputation: 62864
According to the method contract, File.tap()
accepts a Lamb
parameter.
Lamb
is a functional interface (contains only one abstract method) and the body of your lambda is the anonymous implementation of its abstract method.
The abstract method has a definition public void call(Class c)
and this is why your code fails to compile. You're trying to pass a File
object, instead of a Class
one.
In the mean time, mkdir
is a static method and can be invoked like this:
public static void main(String[] args) {
File.tap((f) -> {
File.mkdir("dir");
File.mkdir("dir2");
});
}
Upvotes: 3
Reputation: 20618
You defined a (functional) interface Lamb
whose single method takes a Class
as an argument.
So the f
in your lambda expression (f) -> { ... }
is of type Class
. But this class does not know the method mkdir
.
You have a static method mkdir
in your custom class File
. Static methods are called like this:
File.tap((f) -> {
File.mkdir("dir");
File.mkdir("dir2");
});
This makes the Class
argument in method Lamb.call(Class)
rather useless. Maybe you want to reconsider the design.
Upvotes: 1