简体   繁体   English

如何从超类继承接口

[英]How to inherit interface from superclass

I have an interface with a superclass, a subclass, and a driver class. 我有一个带有超类,子类和驱动程序类的接口。 The interface must be implemented in the subclass, however, I am confused on how to do it. 该接口必须在子类中实现,但是,我对该如何执行感到困惑。 Do I implement the interface in the superclass and then extends the subclass? 是否在超类中实现接口,然后扩展子类? The superclass is called store. 该超类称为存储。

The subclass is called retail, it should receive the superclass's constructor, the number of items sold, unit price, and sale price should be array arguments. 子类称为Retail,它应该接收超类的构造函数,所售商品的数量,单价和售价应为数组参数。 This class implements two methods that are defined at interface. 此类实现在接口上定义的两个方法。

The interface should have two methods. 接口应该有两种方法。 One is the profit and the other is salary The profit method is to calculate the store profit in a week, and salary method is to calculate the store manager's salary in a week. 一种是利润,另一种是工资。利润法是每周计算商店利润,而工资法是一周计算商店经理的工资。

/*
The interface should have two methods.
One is the “Profit” and the other is “Salary”.
The profit method is to calculate the store profit in a week, and salary         method
is to calculate the store manager’s salary in a week.
*/
public interface Interface {

    public void profit();
    public void salary();

}
/*
The store class is a super class that receives store location, 
manager name, hours worked, and hour rate.  
This class should have the constructor that receives all of these.  
It also should have get and set methods for each of fields. 
This class also has “toString()” to 
display restaurant location and manager’s name.
*/
public class Store {
    private String location;
    private String manager;
    private int hours;
    private int rate;

    public Store(String l, String m, int hrs, int r) {
        location = l;
        manager = m;
        hours = hrs;
        rate = r;
    }

    public void setLocation(String l) {
        location = l;
    }

    public String getLocation() {
        return location;
    }

    public void setName(String m) {
        manager = m;
    }

    public String getName() {
        return manager;
    }

    public void setHours(int hrs) {
        hours = hrs;
    }

    public int getHours() {
        return hours;
    }

    public void setRate(int r) {
        rate = r;
    }

    public int getRate() {
        return rate;
    }

    public String toString() {
        String result = "Store Location: " + location + "\n";
        result += "Manager name:" + manager + "\n";
        return result;
    }
}
public class Retail extends Store {
    private int items;
    private double unit;
    private double sale;

    public Retail(String l, String m, int hrs, int r,int i, double u, double s){
        super(l,m, hrs, r);
        items = i;
        unit = u;
        sale = s;
    }
    public void profit() {
        double[][] money = {{1.99, 2.99, 3.99, 4.99},
                            {5.99, 6.99, 7.99, 8.99},
                            {150, 48, 350,20}};
        for (int i = 0; i < money.length; i++) {
            for (int j = 0; j < money[i].length; j++) {
                sum += money[i][j];
            }
        }
        double profit = items * ( s - u);
    }

    public void salary() {
        double pay = hrs * r;
        //double salary = pay - ( pay * 0.05);
    }

    public double getSalary() {
        double baseSalary = super.getHours();
    }

    public String toString() {
        result += super.getName(); // inherited from superclass
        String result = "Total Benefit: " + profit + "\n";
        result += "Salary: " + salary + "\n";
        return result;
    }
}

General rules: 通用规则:

  • If the interface contract is applicable in the full hierarchy then implement it in the superclass and all subclasses adhere to the interface contract automatically. 如果interface协定适用于整个层次结构,则在超类中implement它,并且所有子类都会自动遵守interface协定。
  • You may choose to implement the interface in the superclass but make the superclass abstract if it does not have enough details to implement the interface methods. 您可以选择在超类中实现该interface ,但是如果它没有足够的细节来实现该interface方法,则使该超类成为abstract This way you can enforce the subclasses to adhere to the interface contract. 这样,您可以强制子类遵守interface协定。
  • If the interface contract is not at all relevant to the full hierarchy then implement only in the applicable subclasses. 如果interface协定与整个层次结构根本不相关,则仅在适用的子类中实现。

Your case: 您的情况:

In your example, the question is whether interface methods profit() and salary() applicable to any kind of Store ? 在您的示例中,问题是接口方法profit()salary()适用于任何种类的Store If yes (I assume it is), then go ahead and implement in the superclass. 如果是(我认为是),则继续在超类中实现。 However, you may not be able to compute profit() and salary() in the Store class with the data points available. 但是,您可能无法使用可用的数据点在Store类中计算profit()salary() So, you may choose to declare Store class as abstract. 因此,您可以选择将Store类声明为抽象。 In case you can implement these methods make Store class concrete. 如果可以实现这些方法,请使Store类具体化。

On the other hand, if the interface methods profit() and salary() may not be applicable to all kind of Store s then go ahead and implement the interface only in Retail class. 另一方面,如果接口方法profit()salary()可能不适用于所有类型的Store则继续仅在Retail类中实现该interface

Though I think the first option is the good one however, the choice is yours based on the business scenario. 尽管我认为第一个选择是好的选择,但是根据业务场景选择是您的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM