Java 从getter返回泛型集合
从getter方法返回泛型集合时出现问题。我的班级是这样的:Java 从getter返回泛型集合,java,generics,collections,Java,Generics,Collections,从getter方法返回泛型集合时出现问题。我的班级是这样的: public abstract class ContentGroup<E extends Content> extends Content { private List<E> childContents = new LinkedList<E>(); public List<E> getChildContents() { return childCont
public abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
...
}
public class Container extends ContentGroup {
}
class Content {
}
class ExtendedContent extends Content {
void foo() {
}
}
abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
}
class ExtendedContentGroup extends ContentGroup<ExtendedContent> {
}
public class Toto {
public <E extends Content> E toto(ContentGroup<E> contentGroup) {
return contentGroup.getChildContents().get(0);
}
public void bar() {
ExtendedContent extendedContent = toto(new ExtendedContentGroup());
extendedContent.foo();
}
}
编辑
我更新了代码以更好地反映实际实现。其中一个答案表明,问题在于容器没有定义类型。问题通过以下方式解决:
public class Container extends ContentGroup<Content> {
}
公共类容器扩展ContentGroup{
}
您将泛型与
看看方法参数,它不是泛型的
public void do(ContentGroup contentGroup) { // RAW type is used
它也应该是泛型的,否则在运行时您将在强制转换时遇到异常。您将泛型与
看看方法参数,它不是泛型的
public void do(ContentGroup contentGroup) { // RAW type is used
它也应该是泛型的,否则在运行时您将在强制转换时遇到异常。您将泛型与
看看方法参数,它不是泛型的
public void do(ContentGroup contentGroup) { // RAW type is used
它也应该是泛型的,否则在运行时您将在强制转换时遇到异常。您将泛型与
看看方法参数,它不是泛型的
public void do(ContentGroup contentGroup) { // RAW type is used
它也应该是通用的,否则在运行时您在强制转换时会遇到异常。要完成user3218114的回答,您应该执行以下操作:
public abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
...
}
public class Container extends ContentGroup {
}
class Content {
}
class ExtendedContent extends Content {
void foo() {
}
}
abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
}
class ExtendedContentGroup extends ContentGroup<ExtendedContent> {
}
public class Toto {
public <E extends Content> E toto(ContentGroup<E> contentGroup) {
return contentGroup.getChildContents().get(0);
}
public void bar() {
ExtendedContent extendedContent = toto(new ExtendedContentGroup());
extendedContent.foo();
}
}
课程内容{
}
类ExtendedContent扩展内容{
void foo(){
}
}
抽象类ContentGroup扩展了内容{
private List childContents=new LinkedList();
公共列表getChildContents(){
返回内容;
}
}
类ExtendedContentGroup扩展ContentGroup{
}
公共课托托{
公共电子托托(ContentGroup ContentGroup){
返回contentGroup.getChildContents().get(0);
}
公共空白栏(){
ExtendedContent ExtendedContent=toto(新的ExtendedContentGroup());
extendedContent.foo();
}
}
还请注意,我确实收到了一个编译错误,因为在Java中,do是一个保留字(可能来自我的IDE设置,但不是100%确定),所以我怀疑您应该重命名您的函数(在我的情况下是toto)。要完成user3218114的回答,您应该执行以下操作:
public abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
...
}
public class Container extends ContentGroup {
}
class Content {
}
class ExtendedContent extends Content {
void foo() {
}
}
abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
}
class ExtendedContentGroup extends ContentGroup<ExtendedContent> {
}
public class Toto {
public <E extends Content> E toto(ContentGroup<E> contentGroup) {
return contentGroup.getChildContents().get(0);
}
public void bar() {
ExtendedContent extendedContent = toto(new ExtendedContentGroup());
extendedContent.foo();
}
}
课程内容{
}
类ExtendedContent扩展内容{
void foo(){
}
}
抽象类ContentGroup扩展了内容{
private List childContents=new LinkedList();
公共列表getChildContents(){
返回内容;
}
}
类ExtendedContentGroup扩展ContentGroup{
}
公共课托托{
公共电子托托(ContentGroup ContentGroup){
返回contentGroup.getChildContents().get(0);
}
公共空白栏(){
ExtendedContent ExtendedContent=toto(新的ExtendedContentGroup());
extendedContent.foo();
}
}
还请注意,我确实收到了一个编译错误,因为在Java中,do是一个保留字(可能来自我的IDE设置,但不是100%确定),所以我怀疑您应该重命名您的函数(在我的情况下是toto)。要完成user3218114的回答,您应该执行以下操作:
public abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
...
}
public class Container extends ContentGroup {
}
class Content {
}
class ExtendedContent extends Content {
void foo() {
}
}
abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
}
class ExtendedContentGroup extends ContentGroup<ExtendedContent> {
}
public class Toto {
public <E extends Content> E toto(ContentGroup<E> contentGroup) {
return contentGroup.getChildContents().get(0);
}
public void bar() {
ExtendedContent extendedContent = toto(new ExtendedContentGroup());
extendedContent.foo();
}
}
课程内容{
}
类ExtendedContent扩展内容{
void foo(){
}
}
抽象类ContentGroup扩展了内容{
private List childContents=new LinkedList();
公共列表getChildContents(){
返回内容;
}
}
类ExtendedContentGroup扩展ContentGroup{
}
公共课托托{
公共电子托托(ContentGroup ContentGroup){
返回contentGroup.getChildContents().get(0);
}
公共空白栏(){
ExtendedContent ExtendedContent=toto(新的ExtendedContentGroup());
extendedContent.foo();
}
}
还请注意,我确实收到了一个编译错误,因为在Java中,do是一个保留字(可能来自我的IDE设置,但不是100%确定),所以我怀疑您应该重命名您的函数(在我的情况下是toto)。要完成user3218114的回答,您应该执行以下操作:
public abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
...
}
public class Container extends ContentGroup {
}
class Content {
}
class ExtendedContent extends Content {
void foo() {
}
}
abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
}
class ExtendedContentGroup extends ContentGroup<ExtendedContent> {
}
public class Toto {
public <E extends Content> E toto(ContentGroup<E> contentGroup) {
return contentGroup.getChildContents().get(0);
}
public void bar() {
ExtendedContent extendedContent = toto(new ExtendedContentGroup());
extendedContent.foo();
}
}
课程内容{
}
类ExtendedContent扩展内容{
void foo(){
}
}
抽象类ContentGroup扩展了内容{
private List childContents=new LinkedList();
公共列表getChildContents(){
返回内容;
}
}
类ExtendedContentGroup扩展ContentGroup{
}
公共课托托{
公共电子托托(ContentGroup ContentGroup){
返回contentGroup.getChildContents().get(0);
}
公共空白栏(){
ExtendedContent ExtendedContent=toto(新的ExtendedContentGroup());
extendedContent.foo();
}
}
还请注意,我确实收到了一个编译错误,因为do在Java中是一个保留字(可能来自我的IDE设置,但不是100%确定),所以我怀疑您应该重命名您的函数(在我的情况下是toto)。可能重复的
do
在Java中是一个关键字。无法用作标识符(方法名)如果将签名更改为\u do(ContentGroup ContentGroup)
?请在定义此方法的位置共享完整的代码。我更改了方法调用片段以简化代码(不幸的是,我选择了java关键字命名方法的可能性太大了)。已编辑该问题。do
的可能重复项是Java中的一个关键字。无法用作标识符(方法名)如果将签名更改为\u do(ContentGroup ContentGroup)
?请在定义此方法的位置共享完整的代码。我更改了方法调用片段以简化代码(不幸的是,我选择了java关键字命名方法的可能性太大了)。已编辑该问题。do
的可能重复项是Java中的一个关键字。无法用作标识符(方法名)如果将签名更改为\u do(ContentGroup ContentGroup)
?请在定义此方法的位置共享完整的代码。我更改了方法调用片段以简化代码(遗憾的是,我选择的方法命名的可能性非常大)