Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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_Java 8 - Fatal编程技术网

Java 如何在子类中编写代码来恢复父类中的一些逻辑?

Java 如何在子类中编写代码来恢复父类中的一些逻辑?,java,java-8,Java,Java 8,我有产品对象和两个类AsiaProductValidation和ProductValidation。当地区为亚洲时,称为AsiaProductValidation。ProductValidation是父类,它具有一些可用于AsiaProductValidation的函数。我想知道我是否可以在AsiaProductValidation中编写validate方法来重用在父类中编写的一些检查 class Product{ int id, long price;

我有产品对象和两个类AsiaProductValidation和ProductValidation。当地区为亚洲时,称为AsiaProductValidation。ProductValidation是父类,它具有一些可用于AsiaProductValidation的函数。我想知道我是否可以在AsiaProductValidation中编写validate方法来重用在父类中编写的一些检查

 class Product{
        int id,
        long price;
        String region;

        Product(int id, long price, String region){
            this.id = id;
            this.price = price;
            this.region = region;
       }

       // getters for id,price and locId
    }

    Class ProductValidation{
       public static validate(List<Product> list, int qty, int totalAvailableQty){
         if(list.size() == 0)
            return //some expceptio
         if(qty < totalAvailableQty){
            return //some exception
         }

         if(qty == totalAvailableQty){
             calculate(list);
          }

        }

        private long[] calculate(List<Product> list){
          return list.stream().mapTolong(p -> p.getPrice()).toArrat()l
        }



    }

    class AsiaProductValidation{


    public static validate(List<Product> list, int qty, int totalAvailableQty){
        // First two conditions stay same as above
        if(list.size() == 0)
            return //some expceptio
         if(qty < totalAvailableQty){
            return //some exception
         }
          //only calculate function changes, how can i use inheritance here to avoid writing above two 
         checks in this class. and also call above two checks when this class is called?

         calculate(list);
    }

     private long[] calculate(List<Product> list){
          return list.stream().mapTolong(p -> p.getPrice() < 100 ? 0 : p.getPrice()).toArray();l
        }
    }

    class RegionCalculate{

      public void call(List<Product> list, int qty, int totalAvailableQty ){
         if(region == Asia){
                AsiaProductValidation.validate(qty,totalAvailableQty )
         }
         else{
             ProductValidation.validate(qty,totalAvailableQty)
         }
       }

    }

    long[] price = {40, 90, 40};

    List<Product> list = new ArrayList();
    list.add(new Product(1,100, "ASIA"));
    list.add(new Product(1,110, "AUS));
    list.add(new Product(1,90, "EUROPE"));

    RegionCalculate reg = new RegionCalculate();
    reg.call(list, 1100, 500);
类产品{
int id,
多头价格;
弦区;
产品(整数id、长价格、字符串区域){
this.id=id;
这个价格=价格;
这个区域=区域;
}
//id、price和locId的getter
}
类产品验证{
公共静态验证(列表、整数数量、整数总可用数量){
if(list.size()==0)
return//someexpectio
如果(数量<总可用数量){
返回//一些异常
}
如果(数量==总可用数量){
计算(列表);
}
}
专用长[]计算(列表){
return list.stream().mapTolong(p->p.getPrice()).toArrat()l
}
}
类AsiaProductValidation{
公共静态验证(列表、整数数量、整数总可用数量){
//前两个条件与上面相同
if(list.size()==0)
return//someexpectio
如果(数量<总可用数量){
返回//一些异常
}
//只计算函数更改,我如何在这里使用继承来避免写上面两个呢
在这个类中进行检查。当调用这个类时,是否也调用上面两个检查?
计算(列表);
}
专用长[]计算(列表){
return list.stream().mapTolong(p->p.getPrice()<100?0:p.getPrice()).toArray();l
}
}
类区域计算{
公共作废调用(列表列表、整数数量、整数总可用数量){
if(地区==亚洲){
AsiaProductValidation.validate(数量,总可用数量)
}
否则{
ProductValidation.validate(数量,总可用数量)
}
}
}
长[]价格={40,90,40};
列表=新的ArrayList();
增加(新产品(1100,“亚洲”);
增加(新产品(1110,“澳大利亚”);
增加(新产品(1,90,“欧洲”);
RegionCalculate reg=新的RegionCalculate();
注册呼叫(列表,1100500);

您希望在类扩展中编写。您应该将AsiaProductValidation用作抽象类,以便它可以轻松扩展ProductValidation的功能:

Class ProductValidation { //Insert Implementation }

abstract class AsiaProductValidation extends ProductValidation {
super.methodName()
//etc }

您需要的是类扩展。标准做法是使用需要实现或需要扩展的抽象方法创建一个抽象类。然后在子类中首先调用超级类方法(例如super.validate())什么意思是
return//some exception
?你的意思是
throw
?你的
static
和非
static
方法的混合不起作用。当你想要可重写的方法时,不要使用
static
。然后,当你从
calculate
中删除
private
修饰符时,你可以在subcl中重写它ass,不需要重写
calc
方法。