论坛首页 入门技术论坛

Is this a good/right way to use generic interface?

浏览 1976 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-09-06  
I have a generic interface like:
public interface Ia<E> {
    public void add(E element);
    public E get();
}


I can implement it like:

1.
public class A implements Ia {

    public void add(Object element) {
    }

    public Object get() {
        return null;
    }

}


2.
public class A implements Ia<Object> {

    public void add(Object element) {
    }

    public Object get() {
        return null;
    }

}


3.
public class A<F> implements Ia<F> {

    public void add(Object element) {
    }

    public F get() {
        return null;
    }

}


4.
public class A<F> implements Ia<F> {

    public void add(F element) {
    }

    public F get() {
        return null;
    }

}


IMO, all implementations above are valid, but:
* implementation 1 treat generic as old code before generic imported into Java, compiler will give a warning on it, but at least it's clear to most of all
* implementation 2 is weird in fact, it's same with implementation 1, but it will give a shock to user
* implementation 3 use generic interface in a not bad way, but it use Object on one of methods prototype, and I don't think it's better for user either
* implementation 4 use generic interface in a right way

What's your insight?
   发表时间:2007-09-07  
Hi, Boy! What do you wanna express?
0 请登录后投票
   发表时间:2007-09-07  
movingboy 写道
Hi, Boy! What do you wanna express?
you don't understand? that's why I post this notes
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics