簡體   English   中英

實現泛型接口的Java泛型類

[英]Java Generic Class which Implements a Generic Interface

最初,我想創建一種深度復制對象的通用方法,該方法實現了“ X”接口。 但是,我不想為每個實現“ X”的類編寫一個復制列表功能。 相反,我創建了一個通用類,將其與接口“ X”綁定在一起。

問題在於,如果類“ A”實現了“ X”並包含(執行)/(不執行)實現“ X”的子類。 如何阻止子類繼承自“ X”的“ A”實現並強制執行自己的實現,而不必為每個子類都實現“ X”。

我很確定由於繼承和泛型,在Java中是不可能做到的。

這是到目前為止的實現,但是我需要一個類型轉換,該轉換為不繼承接口“ X”的子類帶來不安全的操作。

import java.util.*;


public class HelloWorld {

    public static void main(String[] args) {
    // Prints "Hello, World" to the terminal window.
    System.out.println("Hello, World");

    List<Foo> l = new ArrayList<Foo>();
    List<Bar> b = new ArrayList<Bar>();
    List<Nar> n = new ArrayList<Nar>();

    GenericCopyClass<Foo> g = new GenericCopyClass<Foo>();
    GenericCopyClass<Bar> gb = new GenericCopyClass<Bar>();
    GenericCopyClass<Nar> gn = new GenericCopyClass<Nar>();


    l.add(new Foo());
    l.add(new Foo());
    l.add(new Foo());


    b.add(new Bar());
    b.add(new Bar());
    b.add(new Bar());

    n.add(new Nar());
    n.add(new Nar());
    n.add(new Nar());


    //prints FOO
    List<Foo> foo = g.copyList(l);

    //prints BAR
    List<Bar> bar = gb.copyList(b);

    //prints FOO
    List<Nar> nar = gn.copyList(n);

    /* Is there a clean way to prevent a typecast of Foo to Nar using generics*/

    }

}

class Foo implements WantToClone{
    public Foo(){}
    private Foo(int i){
    System.out.println("Foo");
    }
    public Foo instance(){
    return new Foo(1);
    }
}

class Bar extends Foo{
    public Bar(){} 

    private Bar(int i){
    System.out.println("Bar");
    }
    public Bar instance(){
    return new Bar(1);
    }
}

class Nar extends Foo{
    public Nar(){} 

}

interface WantToClone<E extends WantToClone<E>>{
    public E instance();
}


class GenericCopyClass<S extends WantToClone>{

    public GenericCopyClass(){}

    public List<S> copyList(List<S> original){
        List<S> temp = new ArrayList<S>();

        for(S item : original){
            temp.add((S)item.instance());   
        }
        return original;
    }

}

輸出如下:

Hello, World
Foo
Foo
Foo
Bar
Bar
Bar
Foo
Foo
Foo

*編輯1

問題:

隱式類型轉換:

temp.add((S)item.instance());

接口未明確指定類實例:

interface WantToClone<E extends WantToClone<E>>

我將使用包含所有通用方法的參數化抽象類,並讓子類僅實現instance方法:

    public abstract class AbstractFoo< F extends AbstractFoo<F> > 
    implements WantToClone<F>{

        public AbstractFoo(){}

        //add common methods here
    }

    public class Foo extends AbstractFoo<Foo>{
        public Foo(){}

        private Foo(int i){
            System.out.println("Foo");
        }

        public Foo instance(){
            return new Foo(1);
        }
    }

    public class Bar extends AbstractFoo<Bar>{
        public Bar(){} 

        private Bar(int i){
            System.out.println("Bar");
        }

        public Bar instance(){
            return new Bar(1);
        }
    }

    public class Nar extends AbstractFoo<Nar>{
        public Nar(){}

        @Override
        public Nar instance() {
            return new Nar();
        } 

    }

    public interface WantToClone< E extends WantToClone<E> >{
        public E instance();
    }


    public class GenericCopyClass< S extends WantToClone<S> >{

        public GenericCopyClass(){}

        public List<S> copyList(List<S> original){
            List<S> temp = new ArrayList<S>();

            for(S item : original){
                temp.add( item.instance() );   
            }
            return original;
        }    
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM