如何在java 8中使用lambda表达式覆盖基类方法? [英] How can I override a base class method using a lambda expression in java 8?

查看:748
本文介绍了如何在java 8中使用lambda表达式覆盖基类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须将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.

我有 java。 nio.file.SimpleFileVisitor< Path> 作为基类,我想覆盖它的方法,但我希望在另一个方法中这样做。我可以用匿名类这样做:

I have java.nio.file.SimpleFileVisitor<Path> 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<Path> visitor = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
            System.out.println(file);
            return super.visitFile(file, attrs);
        }
    };
    Files.walkFileTree(path, visitor);
}

有没有办法在lambda的帮助下删除那些代码?

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

我认为lambda将是(f) - > System.out.println(f);

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

我想忘记SimpleFileVisitor并使用默认方法创建一个等效的接口,但我怎么能选择覆盖什么方法?我是否需要在没有默认实现的情况下保留我想覆盖的方法?在这种情况下,我需要使用不同的未实现方法的不同情况的几个接口。

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.

谢谢。

推荐答案

使用委派。对于此任务,您需要一个必须只实现一次的辅助类:

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

interface IoBiFunction<T, U, R> {
  R apply(T t, U u) throws IOException;
}
class LambdaFileVisitor<T> extends SimpleFileVisitor<T> {
    IoBiFunction<T, BasicFileAttributes, FileVisitResult> preVisitDir=super::preVisitDirectory;
    IoBiFunction<T, BasicFileAttributes, FileVisitResult> visitFile=super::visitFile;
    IoBiFunction<T, IOException, FileVisitResult> visitFailed=super::visitFileFailed;
    IoBiFunction<T, IOException, FileVisitResult> postVisitDir=super::postVisitDirectory;

    public LambdaFileVisitor<T> onVisitFile(IoBiFunction<T, BasicFileAttributes, FileVisitResult> f) {
        this.visitFile = Objects.requireNonNull(f);
        return this;
    }
    public LambdaFileVisitor<T> onVisitFailed(IoBiFunction<T, IOException, FileVisitResult> f) {
        this.visitFailed = Objects.requireNonNull(f);
        return this;
    }
    public LambdaFileVisitor<T> onPreVisitDir(IoBiFunction<T, BasicFileAttributes, FileVisitResult> f) {
        this.preVisitDir = Objects.requireNonNull(f);
        return this;
    }
    public LambdaFileVisitor<T> onPostVisitDir(IoBiFunction<T, IOException, FileVisitResult> 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);
    }
}

一旦你有了帮助类,你可以一起使用它使用lambda表达式,例如

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

FileVisitor<Path> fv=new LambdaFileVisitor<Path>()
  .onVisitFile((f,a)->{System.out.println(f); return CONTINUE; })
  .onVisitFailed((f,e)->{ throw e; });

FileVisitor<Path> fv2=new LambdaFileVisitor<Path>()
  .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;
  });

这篇关于如何在java 8中使用lambda表达式覆盖基类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆