4.2 The method attribute, method parameter passing mechanism, the number of variable process parameter

The properties of a process of

  The method evolved from the traditional function, with the traditional function significantly different: In structured programming, functions are first-class citizens, this program consists of a composition of functions; object-oriented programming language, is like first-class citizens , the entire system consists of one classes. Therefore, in the Java language, the method does not exist independently, the method must belong to a class or object.

The properties of the method:
(1) a procedure similar function. Function but the difference is, the method does not exist, the method must be defined inside the class.
(2) must be performed by a method, a method must be called by a class or object. From the logical point of view, this method belong to the class itself, should class to call
    if the method static modification, which belong to the class itself, should be called with the class;
    if the method is not static modification, which belongs to the object itself, should the object call
[rule] If you call the same class method, the caller may be omitted, then the system will default to add the caller. If the method is non-static method, the caller added this as a default; if the method is a static method, add the class as the caller.

. 1  class Method_attribute 
 2  {
 . 3      // define a common method 
. 4      public  void nonStaticMethod ()
 . 5      {
 . 6          System.out.println ( "This is a common method" );
 7      }
 . 8  
. 9      // define a static method 
10      public  static  void StaticMethod, and ()
 . 11      {
 12 is          System.out.println ( "this is a class method" );
 13      }
 14  
15      // in the same class calls a method in a further method 
16      public  void Test ()
 . 17     {
 18 is  
. 19          the this .nonStaticMethod ();
 20 is          the this .StaticMethod ();
 21 is          StaticMethod, and (); // omitted keynote of class 
22 is      }
 23 is      public  static  void main (String [] args) 
 24      {
 25          var P = new new Method_attribute ();
 26          // At this time test () method in this representative of two objects P 
27          p.test ();        
 28      }
 29  }
 30 ---------- --- output window capture running Java -------
 31 is  this is a common method of
 32  which is a class method
 33 This is a class method
 34 is  
35 output is completed (Processed 0 seconds) - normal termination

 Second, the method of parameter passing mechanism

Java, methods can not exist independently and must be used as the main theme by class or object method call. If the declaration method, comprising the parameter declaration, must specify parameter values ​​for these parameter when invoking the method, the parameter passed to the parameter values ​​call parameters also called arguments.

Java there is only one way of passing parameters: value is passed. The so-called value is passed, it is a copy of the actual parameter (replica) passed within the method, and the parameter itself is not affected.

class ParamTransferTest 
{ 
    public  static  void the swap ( int a, int b) 
    { 
        // achieve variables a and b values exchange
         // define a temporary variable to a value stored 
        var TEMP = a; 
        a = b; 
        b = TEMP; 
        the System .out.println ( "method in the swap, a value:" + a + ", b values:" + B); 
    } 
    public  static  void main (String [] args) 
    { 
        int a =. 6 ;
         int B = . 9 ; 
        the swap (A, B); 
        System.out.println ("After completion of the exchange, a value:" + a + ", b values:" + B); 
    } 
}
 ---------- -------- output window capture running Java - 
the swap in the method, a value: 9, B value: 6 
after the end of the exchange, a value: 6, B value: 9 

output completion (Processed 0 seconds) - normal termination

 Java program () method is executed from main, main () method begins defines a, b two local variables, as shown:

 Program () function is started from the main, when the program enters the swap () method, the system allocates two stack area, the Mian () method of the variables a, b copy of incoming swap () method, instead of a, b itself.

Program swap () method, the variables a, b values ​​of the exchange, after the end of the exchange, where the memory stores:

Two diagrams can be found, Mian () method in the stack area a, b values are not changed, only the program change swap () in the stack area variable a, b. This is the value passed essence: when the system starts to perform a method, system initialization parameter, the value of argument variable is assigned to parameter variables method, where the method is not a variable argument operation.

Look at an example:

. 1  class DataWrap
 2  {
 . 3      int A;
 . 4      int B;
 . 5  }
 . 6  
. 7  public  class ReferenceTransferTest 
 . 8  {
 . 9      public  static  void the swap (DataWrap dw)
 10      {
 . 11          // variables to achieve two members of the following dw value exchange 
12          TEMP = var dw.a;
 13 is          dw.a = dw.b;
 14          dw.b = TEMP;
 15          System.out.println ( "the swap () method, the value of a member variable is:" + dw.a + "b value member variables are:" + dw.b);
16      }
 . 17  
18 is      public  static  void main (String [] args) 
 . 19      {
 20 is          var DW = new new DataWrap ();
 21 is          dw.a =. 6 ;
 22 is          dw.b =. 9 ;
 23 is          the swap (DW);
 24          the System.out. println ( "swap () method, the value of a member variable is:" + dw.a + ", the value of a member variable b is:" + dw.b);
 25      }
 26 is  }
 27 -------- - run Java capture the output window ----------
 28 swap () method, the value of a member variable is: 9, b value member variables are: 6
 29 swap () method where a member of the value of a variable is: 9, the member variable value b: 6
 30  
31 is Output completion (Processed 0 seconds) - normal termination

 The above results swap () method, and in Mian () method in the two variables a, b are changed. It's easy to create the illusion: when you call swap () method, passing the time dw itself, not its replica.

Analyze the following during program execution:

(1) The program execution begins Mian () Method:

var dw DataWrap = new ();
dw.a = 6;
dw.b = 9;

Here creates a DataWrap object and assign a reference variable dw. The object is to save the object itself heap memory, stack memory stored reference variables.

 

(2) performing swap (dw); 

The next main () method starts calling swap () method, mian (0 method is not over, the system will open two stack area for the main () and swap (), used to store mian () and swap () method in the local variables at call swap (method 0, dw passed as actual parameters swap () method, the same value is passed: the main () method in the dw variable assigned to swap () method in the dw parameter, thereby completing swap () dw shape parameter method initializes the memory schematic view of the main () method arguments passed dw swap () method in FIG Showing:

 

 The system only replicates dw variables, but not copy DataWrap object.

  Regardless of the program in the swap () method or when operating dw variable mian () method, the actual operation of the heap are DataWrap object in memory, they refer to the same variable. Therefore exchanged a, b dw two member variables referenced in the swap () method can be seen in the main () method dw reference values ​​of a, b variables referenced variables also changed.

 

 In fact, we create swap () method,

static void public the swap (DataWrap dw); dw change in notation easier to understand, not be confused.

Third, the number of variable process parameter

Java允许定义形参个数可变的参数,从而允许为方法指定数量不确定的形参。如果在定义方法时,在最后一个形参的类型后增加三点(...),则表示该形参可以接受多个参数值,多个参数被当作数组传入。

 1 class  VarArgs
 2 {
 3     public void test(int a,String... names)
 4     {
 5         System.out.println("a参数:"+a);
 6         System.out.println("names数组的长度:"+names.length);
 7         for(int i=0;i<names.length;i++)
 8         {
 9             System.out.println(names[i]);
10         }
11     }
12     public static void main(String... args) 
13     {
14         VarArgs va=new VarArgs();
15         va.test(2,"efdf","fdfs","fjgd");
16         va.test(34,new String[]{"孙悟空","猪八戒"});
17     }
18 }
19 class  VarArgs
20 {
21     public void test(int a,String... names)
22     {
23         System.out.println("a参数:"+a);
24         System.out.println("names数组的长度:"+names.length);
25         for(int i=0;i<names.length;i++)
26         {
27             System.out.println(names[i]);
28         }
29     }
30     public static void main(String... args) 
31     {
32         VarArgs va=new VarArgs();
33         va.test(2,"efdf","fdfs","fjgd");
34         va.test(34,new String[]{"孙悟空","猪八戒"});
35     }
36 }
37 ---------- 运行Java捕获输出窗 ----------
38 a参数:2
39 names数组的长度:3
40 efdf
41 fdfs
42 fjgd
43 a参数:34
44 names数组的长度:2
45 孙悟空
46 猪八戒
47 
48 输出完成 (耗时 0 秒) - 正常终止

va.test(2,"efdf","fdfs","fjgd");   va.test(34,new String[]{"孙悟空","猪八戒"});
这两种多形参传入方法都可以,但是第一种方法更加简洁。

类型[] 形参名

类型...写法的好处是:
调用方法时更加方便,即可直接传入多个元素,系统会自动将它们封装为数组
也可直接传入数组
确定:类型... 这种写法只能作为形参列表的最后一个形参
【暗示】一个方法只能由一个形参个数可变的形参

再看一个例子:

 1 class VarIntTest 
 2 {
 3     public int add(int... num)
 4     {
 5         int result=0;
 6         for (int i=0;i<num.length;i++)
 7         {
 8             result+=num[i];
 9         }
10         return result;
11     }
12     public static void main(String[] args) 
13     {
14         var p=new VarIntTest();
15         System.out.println(p.add(1,2,3,4));
16     }
17 }
18 ---------- 运行Java捕获输出窗 ----------
19 10
20 
21 输出完成 (耗时 0 秒) - 正常终止

 

Guess you like

Origin www.cnblogs.com/weststar/p/12341091.html
Recommended