Java 8:Lambda函数和通用通配符

时间:2023-02-08 12:01:52

I have the following class

我有以下课程

class Book implement Borrowable  {
    @Override
    public String toString(Function<? extends Borrowable
                            , String> format) {
          return format.apply(this);    
    }
}

This gives me an error that i cannot use "apply" on this(Book object).

这给了我一个错误,我无法在此(Book对象)上使用“apply”。

My current formatter is

我目前的格式化程序是

 Function<Book, String> REGULAR_FORMAT = book -> "name='" + book.name + '\'' +
        ", author='" + book.author + '\'' +
        ", year=" + book.year;

I don't want to make the lambda function of the type

我不想制作该类型的lambda函数

Function<Borrowable, String>

as I would lose access to the members of Book not exposed by Borrowable.

因为我将无法访问未被Borrowable公开的Book成员。

2 个解决方案

#1


7  

The Function<? extends Borrowable, String> type means function that able to accept some type which extends Borrowable. It does not mean that it accepts Book. Probably the best solution is to introduce the generic parameter for Borrowable:

功能 type表示能够接受某些扩展Borrowable的类型的函数。这并不意味着它接受Book。可能最好的解决方案是为Borrowable引入通用参数:

public interface Borrowable<T> {
    public String toString(Function<? super T, String> format);
}

And specify it in Book:

并在Book中指定它:

public class Book implements Borrowable<Book> {
    @Override
    public String toString(Function<? super Book, String> format) {
        return format.apply(this);
    }
}

It's similar to how the Comparable interface works.

它类似于Comparable接口的工作方式。

#2


2  

You might be looking for Function<? super Book, String>.

您可能正在寻找功能 。

A Function<Book, String> is a valid Function<? extends Borrowable, String>, but so is a Function<DVD, String>. Your method (toString) might be called with a Function<DVD, String>, which you can't pass this to because this isn't a DVD!

函数 是一个有效的函数 ,但函数 也是如此。您的方法(toString)可能会使用Function 调用,您无法将其传递给它,因为这不是DVD! ,string> ,string> ,string>

Change the argument type to Function<? super Book, String>, perhaps.

将参数类型更改为Function 。

#1


7  

The Function<? extends Borrowable, String> type means function that able to accept some type which extends Borrowable. It does not mean that it accepts Book. Probably the best solution is to introduce the generic parameter for Borrowable:

功能 type表示能够接受某些扩展Borrowable的类型的函数。这并不意味着它接受Book。可能最好的解决方案是为Borrowable引入通用参数:

public interface Borrowable<T> {
    public String toString(Function<? super T, String> format);
}

And specify it in Book:

并在Book中指定它:

public class Book implements Borrowable<Book> {
    @Override
    public String toString(Function<? super Book, String> format) {
        return format.apply(this);
    }
}

It's similar to how the Comparable interface works.

它类似于Comparable接口的工作方式。

#2


2  

You might be looking for Function<? super Book, String>.

您可能正在寻找功能 。

A Function<Book, String> is a valid Function<? extends Borrowable, String>, but so is a Function<DVD, String>. Your method (toString) might be called with a Function<DVD, String>, which you can't pass this to because this isn't a DVD!

函数 是一个有效的函数 ,但函数 也是如此。您的方法(toString)可能会使用Function 调用,您无法将其传递给它,因为这不是DVD! ,string> ,string> ,string>

Change the argument type to Function<? super Book, String>, perhaps.

将参数类型更改为Function 。