方法返回类型包含子类定义 [英] Method return type contains subclass definition

查看:121
本文介绍了方法返回类型包含子类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public< A extends B> double [] [] foo(C arg){
....}

I
$ b a) foo(C arg)的返回类型是< A扩展B>双[] [] ?这是什么意思?例如,我会理解返回类型,比如 double [] [] >,但我无法确定前一个修饰符(可能?)< A b)为什么在返回类型中有一个子类声明?由于A是B的一个子类,我们在哪里重写或添加任何方法/成员等?对我来说,它似乎是一个只包含基类A的相同方法/成员的子类。不是吗?那么在编写 public< A> c)最后,我认为<>必须处理Java泛型,但是甚至那么我已经看到类似于 D 的声明,其中T用于参数化原始类型 D 。在这里,我尝试删除<>(因为我不明白它们代表的是什么),但是编译器抱怨了。

  • 返回类型是 double [] [] < A扩展B> 部分是通用方法

  • restriction 扩展B 意味着只有泛型类型 A 扩展或者实际上在这个方法中实现 B 是允许的。

  • 无法回答第三个问题,因为它需要方法体中的代码,更多上下文。


  • I come up with a code that uses a syntax like this:

    public <A extends B> double[][] foo(C arg) {
    .... }
    

    I get a couple of questions by viewing it.

    a) The return type of foo(C arg) is <A extends B> double[][]? What does this mean? I would understood a return type like double[][] for example, but I cannot determine what does the previous modifier (maybe?) <A extends B> does?

    b) Why there is a subclass declaration inside a return type? Since A is a subclass of B where do we override or add any methods/members etc? To me it seems that it's a subclass containing just the same methods/members of the base class A. Isn't so? So is there any difference in writing public <A> double[][] foo(C arg)?

    c) Finally I suppose that <> have to do with Java generics but even then I have seen declaration like D<T> which T is used to parametrize the raw type D. Here I tried to remove the <> (since I don't understand what they stand for) but compiler complains.

    解决方案

    1. The return type is double[][]. The <A extends B> part is the parametrization of the generic method
    2. The restriction extends B implies that only generic types A extending or actually implementing B are allowed in this method.
    3. Cannot answer your third question as it would require the code in the method's body and more context.

    这篇关于方法返回类型包含子类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆