java - 如何从父类(super class)继承接口(interface)

标签 java oop inheritance interface abstract

我有一个带有父类(super class)、子类和驱动程序类的接口(interface)。该接口(interface)必须在子类中实现,但是,我对如何实现感到困惑。我是否在父类(super class)中实现接口(interface),然后扩展子类? 父类(super class)称为 store。

子类称为零售,它应该接收父类(super class)的构造函数,销售的商品数量、单价和销售价格应该是数组参数。该类实现了接口(interface)中定义的两个方法。

该接口(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.
*/
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;
    }
}

最佳答案

一般规则:

  • 如果 interface契约(Contract)适用于整个层次结构,然后 implement它在父类(super class)和所有子类中都遵守 interface自动契约。
  • 您可以选择实现 interface在父类(super class)中,但使父类(super class) abstract如果没有足够的详细信息来实现 interface方法。通过这种方式,您可以强制子类遵守 interface契约(Contract)。
  • 如果 interface契约与完整的层次结构完全无关,因此仅在适用的子类中实现。

您的案例:

在您的示例中,问题是接口(interface)方法 profit() 是否和salary()适用于任何类型Store ?如果是(我认为是),那么继续在父类(super class)中实现。但是,您可能无法计算 profit()salary()Store具有可用数据点的类。所以,您可以选择申报Store类作为抽象类。如果您可以实现这些方法,请执行 Store具体类。

另一方面,如果接口(interface)方法 profit()salary()可能不适用于所有类型 Store然后继续实现 interface仅在 Retail类。

虽然我认为第一个选项是好的,但是,选择权在于您根据业务场景。

关于java - 如何从父类(super class)继承接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57516056/

相关文章:

java - 类构造函数不区分 args... 和 args[][]

c# - 在访问器 C# 中调用方法

javascript - 如果我使用原型(prototype)创建 OOP JS 代码,如何从循环引用类方法?

c++ - 指向派生类对象的基类指针类型

java - 这是执行此操作的 'correct' 方法(if 语句)

java.lang.RuntimeException : Deferred binding error in GWT

JavaFX 8 DatePicker 样式

java - Apache-POI 忽略 Excel 工作表的行

python - 如何在 Python 的基类中创建派生类的对象?

java - 将final关键字添加到继承/重写的方法中是否为 "ok"?