public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
//Test1
System.out.println("Test1");
double percent = 10;
System.out.println("befor percent=" + percent);
tripleValue(percent);
System.out.println("After percent" + percent);
//Text2
System.out.println("Text2");
Employee harry = new Employee("Harry", 500);
System.out.println("befor salary" + harry.getSalary());
tripleSalary1(harry);
System.out.println("After salary" + harry.getSalary());
System.out.println("Text3");
Employee harry2 = new Employee("Harry", 500);
System.out.println("befor salary" + harry2.getSalary());
tripleSalary2(harry2);
System.out.println("After salary" + harry2.getSalary());
//Text4
System.out.println("Test4");
System.out.println("\nTest4:");
Employee a = new Employee("Alice", 70000);
Employee b = new Employee("Bob", 60000);
System.out.println("Before: a=" + a.getName());
System.out.println("Before: b=" + b. getName());
swap(a, b);
System.out.println("After: a=" + a.getName());
System.out.println("After: b=" + b.getName());
}
public static void tripleValue(double x) {
x=x*3;
System.out.println("End of method x=" + x);
}
public static void tripleSalary1(Employee x) {
x.raiseSalary(200);
System.out.println("End of method: salary=" + x.getSalary());
}
public static void tripleSalary2(Employee x) {
double a=x.getSalary();
a=a*3;
x.setSalary(a);
System.out.println("End of method: salary=" + x.getSalary());
}
public static void swap(Employee x, Employee y) {
Employee temp=x;
x=y;
y=temp;
System.out.println("End of method: x=" + x.getName());
System.out.println("End of method: y=" + y.getName());
}
}
class Employee{
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void raiseSalary(double byPercent) {
double raise=salary*byPercent/100;
salary=raise;
}
}
運(yùn)行結(jié)果
Hello World!
Test1
befor percent=10.0
End of method x=30.0
After percent10.0
Text2
befor salary500.0
End of method: salary=1000.0
After salary1000.0
Text3
befor salary500.0
End of method: salary=1500.0
After salary1500.0
Test4
Test4:
Before: a=Alice
Before: b=Bob
End of method: x=Bob
End of method: y=Alice
After: a=Alice
After: b=Bob
在這個(gè)程序中赂蠢, 首先試圖將一個(gè)值參數(shù)的值
提高兩倍望伦,但沒(méi)有成功:
Testing tripleValue:
Before: percent=10.0
End of method: x:30.0
After: percent=10.0
隨后秫逝, 成功地將一個(gè)雇員的薪金提高了兩倍:
Testing tripleSalary:
Before: salary=50000.0
End of method: salary=150000.0
After: salary=150000.0
方法結(jié)束之后贺辰, harry 引用的對(duì)象狀態(tài)發(fā)生了改變户盯。這是因?yàn)檫@個(gè)方法可以通過(guò)對(duì)象引用
的拷貝修改所引用的對(duì)象狀態(tài)。
最后饲化,程序演示了 swap 方法的失敗效果:
Testing swap:
Before: a=Alice
Before: b=Bob
End of method: x=Bob
End of method: y=Alice
After: a=Alice
After: b=Bob
可以看出莽鸭, 參數(shù)變量 X 和 y 交換了, 但是變量 a 和 b 沒(méi)有受到影響吃靠。