質問

(Javaでの作業) 私はクラス全体を通して汎用型タイピングを持つ抽象クラスを持っています:

public abstract class ConnectionProcessor<T>
{
    public void process()
    {
        for (List<T> resultsPage : connection)
        {
            processPage(resultsPage);
        }
    }

    protected abstract void processPage(List<T> resultsPage);
}
.

次の宣言で抽象クラスの範囲を拡張する別のクラスがあります。

public class AlbumProcessor<Album> extends ConnectionProcessor
{
    @Override
    protected void processPage(List resultsPage)
    {
        //Do stuff here specific to Album
    }
}
.

この宣言はうまく機能しますが、processPageではAlbum固有のものをやりたいと思います。私はこれをメソッド宣言にすることを好むでしょう:

protected void processPage(List<Album> resultsPage)
.

しかしこれはprocessPageからConnectionProcessorをオーバーライドするための要件を満たしていません。どうしてこれなの?どうやって希望の行動を得ることができますか?AlbumProcessorを介して<Album>をプラグインすることができると思うでしょう。

役に立ちましたか?

解決

を試してください
//extend prameterized version of ConnectionProcessor<T> with Album as actual type argument
public class AlbumProcessor extends ConnectionProcessor<Album> {
.

の代わりにp>
public class AlbumProcessor<Album> extends ConnectionProcessor {
.

上記を行ったときの汎用型ConnectionProcessor<T>のRAWバージョンを拡張し、新しい正式型パラメータの紹介 - 実際のタイプ引数ではない(Albumなど)。

他のヒント

あなたはあなたのスーパークラス汎用型TAlbumにバインドしなかったからです。

むしろ、これはあなたがするべきものです:

public class AlbumProcessor extends ConnectionProcessor<Album>
.

だから、メソッドprocessPageをオーバーライドすると(IDEを使用)、次のようにコードが生成されます。

@Override
protected void processPage(List<Album> resultsPage)
{
    //Do stuff here specific to Album
}
.

public class AlbumProcessor extends ConnectionProcessor<Album>
.

試してください: -

public class AlbumProcessor extends ConnectionProcessor<Album>
{
    @Override
    protected void processPage(List<Album> resultsPage)
    {
        //Do stuff here specific to Album
    }
}
.

あなたがあなたのメソッド宣言のリストへのタイプパラメータとして与えるタイプでスーパークラスをバインドする必要があります。

どのようにして何かがどれほどのものを使うか。

import java.util.List;

public abstract class ConnectionProcessor<T>
{
    public void process()
    {
        System.out.println("Hello");
    }

    protected abstract void processPage(List<? extends T> resultsPage);
}
.

...

public class ProcessorImpl extends ConnectionProcessor<Album> {

    protected void processPage(List<? extends Album> resultsPage) {
        for(Album result : resultsPage){
            System.out.println(result.getAlbumName());
        }
    }

}
.

...

public class Album {
    public String getAlbumName(){
        return "Sweet Smooth SOunds of the 70's";
    }
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top