java lambda 重写方法,如何在java 8中使用lambda表达式覆盖基类方法?

Lambda expressions must be cast to a functional interface. They cannot extend a class as far as I know but I want to know if there is a way to get something similar.

I have java.nio.file.SimpleFileVisitor as base class and I want to override a method of it but I wish to do so inside another method. I can do it with an anonymous class this way:

public static void printContent(Path path) throws IOException {

FileVisitor visitor = new SimpleFileVisitor() {

@Override

public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

throws IOException {

System.out.println(file);

return super.visitFile(file, attrs);

}

};

Files.walkFileTree(path, visitor);

}

Is there a way to remove that load of code with the help of a lambda?

I think the lambda would be (f) -> System.out.println(f);

I thought about forgetting SimpleFileVisitor and creating a equivalent interface with default methods but, how could I select what method to override? Would I need to leave the method I want to override without default implementation? In that case I would need several Interfaces for different cases with different not implemented methods.

Thank you.

解决方案

Use Delegation. For this task you need a helper class that has to be implemented only once:

interface IoBiFunction {

R apply(T t, U u) throws IOException;

}

class LambdaFileVisitor extends SimpleFileVisitor {

IoBiFunction preVisitDir=super::preVisitDirectory;

IoBiFunction visitFile=super::visitFile;

IoBiFunction visitFailed=super::visitFileFailed;

IoBiFunction postVisitDir=super::postVisitDirectory;

public LambdaFileVisitor onVisitFile(IoBiFunction f) {

this.visitFile = Objects.requireNonNull(f);

return this;

}

public LambdaFileVisitor onVisitFailed(IoBiFunction f) {

this.visitFailed = Objects.requireNonNull(f);

return this;

}

public LambdaFileVisitor onPreVisitDir(IoBiFunction f) {

this.preVisitDir = Objects.requireNonNull(f);

return this;

}

public LambdaFileVisitor onPostVisitDir(IoBiFunction f) {

this.postVisitDir = Objects.requireNonNull(f);

return this;

}

@Override

public FileVisitResult visitFile(T file, BasicFileAttributes attrs) throws IOException {

return visitFile.apply(file, attrs);

}

@Override

public FileVisitResult visitFileFailed(T file, IOException exc) throws IOException {

return visitFailed.apply(file, exc);

}

@Override

public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) throws IOException {

return preVisitDir.apply(dir, attrs);

}

@Override

public FileVisitResult postVisitDirectory(T dir, IOException exc) throws IOException {

return postVisitDir.apply(dir, exc);

}

}

Once you have your helper class you can use it together with lambda expressions, e.g.

FileVisitor fv=new LambdaFileVisitor()

.onVisitFile((f,a)->{System.out.println(f); return CONTINUE; })

.onVisitFailed((f,e)->{ throw e; });

or

FileVisitor fv2=new LambdaFileVisitor()

.onPreVisitDir((f,a)->{System.out.println("ENTER "+f); return CONTINUE; })

.onPostVisitDir((f,e)->{

System.out.println("LEAVE "+f);

if(e!=null) throw e; else return CONTINUE;

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值