Skip to content

Commit b64b962

Browse files
committed
参数传递
1 parent 6900d20 commit b64b962

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Part3/ParamTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
public class ParamTest {
2+
public static void main(String[] args) {
3+
System.out.println("Testing tripleValue:");
4+
double percent = 10;
5+
System.out.println("Before: percent = " + percent);
6+
tripleValue(percent);
7+
System.out.println("After: percent = " + percent);
8+
9+
System.out.println("\nTesting tripleSalary:");
10+
Employee harry = new Employee("Harrt", 50000);
11+
System.out.println("Before: salary = " + harry.getSalary());
12+
tripleSalary(harry);
13+
System.out.println("After: salary = " + harry.getSalary());
14+
15+
System.out.println("\nTesting swap:");
16+
Employee a = new Employee("Alice", 70000);
17+
Employee b = new Employee("Bob", 60000);
18+
System.out.println("Before: a = " + a.getName());
19+
System.out.println("Before: b = " + b.getName());
20+
swap(a, b);
21+
System.out.println("After: a = " + a.getName());
22+
System.out.println("After: b = " + b.getName());
23+
}
24+
25+
public static void tripleValue(double x) {
26+
x = 3 * x;
27+
System.out.println("End of method: x = " + x);
28+
}
29+
30+
public static void tripleSalary(Employee x) {
31+
x.raiseSalary(200);
32+
System.out.println("End of method: salary = " + x.getSalary());
33+
}
34+
35+
public static void swap(Employee x, Employee y) {
36+
Employee temp = x;
37+
x = y;
38+
y = temp;
39+
System.out.println("End of method: x = " + x.getName());
40+
System.out.println("End of method: y = " + y.getName());
41+
}
42+
}
43+
44+
class Employee {
45+
private String name;
46+
private double salary;
47+
48+
public Employee(String n, double s) {
49+
name = n;
50+
salary = s;
51+
}
52+
53+
public String getName() {
54+
return name;
55+
}
56+
57+
public double getSalary() {
58+
return salary;
59+
}
60+
61+
public void raiseSalary(double byPercent) {
62+
double raise = salary * byPercent / 100;
63+
salary += raise;
64+
}
65+
}
66+

0 commit comments

Comments
 (0)