亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不能在原語上調用 Equals 方法>Override Equals 方法不起作用

不能在原語上調用 Equals 方法>Override Equals 方法不起作用

千萬里不及你 2022-08-17 16:54:16
我在覆蓋Equals方法時獲得了編譯錯誤,更具體地在Float和Double括號,權重和價格屬性,HashCode方法似乎有效,所以我不知道問題在我的代碼中的位置。該代碼還包含一個接口 Vehicle,但我不希望錯誤來自接口。我不確定我是否完全理解了 Equals 方法 。有人可以幫助我嗎? package info.vehicle;    import java.io.Serializable;    public class Car implements Vehicle,Serializable,Cloneable {    private float weight;    private double price;    private String producer;    public Car()    {    }    public Car(float _weight,double _price,String _producer)    {     weight=_weight;     price=_price;     producer=_producer;    }    public void setProducer(String _producer) throws Exception    {        if(_producer!=null && _producer.length()>1)            this.producer=_producer;    else    {        throw new Exception("Producer must not be null.");    }    }    public void setPrice(double _price) throws Exception    {        if(_price>0)        {            price=_price;        }        else        {            throw new Exception("Price must not be 0.");        }    }    public void setWeight(float _weight) throws Exception    {        if(weight>0)        {            weight=_weight;        }        else        {            throw new Exception("Weight must be different than 0.");        }    }    @Overridepublic boolean equals(Object obj) {    Car other=(Car)obj;    if(((Float)(this.weight)).equals(Float)other.weight)    {    if((Double)(this.price)).equals((Double)other.price))       {        if(this.producer.contentEquals(other.producer))          {             return super.equals(obj);          }       }    }    return false;}     @Override    protected Object clone() throws CloneNotSupportedException {        Car temp=new Car();        temp.weight=this.weight;        temp.price=this.price;        temp.producer=this.producer;        return temp;    }    @Override    public int hashCode() {        final int prime=31;        int result=0;        result+=(int)weight*prime;        result+=(int)price*prime;        result+=producer.length()*prime;        return result;    }
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

簡單的語法錯誤。

equals(Float)other.weight)

應該是

equals((Float) other.weight)

if((Double)(this.price))

應該是

if(((Double)(this.price))

但是你為什么要明確地鑄造它們呢?


查看完整回答
反對 回復 2022-08-17
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號