且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编写带有嵌套模板参数的类?

更新时间:2022-04-26 21:57:25

这是因为您没有在此处指定 Parent 的类型参数:

This is because you didn't specify the type parameter of Parent here:

Foo<Parent, String> foo = new Foo<>(stringParent, "bonjour");
Foo<Child, Integer> childFoo = new Foo<>(intChildBar, 42);

Object b = foo.parent.b;

如果您指定 Parent 的类型参数,则代替 Foo< Parent,String> ,即 Foo< Parent< String>,String> ,那么您可以获得正确的 b 类型:

Instead of Foo<Parent, String>, if you specify the type parameter of Parent, that is Foo<Parent<String>, String>, then you can get the correct type of b:

Foo<Parent<String>, String> foo = new Foo<>(stringParent, "bonjour");
Foo<Child, Integer> childFoo = new Foo<>(intChildBar, 42);

String b = foo.parent.b;