方法参数

 1 public class ParamTest {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8 
 9         System.out.println("Testing tripleValue:");
10         double percent = 10;
11         System.out.println("Before:percent:"+percent);
12         tripleValue(percent);
13         System.out.println("After:percent="+percent);
14         
15         
16         System.out.println("
Testing tripSalary");
17         Employee harry = new Employee("Harry",50000);
18         System.out.println("Before: salary=" + harry.getSalary());
19         tripleSalary(harry);
20         System.out.println("After: salary=" + harry.getSalary());
21         
22         System.out.println("
Testing swap:");
23         Employee a = new Employee("Alice", 70000);
24         Employee b = new Employee("Bob", 60000);
25         System.out.println("Before: a="+ a.getName());
26         System.out.println("Before: b="+ b.getName());
27         swap(a,b);
28         System.out.println("After: a="+ a.getName());
29         System.out.println("After: b="+ b.getName());
30     }
31         public static void tripleValue(double x) {
32             x = 3*x;
33             System.out.println("End of method: x="+x);            
34         }
35         
36         public static void tripleSalary(Employee x) {
37             x.raiseSalary(200);
38             System.out.println("End of method: salary =" + x.getSalary());
39         }
40         
41         public static void swap(Employee x, Employee y) {
42             Employee temp = x;
43             x = y;
44             y = temp;
45             System.out.println("End of method: x=" + x.getName());
46             System.out.println("End of method: y=" + y.getName());
47         }
48     }
49     
50     
51     
52     class Employee {
53         private String name;
54         private double salary;
55         
56         public Employee(String n, double s) {
57             name = n;
58             salary = s;
59         }
60         
61         public String getName() {
62             return name;
63         }
64         
65         public double getSalary() {
66             return salary;
67         }
68         
69         public void raiseSalary(double byPercent) {
70             double raise = salary * byPercent / 100;
71             salary += raise;
72         }
73     
74 
75 }
View Code
原文地址:https://www.cnblogs.com/linst/p/4966638.html