简体   繁体   English

我如何从java中的泛型类继承?

[英]How do I inherit from a generic class in java?

I have a class like following 我有一个像下面这样的课程

public class Foo<idType extends WritableComparable<idType>, EData extends Writable> {
  public Foo();

  public Foo(idType foo; idType bar){
  this.foo = foo;
  this.bar = bar;
  }
  private idType foo;
  private idType bar;

}

Now one of the usage of this class is like following: 现在,这个类的一个用法如下:

elist = new ArrayList<Foo<StringType, EmptyType>>();

So this works just fine: 所以这很好用:

Now I want to extend this class to add one more field 现在我想扩展这个类以添加一个字段

private String foobar; 私人String foobar;

Now, basically an instance of this would be have three fields. 现在,基本上这个实例将有三个字段。

Two of them 他们两个人

   foobar.foo //base class
   foobar.bar //base class
   foobar.foobar // new variable added

Now, my usage is still the same: 现在,我的用法仍然是相同的:

 elist = new ArrayList<FooBar<StringType, EmptyType>>();

I tried a simple extension: 我尝试了一个简单的扩展:

 public class Foobar extends Foo{
 private String foobar;
 public FooBar(String foobar){this.foobar = foobar;}

} }

But when I use 但是当我使用时

I get an error: 我收到一个错误:

 elist = new ArrayList<FooBar<StringType, EmptyType>>();
ArrayList<FooBar><StringType,EmptyType>> cannot be resolved to a type

If you want to let the user specify the types to your subclass, specify the same type parameters, and pass them on to the base: 如果要让用户指定子类的类型,请指定相同的类型参数,并将它们传递给基类:

public class FooBar <idType extends WritableComparable<idType>, EData extends Writable>
    extends Foo<idType, EData>
{ 
    ...
}

If you want to let the user only specify one of those types, you can do that, eg you want to force Integer for idType : 如果你想让用户只指定其中一种类型,你可以这样做,例如你想为idType强制Integer

public class FooBar <EData extends Writable>
    extends Foo<Integer, EData>
{ 
    ...
}

If you want to only use specific types for the base, same idea: 如果你只想使用特定类型的基础,同样的想法:

public class FooBar
    extends Foo<Integer, Something>
{ 
    ...
}

You can even add a type: 你甚至可以添加一个类型:

public class FooBar <idType extends WritableComparable<idType>, EData extends Writable, AnotherType>
    extends Foo<idType, EData>
{ 
    private AnotherType x;
    ...
}

The point is, you specify your own parameter types in the subclass in any way you see fit, and you can pass those types to the base as long as they are compatible types. 关键是,您可以以任何您认为合适的方式在子类中指定自己的参数类型,只要它们是兼容类型,您就可以将这些类型传递给基类。

Edit: Responding to a comment on the question above, you do have to specify constraints on the FooBar type parameters that match constraints on the base Foo . 编辑:响应上面问题的注释,您必须在FooBar类型参数上指定与基本Foo上的约束匹配的约束。 For example, the following is not sufficient: 例如,以下内容是不够的:

public class FooBar <idType, EData>
    extends Foo<idType, EData> // <-- will fail to compile
{ 
    ...
}

This will lead to the following compilation errors: 这将导致以下编译错误:

type parameter idType is not within its bound
type parameter EData is not within its bound

This is because Foo expects types that extend WritableComparable<idType> and Writable , respectively, but the above erroneous declaration of FooBar attempts to pass types that do not meet those constraints as type parameters to Foo . 这是因为Foo期望分别扩展WritableComparable<idType>Writable类型,但上面错误的FooBar声明尝试将不满足这​​些约束的类型作为类型参数传递给Foo

Your error, by the way, as posted, does not appear to match your code and has an extra > at the end. 顺便说一句,您发布的错误似乎与您的代码不符,并且最后还有一个额外的> It appears you made a typo when copying and pasting. 看来你在复制和粘贴时写了一个拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM