的Java / Android的工作室 - 协变返回类型 - 在&QUOT方法;子"在&QUOT法的冲突;基类":尝试使用不兼容的返回类型 [英] Java/Android Studio - Covariant Return Type - Method in "subclass" clashes with method in "baseclass": attempting to use incompatible return type

查看:235
本文介绍了的Java / Android的工作室 - 协变返回类型 - 在&QUOT方法;子"在&QUOT法的冲突;基类":尝试使用不兼容的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在申请在下列情况下一个协变返回类型(code在很大程度上截断相关性):

I am currently trying to apply a covariant return type in the following situation (code is truncated heavily for relevance):

属性类:

  public abstract class Attribute
    {
         protected Number value;
         public Attribute(Number initialValue)  
         {     this.value = initialValue;}

         public abstract Number getValue();
    }

DoubleAttribute类:

  public class DoubleAttribute extends Attribute
        {
             public DoubleAttribute(double initialValue)  
             {     super(initialValue);}

             @Override     
             public double getValue() //The error shows on 'double'
             {     return this.value.doubleValue();}
        }

不幸的是,Android的工作室是不是让我改变子 DoubleAttribute 编号来的返回类型双击

Android的工作室目前正在给我以下错误:

Android Studio is currently giving me the following error:

'的getValue()与attributes.DoubleAttribute冲突在'attributes.Attribute的getValue();尝试使用不兼容的返回类型。

我目前的想法是什么,我做错了损失。 ,我已经要求对谷歌的每个协变返回类型的例子告诉我,这应该工作,因为双击类是

I am currently at a loss for ideas as to what i am doing wrong. Every covariant return type example that i've sought on Google tells me that this should work, as the double class is a subclass of Number.

如果任何人都可以通过指出为什么Android的工作室告诉我,这是不允许的,我将非常感激帮助我。

If anyone could help me by pointing out why Android Studio is telling me that this isn't allowed i would be very grateful.

在一个侧面说明,我不完全确定为,如果我采用的方法是在第一时间正确的。我目前的测量属性,这些属性可以是字符串键入双击键入键入或 INT 键入并随后将其存储在一个SQLite表。首先我声明了变量作为一个字符串,然后分析在String再一个双/浮点/ INT /长presentation。我才发现,不过,这需要过量铸造和类型检查和寻求替代解决方案的负荷,但我仍然怀疑如果我当前继承的解决方案是最佳的。

On a side note, i am not entirely sure as to if the approach i am taking is correct in the first place. I am currently measuring attributes that can either be of String type, double type, long type or int type and subsequently storing them in a SQLite table. At first i declared the value variable as a String and just parsed in a String representation of a double/float/int/long. I found, however, that this required loads of excess casting and type checking and sought for an alternative solution, but i am still doubting if my current inheritance solution is an optimal one.

如果任何人都可以在这个问题上分享他们的见解,以及,我会更感激。

If anyone could share their insights on this matter as well, i would be even more grateful.

先谢谢您的任何努力,并且如果有更多的书面code的要求我将修改我的问题。

Thanks in advance for any effort, and if more of the written code is required i'll edit my question.

推荐答案

您不返回双击类。返回原始的两倍。一个基本类型不能是一个对象的子类。

You do not return a Doubleclass. You return a primitive double. A primitive type can not be a subclass of an object.

public double getValue() //The error shows on 'double'
{
    return this.value.doubleValue();
}

更改为(大写D)

change to (Uppercase D)

public Double getValue() //The error shows on 'double'
{
     return this.value;
}

这篇关于的Java / Android的工作室 - 协变返回类型 - 在&QUOT方法;子"在&QUOT法的冲突;基类":尝试使用不兼容的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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