最も安いアイテムを返します

FHForce99:

私は、Javaにはかなり新しいですが、私は立ち往生currenltyだと、私はアイテムを返すようにしようとしていますが、それは安いアイテムを返しますです。私が使っている本の中で、それはに私が持っていると言い、「別のStoreItemを取る安い方法を追加します。これ、このアイテムまたは渡された1安い方の項目を返します。」、私はちょうどあなたがリターンにそれを取得する方法について混乱しています安いアイテム。

package cwk18;
import java.util.Collections;

    public class StoreItem {
       private String name;
       private double price;

    public StoreItem(){ // constructor
        name= "name unavailable";
        price = 1;
    }
    public StoreItem(String naming1,int val){ // parameterized construtor
        this();
        setName(naming1);
        setPrice(val);
    }
    public String getName(){ //name accessor
        return this.name;
    }
    public void setName(String newName){ // name mutator
        this.name = newName;
    }
    public double getPrice(){ // price accessor
        return this.price;
    }
    public void setPrice(int val){// price mutator
            this.price = val;
    }
    public void cheaper(StoreItem newItem){ 

    }

}

Glenio:

あなたが言う場合、この関数は安い方のアイテムを返すことです。機能はすべきではありませんvoid

だが 'The object name'

public StoreItem cheaper(StoreItem newItem) \\before is public void cheaper(...
{ 
   if(this.price >newItem.getPrice())
   {
       return newItem;
   }
   else 
   {
       return this;  //this means return the current item
   }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=365102&siteId=1