0

I am having issues with my subclasses and trying to make my function "method" generic, please see below, I am having issues implementing classes C1 and C2 to use my objects of type B1 and B2 respectively.

Edit: Sorry if it isn't clear, basically I am trying to override the function method, but I need the parameter to be generic, so for class C1 which is a subclass of C it should use B1 as a parameter and for C2 which is a subclass of C1 it should use B2 as a parameter.

The code the behavior that I am looking to achieve but I am struggling to make it compile.

Here are my classes

class A{}
class T1{}
class V1{}
abstract class B<T extends T1, V extends V1>{}
class B1 extends B<SomeChildOfT1, SomeChildOfV1>{}
class B2 extends B1{}

abstract class C <E extends B<? extends T1, ? extends T2>> extends A
{
    void method(E)
      {//bla}
}

class C1<E extends B1> extends C<B1>
{
   //method with object B1 as parameter
   @override
   void method(E){
    //bla
    } 
}

class C2 extends C2<B2>
{
    @override
  void method(B2)
  {

  }
}
4

2 回答 2

0

Is there any reason you can't make C1< E > extend C< E >? The following compiles for me with suitable edits to your sample:

class A{}
class T1{}
class SomeChildOfT1 extends T1 {}
class V1{}
class SomeChildOfV1 extends V1 {}
abstract class B<T extends T1, V extends V1>{}
class B1 extends B<SomeChildOfT1, SomeChildOfV1>{}
class B2 extends B1{}

abstract class C <E extends B<? extends T1, ? extends V1>> extends A {
    void method(E e) {}
}

class C1<E extends B1> extends C<E> {
    @Override void method(E e) {}
}

class C2 extends C1<B2> {
    @Override void method(B2 b2) {}
}
于 2012-10-29T07:08:46.350 回答
0

虽然有点乱,但我认为你应该这样做:

class A{}
class T1{}
class V1{}
abstract class B<T extends T1, V extends V1>{}
class B1 extends B<SomeChildOfT1, SomeChildOfV1>{}
class B2 extends B1{}

abstract class C <T extends T1, V extends V1, E extends B<T, V>> extends A
{
    void method(E)
      {//bla}
}

class C1<E extends B1> extends C<SomeChildOfT1, SomeChildOfV1, E>
{
   //method with object B1 as parameter
   @override
   void method(E){
    //bla
    } 
}

class C2 extends C1<B2>
{
    @override
  void method(B2)
  {

  }
}
于 2012-10-29T06:30:25.060 回答