Java Generics - 使用extends关键字

时间:2015-04-17 13:22:47

标签: java generics

我阅读了关于Java Generics的文档,它说java'扩展'与泛型相关的关键字意味着实现该接口的类和类。

基于此,我尝试创建泛型类以进一步理解这一点。这是我的示例代码。

接口代码:

package com.vipin.generics.doubtclear;

public interface DemoInterface1<T> {

    void display(T t);
}

现在,我创建了几个实现此接口的简单类:

package com.vipin.generics.doubtclear;

class myClass<T> implements DemoInterface1<T> {

    public myClass(T t) {

    }

    @Override
    public void display(T t) {
        System.out.println("Inside display method of myClass, object is ---> "+t);
    }
}

class myClass1<T> implements DemoInterface1<T> {

    public myClass1(T t) {

    }

    @Override
    public void display(T t) {
        System.out.println("Inside display method of myClass1, object is ---> "+t);
    }
}

这是具有main的类,它尝试创建对象:

public class DoubtClear1 {

    static <T extends DemoInterface1<T>> void myFunc(T t) {
        t.display(t);
    }

    public static void main(String[] args) {

        myClass<Integer> iObj = new myClass<Integer>(1);
        myClass1<Integer> iObj1 = new myClass1<Integer>(1);
        DoubtClear1.myFunc(iObj);

    }
}
  • 主要是我正在创建myClass<Integer>myClass1<Integer>个对象。

  • 根据我对'延伸&#39;的理解应用于泛型myFunc()时的关键字能够采用任何实现DemoInterface1接口的对象引用。

  • 这就是我在通话中所做的事情==&gt; DoubtClear1.myFunc(iObj);

但是,这给了我编译时错误:

The method myFunc(T) in the type DoubtClear1 is not applicable for the arguments (myClass<Integer>)

这真令人困惑。我正在做关于这个主题的文档中所写的内容,但它失败了。我不确定我对这个主题的理解是否正确。

从我的感觉来看,Generics令人困惑,需要反复研究才能真正得到它。

清除这一点的任何信息都非常感激。

2 个答案:

答案 0 :(得分:2)

目前您正在评估条件

static <myClass1<Integer> extends DemoInterface1<myClass1<Integer>> void myFunc(myClass1<Integer> t) {

这不是真的,myClass1<Integer>DemoInterface1<myClass1<Integer>>不匹配,因为Integer的{​​{1}}通用条件不是myClass1(因为它是{{1} }})。

您应该执行以下操作

myClass1<Integer>

因此,以下内容应该有效

Integer

编辑:如果你想添加一个从static <T> void myFunc(DemoInterface1<T> interface, T t) { interface.display(t); } 延伸的新界面,那么就会改变myClass<Integer> iObj = new myClass<Integer>(1); myClass1<Integer> iObj1 = new myClass1<Integer>(1); DoubtClear1.myFunc(iObj, 1); 参数

DemoInterface1

我认为最初是你想要的。

EDIT2:

由于以下原因,代码中的错误未发生:

myFunc

这确实意味着static <T, N extends DemoInterface1<T>> void myFunc(N interface, T t) { interface.display(t); } 实现或延伸<T extends XYZ>

错误是因为你这样使用它:

T

基本上,XYZ方法收到的/* T extends from DemoInterface1, okay - this is myClass1 | | the generic parameter of that | DemoInterface1 is bound to T, | which as mentioned, | extends from DemoInterface1 | (in your case, it means this is myClass1<Integer>, NOT Integer) | | | | | | | | here you receive a bound for T | | where T is meant to extend from | | DemoInterface1<T> | | in your example, T is myClass1<Integer>, and NOT `Integer` that is the <T> of DemoInterface1<T> (myClass1<Integer>) | | | | | \|/ \|/ \|/ */ static <T extends DemoInterface1<T>> void myFunc(T t) { t.display(t); } 不是T,而是display

EDIT3:

Integer

是的,您正在提供myClass1I got your point we have to pass object like myClass1<Integer> and NOT Integer. However, when we do like: myClass1<Integer> iObj1 = new myClass1<Integer>(1), isn't iObj1 of myClass1<Integer> type, and NOT Integer. This i am passing to myFunc(). Does <T> gets replaced with Integer? or with the object with which we are calling it? May be, this is the cause of confusion. Please explain. Thanks a lot! 个实例。因此,myFunc中的iObj1被解析为<T>

但是,您还将其用作static <T extends DemoInterface1<T>> void myFunc(T t) {myClass1<Integer>的参数。因此,您尝试为display提供t.display(__)方法,但等待myClass1<Integer>。从本质上讲,您几乎都在调用iObj1.display(__)Integer等待iObj1.display(iObj1),而不是display(T)

答案 1 :(得分:1)

你有这个功能:

static <T extends DemoInterface1<T> > void myFunc(T t) {
    t.display(t);
}

然后你这样称呼它:

myClass<Integer> iObj = new myClass<Integer>(1);
DoubtClear1.myFunc(iObj);

现在,myClass<Integer>T extends DemoInterface1<T>类型不匹配,但与T extends DemoInterface1<Integer>类型不匹配。所以也许你想要这样的东西:

static <T extends DemoInterface1<?> > void myFunc(T t) {
    t.display(t);
}

那会有用。 (除非我误解了你的意图。)

相关问题