Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从超类继承接口_Java_Oop_Inheritance_Interface_Abstract - Fatal编程技术网

Java 如何从超类继承接口

Java 如何从超类继承接口,java,oop,inheritance,interface,abstract,Java,Oop,Inheritance,Interface,Abstract,我有一个与超类、子类和驱动程序类的接口。接口必须在子类中实现,但是,我对如何实现感到困惑。我是否在超类中实现接口,然后扩展子类? 超类称为store 子类称为retail,它应该接收超类的构造函数,销售的商品数量、单价和销售价格应该是数组参数。此类实现了在接口处定义的两个方法 接口应该有两种方法。一个是利润,另一个是工资利润法是计算商店一周的利润,工资法是计算商店经理一周的工资 /* The interface should have two methods. One is the “Profi

我有一个与超类、子类和驱动程序类的接口。接口必须在子类中实现,但是,我对如何实现感到困惑。我是否在超类中实现接口,然后扩展子类? 超类称为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();

}
公共类零售店扩展{
私人物品;
私人双人房;
私人双重销售;
公共零售(字符串l、字符串m、整数小时、整数r、整数i、双u、双s){
超级(l、m、hrs、r);
项目=一;
单位=u;
sale=s;
}
公共利益(){
双[]货币={{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
一般规则:
  • 如果
    接口
    契约适用于整个层次结构,则
    在超类中实现它,并且所有子类自动遵守
    接口
    契约
  • 您可以选择在超类中实现
    接口
    ,但如果超类没有足够的细节来实现
    接口
    方法,则可以将其抽象化。通过这种方式,您可以强制子类遵守
    接口
    契约
  • 如果
    接口
    契约与整个层次结构完全不相关,则仅在适用的子类中实现
你的情况: 在您的示例中,问题是接口方法
profit()
salary()
是否适用于任何类型的
商店
?如果是(我假设是),那么继续在超类中实现。但是,您可能无法使用可用的数据点在
Store
类中计算
profit()
salary()
。因此,您可以选择将
存储
类声明为抽象类。如果您可以实现这些方法,请将
Store
类具体化

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


尽管我认为第一个选项是好的,但是根据业务场景,您可以做出选择。

与其使用“创造性”的代码格式,不如坚持一个公认的标准,因为这样做会使代码更易于阅读和理解。我尝试将其格式设置得更好,但它仍然可以改进是的,这是我见过的最糟糕的格式之一。请在线查找Java格式化和Java最佳实践,并获得一些基本的格式化知识。它将帮助您更好地理解自己的代码。括号之所以结束是有原因的,因为它清楚地表明了块中的内容。对您的问题的一般回答是,有时超类实现接口,有时不实现接口,这取决于需要。如果超级类及其所有子类都应该显示接口的声明行为,那么它应该实现它。请不要通过破坏您的帖子为其他人做更多的工作。通过在Stack Exchange网络上发布,您已授予Stack Exchange在下不可撤销的权利,以分发该内容(即,无论您未来的选择如何)。根据堆栈交换策略,帖子的非破坏版本是分发的版本。因此,任何故意破坏行为都将恢复原状。如果您想了解有关删除帖子的更多信息,请参阅:
/*
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;
    }
}