Java 如何扩展只需要新参数的方法?

Java 如何扩展只需要新参数的方法?,java,methods,Java,Methods,假设我们有这样一种方法: public List<Animal> findByKeyword(String keyword){ List<Animal> animals = new ArrayList<Animal>(); // Validate keyword is not null etc.. // hit db for animals with name like keyword and add to list // hi

假设我们有这样一种方法:

public List<Animal> findByKeyword(String keyword){
    List<Animal> animals = new ArrayList<Animal>();
    // Validate keyword is not null etc..
    // hit db for animals with name like keyword and add to list
    // hit db for animals with owner name like keyword and add to list
    // hit db for animals with nick name like keyword and add to list
    // remove duplicates in the list
    return animals;
 }
所以我不想复制粘贴整个方法。但是我也不想将参数添加到现有方法中,因为该方法在很多地方都使用(默认情况下excludeDead=false..假设这是一个新的要求..)

但问题是,我不想做这样的改变:

     // remove duplicates in the list 
     // if(excludeDead) removeDeadAnimalsFromTheList
因为如果ExludeDad是真的,我会不必要地装载所有的死动物。。所以我想做的是修改:

    // hit db for animals with name like keyword and add to list BASED ON THE GIVEN BOOLEAN VALUE
扩展这段代码的最佳方式是什么?有没有办法使参数成为可选的


请不要假设这是一个真正的代码,我只是想让它简单

调用新实现中的第一个方法,然后将该条件应用于结果列表

public List<Animal> findByKeyword(String keyword, boolean excludeDead){
     List<Animal> list = findByKeyword(keyword);

     // Now apply the condition on the list above.
}
public List findByKeyword(字符串关键字,boolean excludeDead){
List List=findByKeyword(关键字);
//现在应用上面列表中的条件。
}

调用新实现中的第一个方法,然后将条件应用于结果列表

public List<Animal> findByKeyword(String keyword, boolean excludeDead){
     List<Animal> list = findByKeyword(keyword);

     // Now apply the condition on the list above.
}
public List findByKeyword(字符串关键字,boolean excludeDead){
List List=findByKeyword(关键字);
//现在应用上面列表中的条件。
}
重载方法:

public List<Animal> findByKeyword(String keyword, boolean excludeDead) {
   // the original code here
   if (excludeDead) {
       // additional code in case excludeDead is true
   }
}

public List<Animal> findByKeyword(String keyword) {
    return findByKeyword(keyword, false);
}
public List findByKeyword(字符串关键字,boolean excludeDead){
//这里是原始代码
如果(不包括死亡){
//如果excludeDead为true,则添加代码
}
}
公共列表findByKeyword(字符串关键字){
返回findByKeyword(关键字,false);
}
重载方法:

public List<Animal> findByKeyword(String keyword, boolean excludeDead) {
   // the original code here
   if (excludeDead) {
       // additional code in case excludeDead is true
   }
}

public List<Animal> findByKeyword(String keyword) {
    return findByKeyword(keyword, false);
}
public List findByKeyword(字符串关键字,boolean excludeDead){
//这里是原始代码
如果(不包括死亡){
//如果excludeDead为true,则添加代码
}
}
公共列表findByKeyword(字符串关键字){
返回findByKeyword(关键字,false);
}

使用默认值从第一个方法调用第二个方法

public List<Animal> findByKeyword(String keyword){
   return findByKeyword(keyword, false); //your default value
 }

 public List<Animal> findByKeyword(String keyword,boolean excludeDead){
      List<Animal> animals = new ArrayList<Animal>();
    // Validate keyword is not null etc..
    // hit db for animals with name like keyword and add to list
    // hit db for animals with owner name like keyword and add to list
    // hit db for animals with nick name like keyword and add to list
    // remove duplicates in the list
    return animals;
 }
公共列表findByKeyword(字符串关键字){
返回findByKeyword(关键字,false);//您的默认值
}
公共列表findByKeyword(字符串关键字,布尔排除死){
列出动物=新建ArrayList();
//验证关键字不为空等。。
//点击db查找具有类似名称关键字的动物并添加到列表中
//为拥有者名称(如关键字)的动物点击db并添加到列表中
//用昵称类关键字点击db并添加到列表
//删除列表中的重复项
归还动物;
}

使用默认值从第一个方法调用第二个方法

public List<Animal> findByKeyword(String keyword){
   return findByKeyword(keyword, false); //your default value
 }

 public List<Animal> findByKeyword(String keyword,boolean excludeDead){
      List<Animal> animals = new ArrayList<Animal>();
    // Validate keyword is not null etc..
    // hit db for animals with name like keyword and add to list
    // hit db for animals with owner name like keyword and add to list
    // hit db for animals with nick name like keyword and add to list
    // remove duplicates in the list
    return animals;
 }
公共列表findByKeyword(字符串关键字){
返回findByKeyword(关键字,false);//您的默认值
}
公共列表findByKeyword(字符串关键字,布尔排除死){
列出动物=新建ArrayList();
//验证关键字不为空等。。
//点击db查找具有类似名称关键字的动物并添加到列表中
//为拥有者名称(如关键字)的动物点击db并添加到列表中
//用昵称类关键字点击db并添加到列表
//删除列表中的重复项
归还动物;
}

您应该根据需要创建一个新方法

public List<Animal> findByKeyword(String keyword, boolean excludeDead){
    List<Animal> animals = new ArrayList<Animal>();
    // Validate keyword is not null etc..
    // hit db for animals with name like keyword and add to list
    // hit db for animals with owner name like keyword and add to list
    // hit db for animals with nick name like keyword and add to list
    // remove duplicates in the list
// remove duplicates in the list 
     // if(excludeDead) removeDeadAnimalsFromTheList
    return animals;
 }
public List findByKeyword(字符串关键字,boolean excludeDead){
列出动物=新建ArrayList();
//验证关键字不为空等。。
//点击db查找具有类似名称关键字的动物并添加到列表中
//为拥有者名称(如关键字)的动物点击db并添加到列表中
//用昵称类关键字点击db并添加到列表
//删除列表中的重复项
//删除列表中的重复项
//如果(不包括死亡)从列表中删除了死亡动物
归还动物;
}
并从现有方法中调用它

 public List<Animal> findByKeyword(String keyword) {
     return  findByKeyword(keyword,false);
 }
公共列表findByKeyword(字符串关键字){
返回findByKeyword(关键字,false);
}

希望这能有所帮助。

您应该创建一个新的方法

public List<Animal> findByKeyword(String keyword, boolean excludeDead){
    List<Animal> animals = new ArrayList<Animal>();
    // Validate keyword is not null etc..
    // hit db for animals with name like keyword and add to list
    // hit db for animals with owner name like keyword and add to list
    // hit db for animals with nick name like keyword and add to list
    // remove duplicates in the list
// remove duplicates in the list 
     // if(excludeDead) removeDeadAnimalsFromTheList
    return animals;
 }
public List findByKeyword(字符串关键字,boolean excludeDead){
列出动物=新建ArrayList();
//验证关键字不为空等。。
//点击db查找具有类似名称关键字的动物并添加到列表中
//为拥有者名称(如关键字)的动物点击db并添加到列表中
//用昵称类关键字点击db并添加到列表
//删除列表中的重复项
//删除列表中的重复项
//如果(不包括死亡)从列表中删除了死亡动物
归还动物;
}
并从现有方法中调用它

 public List<Animal> findByKeyword(String keyword) {
     return  findByKeyword(keyword,false);
 }
公共列表findByKeyword(字符串关键字){
返回findByKeyword(关键字,false);
}

希望这能有所帮助。

对不起,我的问题完全错了。。我会编辑它。我想每次都用新参数点击db,这样我就不会加载所有的死动物了..编辑了我的问题。很抱歉,第一个版本不正确。我错过了最重要的部分。对不起,我的问题完全错了。。我会编辑它。我想每次都用新参数点击db,这样我就不会加载所有的死动物了..编辑了我的问题。很抱歉,第一个版本不正确。我错过了最重要的部分。这基本上就是我的建议。让原始方法调用新方法。基本上,你会“剪切粘贴”,这比“复制粘贴”要好得多。这基本上就是我的建议。让原始方法调用新方法。基本上,你将“剪切粘贴”这比“复制粘贴”要好得多。这将是含糊不清的,而不是编译-注意没有什么不同arguments@user2310289:噢,忘了删除第二个方法中的布尔参数。错过了,对不起。现在编辑。这将是含糊不清的,没有编译-注意没有不同arguments@user2310289:噢,忘了删除第二个方法中的布尔参数。错过了,对不起。现在编辑。