ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Methods with Variable Argument Lists (var-args)

Methods with Variable Argument Lists (var-args)

原创 Linux操作系统 作者:keeking 时间:2009-12-23 20:43:21 0 删除 编辑
■ arguments The things you specify between the parentheses when you're
  invoking a method:
  doStuff("a", 2);     // invoking doStuff, so a & 2 are arguments
■ parameters The things in the method's signature that indicate what the
  method must receive when it's invoked:

■ Var-arg type When you declare a var-arg parameter, you must specify the
  type of the argument(s) this parameter of your method can receive. (This can
  be a primitive type or an object type.)
■ Basic syntax To declare a method using a var-arg parameter, you follow the
  type with an ellipsis (...), a space, and then the name of the array that will
  hold the parameters received.
■ Other parameters      It's legal to have other parameters in a method that uses
  a var-arg.
■ Var-args limits The var-arg must be the last parameter in the method's
  signature, and you can have only one var-arg in a method.

Let's look at some legal and illegal var-arg declarations:
Legal:
void doStuff(int... x) { }            // expects from 0 to many ints
                                      // as parameters
void doStuff2(char c, int... x) { } // expects first a char,
                                                   // then 0 to many ints
void doStuff3(Animal... animal) { }                // 0 to many Animals
Illegal:
void doStuff4(int x...) { }                           // bad syntax
void doStuff5(int... x, char... y) { }                // too many var-args
void doStuff6(String... s, byte b) { }                // var-arg must be last

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7899089/viewspace-623463/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: Instance Variables
请登录后发表评论 登录
全部评论

注册时间:2009-07-23

  • 博文量
    194
  • 访问量
    287363