简体   繁体   English

方法,可选参数和/或接受多种数据类型

[英]Methods, Optional Parameters and/or Accepting multiple Data Types

After browsing the web for a while looking for an answer to my question I can't find any posts which seem to offer an effective solution to my issue. 在浏览网页一段时间后,找不到我的问题的答案,我找不到任何似乎可以为我的问题提供有效解决方案的帖子。

Currently I would overload a method for each different data type input. 目前,我将为每个不同的数据类型输入重载一个方法。

For example: 例如:

public void ex1(String in){
public void ex1(int in){

I would imagine there would be a way to condense this into one line 我想会有一种方法可以将其压缩为一行

public void ex1(opt(String in), opt(int in)){

or 要么

public void ex1((String/int) in){

However, as far as I've seen, nobody has presented a reasonable method to use either of these forms. 但是,据我所知,没有人提出使用这两种形式的合理方法。

Overloading does work, but doesn't look as clean as I might like it, so any workarounds or ideas would be appreciated. 重载确实可以工作,但看起来却不尽人意,因此,任何变通方法或想法都将不胜感激。

There is a way, but you acn only provide one optional parameter. 有一种方法,但是您只能提供one可选参数。 That way is to use ... in front of parameter type. 这种方式是在参数类型前面使用...

For ex: 例如:

public void ex1(int someint, String... in)){

In this case String in is an optional parameter which you can provide or not. 在这种情况下, String in是一个可选参数,您可以提供或不提供。 But int someint is a must provide parameter. 但是int someint是必须提供的参数。

So String... in is basically a String[] in . 所以String... in实际上是String... in中的String[] in Since array can be null.... Other than this (atleast I) dont know of any other way to achieve this. 由于array可以为null。...除此之外(至少我)不知道实现此目的的任何其他方法。

This means you also cant do something like one of the parameters has to be entered and other can be ignored . 这意味着您也无法执行某些操作,例如必须输入其中一个参数而其他参数可以忽略 You can ignore just one parameter and other has to be passed. 您可以仅忽略一个参数,而必须传递其他参数。

Also this works with just one parameter per method. 同样,每种方法仅使用一个参数即可。 In other words, just one parameter can be made optional. 换句话说,仅一个参数可以设为可选。 Else you must use method overloading. 否则,您必须使用方法重载。

However (as said in comments), this is not an effective way. 但是(如评论中所述),这不是有效的方法。 Since you will have to write logic for each varaible's possibility and the code would be a great mess, compared to the sweet and effective way by method overloading. 与方法重载的有效方法相比,由于您必须为每个变量的可能性编写逻辑,并且代码将是一团糟。

EDIT: 编辑:

varargs (or this optional parameter) must appear as the last parameter in method. varargs (或此可选参数)必须作为方法中的最后一个参数出现。 Thanks @Makoto for pointing this out. 感谢@Makoto指出这一点。

I would imagine there would be a way to condense this into one line 我想会有一种方法可以将其压缩为一行

You could probably do something like this: 您可能会执行以下操作:

public void ex1(String in1, int in2) { // I don't believe they can have the same name
  // ...
}

As far as an optional variables, I would just set the variables to a value, and inside the method, test to see if that value is equal to that "default" value. 至于可选变量,我只需将变量设置为一个值,然后在方法内部进行测试以查看该值是否等于该“默认”值。

ex1("Hey", 0);

public void ex1(String in1, int in2) {

  boolean useString = true;
  boolean useInt = true;

  if(in1.length() < 1) {
    useString = false;
  }
  if(in2 != 0) {
    useInt = false;
  }

  if(useString) {
    // ...
  }
  if(useInt) {
    // ...
  }

}

EDIT: For one optional variable, see Jaskaranbir Singh's answer. 编辑:对于一个可选变量,请参阅Jaskaranbir Singh的答案。

Try this : 试试这个

public void ex1(Serializable in){
// .....
}

Always works for me. 永远为我工作。 String is Serializable, and for primitive types it use autoboxing and convert automatically int to Integer and so on, and Integer is also Serializable, so ex1(1) will work as expexted. String是可序列化的,对于原始类型,它使用自动装箱并自动将int转换为Integer等,并且Integer也是可序列化的,因此ex1(1)将像扩展那样工作。 All you need is detect real type: 您所需要做的就是检测实型:

if(Integer.class.isInstance(in)){
int intVal = ((Integer)in).intValue();
// ...
} else if(String.class.isInstance(in)){
String strVal = (String) in;
// ...
}

Corner case is when in is null, - in that case its hard to detect real type(still possible), but most of all you dont need that. 极端情况是in为null时-在这种情况下,它很难检测到实型(仍然可能),但大多数情况下您不需要这样做。

what I would do is this: 我会做的是这样的:

public void ex1(String[] args){}

by using this you can have as many data types as you want, if any. 通过使用此选项,您可以根据需要拥有任意数量的数据类型。

The full code would look something like this: 完整的代码如下所示:

public void ex1(String[] args){
   try{
      if(/*I know there is some parse string techique*/args[] == ...){
         //code here
      }
   catch(Exception E){}

hopefully you get the idea, basically you have an array of string an parse it into an int, boolean, ect...and put the code that you would put in the separate functions into the if statements 希望你能理解,基本上你有一个字符串数组,将其解析为一个int,boolean,ect ...,然后将要放在单独的函数中的代码放入if语句中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM