【问题标题】:How to inherit interface from superclass如何从超类继承接口
【发布时间】:2019-12-22 06:31:33
【问题描述】:

我有一个包含超类、子类和驱动程序类的接口。该接口必须在子类中实现,但是,我对如何做到这一点感到困惑。我是否在超类中实现接口然后扩展子类? 超类称为 store。

子类称为retail,它应该接收超类的构造函数,销售商品的数量、单价和销售价格应该是数组参数。这个类实现了两个在接口上定义的方法。

接口应该有两个方法。一个是利润,一个是工资,利润法是计算店铺一周的利润,工资法是计算店长一周的工资。

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

【问题讨论】:

  • 与其使用“创造性”的代码格式,最好坚持使用一种公认的标准,因为这样做会使您的代码更易于阅读和理解。
  • 我尝试将其格式化稍微更好,但仍然可以改进
  • 是的,这种格式确实是我见过的最糟糕的格式之一。请在线查找 Java 格式化和 Java 最佳实践,并获得一些基本的格式化知识。它将极大地帮助您理解您的自己的代码。括号在它们所在的位置结束是有原因的,因为它使块中的内容很明显。
  • 您的问题的一般答案是,有时超类实现接口,有时不实现,具体取决于需要。如果超类及其所有子类应该展示接口声明的行为,那么它应该实现它。
  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 3.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: java oop inheritance interface abstract


【解决方案1】:

一般规则:

  • 如果interface 合同适用于整个层次结构,那么implement 它在超类和所有子类中都会自动遵守interface 合同。
  • 您可以选择在超类中实现interface,但如果超类没有足够的细节来实现interface 方法,则将其设为abstract。通过这种方式,您可以强制子类遵守interface 合同。
  • 如果interface 合同与整个层次结构完全不相关,则仅在适用的子类中实施。

你的情况:

在您的示例中,问题是接口方法profit()salary() 是否适用于任何类型的Store?如果是(我假设是),那么继续并在超类中实现。但是,您可能无法使用可用数据点计算 Store 类中的 profit()salary()。因此,您可以选择将Store 类声明为抽象类。如果您可以实现这些方法,则将Store 类具体化。

另一方面,如果接口方法profit()salary() 可能不适用于所有类型的Stores,那么请继续在Retail 类中实现interface

虽然我认为第一个选项是不错的选择,但您可以根据业务场景进行选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多