gpt4 book ai didi

java - 为什么我不能将 lambda 分配给未类型化的谓词引用?但是可以为它分配一个初始化的类型谓词引用吗?

转载 作者:行者123 更新时间:2023-11-29 07:26:33 25 4
gpt4 key购买 nike

我在尝试创建基于方法参数类型使用的自定义谓词引用时注意到一些奇怪的事情。

我有一个名为 AffiliateLinkSubset 的对象,它有一个名为 isGeneral 的 boolean getter。当我尝试执行以下操作时:

Predicate<?> partitionPredicate = AffiliateLinkSubset::isGeneral;

我收到错误无法从静态上下文中引用非静态方法

但是当我将泛型类型 AffiliateLinkSubset 分配给它工作的 Predicate 时,这没什么特别的。但是特别的是以下也有效:

Predicate<AffiliateLinkSubset> partitionPredicate = affiliateLinkSubset::isGeneral;
Predicate<?> test = partitionPredicate;

IDE 没有报错!即使我有效地将相同的 lambda 分配给无类型谓词测试。这怎么可能?谓词会在运行时起作用吗?我假设它会,因为在编译期间所有类型都被删除并替换为 Object 类型。这就是为什么我不明白为什么我不能为一个无类型的谓词分配一个 lambda。谁能解释一下?

AffiliateLinkSubset 是实际类的缩写,这里是:

import POJOs.PojoENUMS.LocalizedStorefront;

import java.util.Map;
import java.util.Set;

public class AffiliateLinkSubsetForStatisticsCalculation {
private Long id;
private String title;
private Double productValue;
private boolean general;
private Set<String> keywords;
private Map<String, Boolean> booleanKeywords;
private LocalizedStorefront localizedStorefront;

public AffiliateLinkSubsetForStatisticsCalculation(Long id, String title, Double productValue, boolean general, Set<String> keywords, Map<String, Boolean> booleanKeywords, LocalizedStorefront localizedStorefront) {
this.id = id;
this.title = title;
this.productValue = productValue;
this.general = general;
this.keywords = keywords;
this.booleanKeywords = booleanKeywords;
this.localizedStorefront = localizedStorefront;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Double getProductValue() {
return productValue;
}

public void setProductValue(Double productValue) {
this.productValue = productValue;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public boolean isGeneral() {
return general;
}

public void setGeneral(boolean general) {
this.general = general;
}

public Set<String> getKeywords() {
return keywords;
}

public void setKeywords(Set<String> keywords) {
this.keywords = keywords;
}

public Map<String, Boolean> getBooleanKeywords() {
return booleanKeywords;
}

public void setBooleanKeywords(Map<String, Boolean> booleanKeywords) {
this.booleanKeywords = booleanKeywords;
}

public LocalizedStorefront getLocalizedStorefront() {
return localizedStorefront;
}

public void setLocalizedStorefront(LocalizedStorefront localizedStorefront) {
this.localizedStorefront = localizedStorefront;
}
}

最佳答案

首先是 Predicate<?> predicate由于未知类型而无法工作。它们中的任何一个都可以工作:

Predicate<? extends AffiliateLinkSubset> predicate = AffiliateLinkSubset::isGeneral;
Predicate<AffiliateLinkSubset> predicate = AffiliateLinkSubset::isGeneral;

方法引用快捷方式AffiliateLinkSubset::isGeneral理解为 object -> object.isGeneral()其中 objectAffiliateLinkSubset 的一种.这就是第一个谓词无法工作的原因,因为 <?>没有定义 AffiliateLinkSubset 的类型. isGeneral() Object 中未定义方法.

让我们继续。如果您键入:

Predicate<?> partitionPredicate = object -> AffiliateLinkSubset.isGeneral();

这不会编译,因为你对待类方法 isGeneral()同样的方式是它是静态的。为此,您需要一个可以调用此方法的类的实例:

AffiliateLinkSubset affiliateLinkSubset = new AffiliateLinkSubset();
Predicate<?> partitionPredicate = object -> affiliateLinkSubset.isGeneral();

现在,objectObject 的实例结果依赖于不触及 object 的 lambda 的右侧并且输入无关紧要,因此这也可能有效:

Supplier<Boolean> supplier = () -> affiliateLinkSubset.isGeneral();

这没有多大意义,所以我猜你已经提到了解决方案:

Predicate<? extends AffiliateLinkSubset> predicate = AffiliateLinkSubset::isGeneral;

关于java - 为什么我不能将 lambda 分配给未类型化的谓词引用?但是可以为它分配一个初始化的类型谓词引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51543897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com