如何覆盖子类中的方法? [英] How do I override a method in a subclass?

查看:130
本文介绍了如何覆盖子类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库存程序,包括一个数组和一个方法来计算所有输入的库存项目的总成本。我现在必须包括一个子类覆盖原来包括一个独特的功能。我创建了一个名为ItemDetails的新文件来设置原始项的子类。我需要包括一个独特的功能,并计算库存的价值,并计算5%的补货费在这个子类。我只是把一些相关的线转移到另一个类?还是我写两次代码?我不知道下一步该做什么。任何帮助是有用的。谢谢。这是我到目前为止:

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()
        {
        }
    }
}

这是要覆盖的Item类文件:

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;
    }

}


推荐答案

您只需声明与父类中的方法具有相同签名的方法。所以你的形式如下:

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
    }
}

这篇关于如何覆盖子类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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