Java generics: how to wrap / extend Android activity test cases?

I tried to extend activityinstrumentationtestcase2 as follows:

public abstract class FooActivityTestCase<T extends Activity>
  extends ActivityInstrumentationTestCase2<Activity> {

  public FooActivityTestCase(String pckg,Class<Activity> activityClass)
  {
    super(pckg,activityClass);
  }

  public void foo(){ ... }
}

I try to extend fooactivitytestcase like this:

public class SpecificFooTestCase
  extends FooActivityTestCase<MyActivity> {
  public SpecificFooTestCase() {
    super("foo.bar",MyActivity.class);  // error on this line
  }
}

Eclipse gives the following error in the constructor:

The constructor FooActivityTestCase<MyActivity>(String,Class<MyActivity>) is undefined

I'm pretty sure the problem is how I use generics When specificfootestcase extends activityinstrumentationtestcase 2, I do not receive any errors Who can point out what I did wrong?

Kublai Khan and Michael miles suggested working together After I changed fooactivitytestcase to extend activityinstrumentationtestcase2 < T > and class < activity > class < T > in the constructor, the class compiles without errors This is the result class (specificfootestcase does not change):

public abstract class FooActivityTestCase<T extends Activity>
  extends ActivityInstrumentationTestCase2<T> {

  public FooActivityTestCase(String pckg,Class<T> activityClass)
  {
    super(pckg,activityClass);
  }

  public void foo(){ ... }
}

Solution

You need to define a super constructor like this:

//Only accepts classes that are Activity or extend Activity
 public FooActivityTestCase(String pckg,Class<? extends Activity> activityClass)
      {
        super(pckg,activityClass);
      }

The question is, for generic parameters, the generic type must always be the same generic type. If you want to be able to pass the content that needs to use wildcards in the inheritance tree? And extension, so you can pass any generic type that extends the class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>