微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何引用内部类成员的泛型类型?

如何解决如何引用内部类成员的泛型类型?

我很好奇为什么 $(ELF) : $(OBjdiR)/$(OBJS) $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) # compile and generate dependency info $(OBjdiR)/%.o: $(SRCDIR)/%.c $(CC) -c $(CFLAGS) $< -o $@ $(CC) -MM $(CFLAGS) $< > $*.d %.o: %.s $(CC) -c $(CFLAGS) $< -o $@ 无法编译,引用:

不兼容的类型:对象不能转换为字符串。

testInnerClass

import java.util.List; class Test< I extends Test.InnerClass,S extends Test.StaticInnerClass,O extends OtherClass> { void testOtherClass(O other) { String firstString = other.strings.get(0); //this works } void testStaticInnerClass(S staticInner) { String firstString = staticInner.strings.get(0); //this works } void testInnerClass(I inner) { String firstString = inner.strings.get(0); //this fails: //"incompatible types: Object cannot be converted to String" } static class StaticInnerClass { List<String> strings; } class InnerClass { List<String> strings; } } class OtherClass { List<String> strings; } testStaticInnerClass 按我的预期工作,但我不确定为什么 testOtherClass 会失败。

解决方法

InnerClassTest 的内部类,它需要通用参数。 所以你需要将类声明更新为:

class Test<
        I extends Test<I,S,O>.InnerClass,S extends Test.StaticInnerClass,O extends OtherClass>

StaticInnerClass,即使在 Test 内部,它被声明为 static。所以作为每个 static 方法或变量,static 类也不取决于班级的任何状态。因此不需要S extends Test<I,O>.StaticInnerClass

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。