JAVA object reference as parameter modification example of object parameters for beginners notes

JAVA using "call by value", all methods to get only a copy of the parameter values.

ParamTest class public 
{ 
   public static void main (String [] args) 
   { 
      System.out.println ( "\ nTesting tripleSalary:"); 
      var = new new Harry the Employee ( "Harry", 50000); 
      System.out.println ( "the Before : the salary = "+ harry.getSalary ()); 
      tripleSalary (Harry); // copy of incoming values, i.e., a copy of the Employee object reference 
      System.out.println (" After: salary = " + harry.getSalary ()) ; 

   } 

   public static void tripleSalary (the Employee x) 
   { 
      x.raiseSalary (200 is); 
      System.out.println ( "End of Method: the salary =" + x.getSalary ()); 
      after this operation // parameter variable x will not reuse, 
      // but the value of the parameter x and the common reference harry Employee object will change 
   } 


class Employee
{
   String name Private; 
   Private Double the salary; 

   public the Employee (n-String, Double S) I // constructor method is a 
   { 
      name = n-; 
      the salary = S; 
   } 

   public String getName () // I accessor method is a 
   { 
      return name; 
   } 

   public getSalary Double () // I accessor method is a 
   { 
      return the salary; 
   } 

   public void raiseSalary (Double byPercent) // I is a method to change an 
   { 
      Double * The raise the salary = byPercent / 100; 
      the salary + = The raise ; 
   } 
}

   / * Changer: After calling this method, the parameter value (state) of the object will change is the method * / We call for changes
   / * accessor method: only way to access the object without modifying the object, which we call changer method * /
  / * constructor method: the same name as the class constructor method, always with the new operator, it will run when configured in its building object classes and instances over the field initialized state * /

Guess you like

Origin www.cnblogs.com/MR---Zhao/p/12576963.html