【发布时间】:2020-02-06 09:15:01
【问题描述】:
有没有办法用switch语句判断一个特定的接口是否是instanceof另一个接口?
MainInterface inter = //load interface
switch (inter) {
case instanceof Interface1:
Interface1 inter1 = (Interface1) inter;
//execute methods of inter1
case instanceof Interface2:
Interface2 inter2 = (Interface2) inter;
//execute methods of inter2
}
感谢您的帮助。
【问题讨论】:
-
还没有,但正在考虑中。还有正在考虑的解构
if(obj instanceof Interface1 i1) {至少会使 if / else if 不那么尴尬 -
不,不直接。不过,您可以使用一个枚举值,您可以根据其类型从接口计算出该值。
-
@Ivar 不幸的是这对我没有帮助:(
-
@TryHard Non of the 23 answers?
标签: java interface switch-statement instanceof