How do I override a method in a subclass?

2019-02-16 00:57发布

I have an inventory program written to include an array and a method to calculate total cost for all inventory items entered. I now have to include a subclass that overrides the original to include "one unique feature". I created a new file named ItemDetails to set up for the subclasses of the original Item. I need to include one unique feature and calculate the value of the inventory and calculate a 5% restocking fee in this subclass. Do I just transfer some of the relevant lines into the other class? Or do I write some code twice? I don't know what to do next. Any help is useful. Thanks. This is what I have so far:

package inventory3;

public class ItemDetails extends Items
{
public static void override()
    {
    private String Name;
    private double pNumber, Units, Price;

public ItemDetails()
        {
        }
    }
}

This is the Item class file that it is supposed to override:

package inventory3;

import java.lang.Comparable;                

    public class Items implements Comparable
{
       private String Name;
       private double pNumber, Units, Price;

public Items()
    {
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
    }

public int compareTo(Object item)
    {

  Items tmp = (Items) item;


    return this.getName().compareTo(tmp.getName());
    } 


public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
    {
    Name = productName;
    pNumber = productNumber;
    Units = unitsInStock;
    Price = unitPrice;
    }
    //setter methods
public void setName(String n)
    {
    Name = n;
    }

public void setpNumber(double no)
    {
    pNumber = no;
    }

public void setUnits(double u)
    {
    Units = u;
    }

public void setPrice(double p)
    {
    Price = p;
    }

//getter methods
public String getName()
    {
return Name;
    }

public double getpNumber()
    {
return pNumber;
    }

public double getUnits()
    {
return Units;
    }

public double getPrice()
    {
return Price;
    }

public double calculateTotalPrice()
    {
    return (Units * Price);
    }

public static double getCombinedCost(Items[] item)          
    {
    double combined = 0;        

    for(int i =0; i < item.length; ++i)
        {
        combined = combined + item[i].calculateTotalPrice();        

        } 
    return combined;
    }

}

1条回答
成全新的幸福
2楼-- · 2019-02-16 01:36

You simply declare a method with the same signature as the method in the parent class. So yours would look like:

package inventory3;

public class ItemDetails extends Items {
    private String Name;
    private double pNumber, Units, Price;

    public ItemDetails(String Name, double pNumber, double Units, double Price) {
        this.Name = Name;
        this.pNumber = pNumber;
        this.Units = Units;
        this.Price = Price;
    }

    // getters and setters....

    // The @Override is optional, but recommended.
    @Override
    public double calculateTotalPrice() {
        return Units * Price * 1.05; // From my understanding this is what you want to do
    }
}
查看更多
登录 后发表回答