将不同类型的变量参数传递给java中的方法?

时间:2018-03-27 21:22:19

标签: java

如果你想传递相同类型的可变数字,我们可以使用......像

public boolean isEqual(String... arr)

想知道如何传递可变数量的不同类型?

我想要的是: 在调用者中,我有三种不同的类型:CustomClass,String和Throwable。

void test() {
    CustomClass obj = new CustomClass("name", 12);
    caller("Hello", obj);
    caller("Hello");
    caller(obj, new Throwable("test"));
    // and more caller with different combination of arguments
}

我能想到的是对象,

public static boolean method(Object... arr)

为此,我们可以使用Builder模式,但如果使用Builder模式由于某种原因不容易,传递变量Object是最好的还是有其他选择?

1 个答案:

答案 0 :(得分:1)

如上所述,您可以重载您的特定方法来涵盖您想要处理的不同对象类型,但是,如果(无论出于何种原因)您有一个方法,希望将可选数量的不同对象类型参数传递给它( 以任何顺序 ),然后您可以使用对象作为参数类型:

String objectType = object[indexNumber].getClass().getSimpleName();

并在方法中使用类似:

// Declare and initialize some variables...
byte byt = 12;
char chr = 65;
Point pnt = new Point(100, 200);
String[] array = {"hello", "world"};
List<Integer> list = new ArrayList<>();
Object obj = this;
JLabel myLabel = new JLabel("hello");

// call our method supplying various values, variables, 
// and objects...
call('A', 32.657, 34, 21.3f, "Hello", 2147483647898L, 
        byt, chr, pnt, array, list, myLabel, obj, 
        new Throwable("test"));

/* Example Method Only! This method can accept no
 * arguments or optionally accept any number of 
 * arguments of any Object Type in any Type order.
 *
 * Displays to Console The Object Type supplied in each
 * supplied optional argument.
 * 
 * Returns: Nothing (void)
 */
private void call(Object... object) {
    Object returnObject;
    String msgAdd =  "- Do whatever in method to handle this argument.";

    if (object.length == 0) {
        System.out.println("No Arguments supplied! - " +
                "Do whatever in method to handle this situation.");
        return;
    }

    for (int i = 0; i < object.length; i++) {
        String objType = object[i].getClass().getSimpleName(); 
        System.out.println("Argumant supplied at Index " + i + " is a " + 
                objType + " Object!" + (objType.length() < 6 ? "\t\t" : "\t") + 
                msgAdd);
    }
}

确定在任何特定参数索引处提供的Object类型。

这是一个简单的例子:

Argumant supplied at Index 0 is a Character Object!   - Do whatever in method to handle this argument.
Argumant supplied at Index 1 is a Double Object!      - Do whatever in method to handle this argument.
Argumant supplied at Index 2 is a Integer Object!     - Do whatever in method to handle this argument.
Argumant supplied at Index 3 is a Float Object!       - Do whatever in method to handle this argument.
Argumant supplied at Index 4 is a String Object!      - Do whatever in method to handle this argument.
Argumant supplied at Index 5 is a Long Object!        - Do whatever in method to handle this argument.
Argumant supplied at Index 6 is a Byte Object!        - Do whatever in method to handle this argument.
Argumant supplied at Index 7 is a Character Object!   - Do whatever in method to handle this argument.
Argumant supplied at Index 8 is a Point Object!       - Do whatever in method to handle this argument.
Argumant supplied at Index 9 is a String[] Object!    - Do whatever in method to handle this argument.
Argumant supplied at Index 10 is a ArrayList Object!  - Do whatever in method to handle this argument.
Argumant supplied at Index 11 is a JLabel Object!     - Do whatever in method to handle this argument.
Argumant supplied at Index 12 is a MyClass Object!    - Do whatever in method to handle this argument.
Argumant supplied at Index 13 is a Throwable Object!  - Do whatever in method to handle this argument.

如果您运行上面的代码,您应该在控制台窗口中显示如下内容:

<?php ini_set('display_errors', 1);

$moviePresenter = new \app\Http\model\MoviePresenter; $movieGenreList = $moviePresenter->getMovieGenreList();

function displayMovieList($movieList, $moviePresenter)
{
      $html = '<div class="movie-list row">';   
      $currentURL = \Request::root();  
      foreach ($movieList as $movie) {
          $genreList = $movie->getGenres();
          $movieGenreList = $moviePresenter->getMovieGenreList();

          foreach ($genreList as $genre){
             foreach ($movieGenreList['genres'] as $movieGenre){
                 if($genre->getID() == $movieGenre['id']){
                 $genre->setName($movieGenre['name']);
             }
          }
      }

     $movieID = $movie->getID();   
     $image = $movie->getPosterImage();  
     $poster = '<img class="img-responsive" src="//image.tmdb.org/t/p/w154/'. $image .'" width="195" height="360">';


     $html .= '<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12 d-flex align-items-center flex-column justify-content-center h-100"><a
     href="' . $currentURL . '/movie/' . $movieID . '">';

    $html .= $poster;
    $html .= '</a><div class="moviedetails row">';

    foreach($genreList as $genre){
      $html .= '<a href="'. $currentURL . '/discovery/genre/'. $genre->getID() . '" class="genres">';
      $html .= $genre->getName() . '</a>';
    }

    $html .= '</div></div>';   
    $html .= '</div>';

    return $html; 
}
?>