- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试创建基于方法参数类型使用的自定义谓词引用时注意到一些奇怪的事情。
我有一个名为 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()
其中 object
是 AffiliateLinkSubset
的一种.这就是第一个谓词无法工作的原因,因为 <?>
没有定义 AffiliateLinkSubset
的类型. isGeneral()
Object
中未定义方法.
让我们继续。如果您键入:
Predicate<?> partitionPredicate = object -> AffiliateLinkSubset.isGeneral();
这不会编译,因为你对待类方法 isGeneral()
同样的方式是它是静态的。为此,您需要一个可以调用此方法的类的实例:
AffiliateLinkSubset affiliateLinkSubset = new AffiliateLinkSubset();
Predicate<?> partitionPredicate = object -> affiliateLinkSubset.isGeneral();
现在,object
是 Object
的实例结果依赖于不触及 object
的 lambda 的右侧并且输入无关紧要,因此这也可能有效:
Supplier<Boolean> supplier = () -> affiliateLinkSubset.isGeneral();
这没有多大意义,所以我猜你已经提到了解决方案:
Predicate<? extends AffiliateLinkSubset> predicate = AffiliateLinkSubset::isGeneral;
关于java - 为什么我不能将 lambda 分配给未类型化的谓词引用?但是可以为它分配一个初始化的类型谓词引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51543897/
我是 Spring 新手,这就是我想要做的事情: 我正在使用一个基于 Maven 的库,它有自己的 Spring 上下文和 Autowiring 字段。 它的bean配置文件是src/test/res
我在我的测试脚本中有以下列表初始化: newSequenceCore=["ls", "ns", "*", "cm", "*", "ov", "ov", "ov", "ov", "kd"] (代表要在控
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Class construction with initial values 当我查看 http://en.
我得到了成员变量“objectCount”的限定错误。编译器还返回“ISO C++ 禁止非常量静态成员的类内初始化”。这是主类: #include #include "Tree.h" using n
我有如下所示的a.h class A { public: void doSomething()=0; }; 然后我有如下所示的b.h #include "a.h" class b: publi
我需要解析 Firebase DataSnapshot (一个 JSON 对象)转换成一个数据类,其属性包括 enum 和 list。所以我更喜欢通过传递 DataSnapshot 来手动解析它进入二
我使用 JQuery 一段时间了,我总是使用以下代码来初始化我的 javascript: $(document).ready( function() { // Initalisation logic
这里是 Objective-C 菜鸟。 为什么会这样: NSString *myString = [NSString alloc]; [myString initWithFormat:@"%f", s
我无法让核心数据支持的 NSArrayController 在我的代码中正常工作。下面是我的代码: pageArrayController = [[NSArrayController alloc] i
我对这一切都很陌生,并且无法将其安装到我的后端代码中。它去哪里?在我的页脚下面有我所有的 JS? 比如,这是什么意思: Popup initialization code should be exec
这可能是一个简单的问题,但是嘿,我是初学者。 所以我创建了一个程序来计算一些东西,它目前正在控制台中运行。我决定向其中添加一个用户界面,因此我使用 NetBeans IDE 中的内置功能创建了一个 J
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
据我所知, dependentObservable 在声明时会进行计算。但如果某些值尚不存在怎么办? 例如: var viewModel ={}; var dependentObservable1 =
我正在阅读 POODR 这本书,它使用旧语法进行默认值初始化。我想用新语法实现相同的功能。 class Gear attr_reader :chainring, :cog, :wheel de
我按照 polymer 教程的说明进行操作: https://www.polymer-project.org/3.0/start/install-3-0 (我跳过了可选部分) 但是,在我执行命令“po
很抱歉问到一个非常新手的Kotlin问题,但是我正在努力理解与构造函数和初始化有关的一些东西。 我有这个类和构造函数: class TestCaseBuilder constructor(
假设我们有一个包含 30 列和 30 行的网格。 生命游戏规则简而言之: 一个小区有八个相邻小区 当一个细胞拥有三个存活的相邻细胞时,该细胞就会存活 如果一个细胞恰好有两个或三个活的相邻细胞,那么它就
我是 MQTT 和 Android 开放附件“AOA” 的新手。在阅读教程时,我意识到,在尝试写入 ByteArrayOutputStream 类型的变量之前,应该写入 0 或 0x00首先到该变量。
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
我有一个inotify /内核问题。我正在使用“inotify” Python项目进行观察,但是,我的问题仍然是固有的关于inotify内核实现的核心。 Python inotify项目处理递归ino
我是一名优秀的程序员,十分优秀!