Java基础复习---方法参数详解

语言总是采用按值调用。
方法得到的是所有参数值的拷贝,特别是,方法不能修改传递给它的任何参数变量的内容
package MethodPara;

public class ParaTest {
	public static void main(String[] args) {
		/*
		 * Test1:Methods can't modify numeric parameters 方法不能修改值参数
		 */
		System.out.println("Test1:Methods can't modify numeric parameters");
		System.out.println("Testing tripleValue:");
		double precent = 10;
		System.out.println("Before :precent=" + precent);
		tripleValue(precent);
		System.out.println("After :precent=" + precent);

		/*
		 * Test2:Methods can change the state of object parameters 
		 * 方法可以改变对象参数的状态
		 */
		System.out.println("\n");
		System.out
				.println("Test2:Methods can change the state of object parameters");
		System.out.println("Testing tripleSalary:");
		Employee harry = new Employee("Harry", 50000);
		System.out.println("Before:salary =" + harry.getSalary());
		tripleSalary(harry);
		System.out.println("After:salary =" + harry.getSalary());
		
		
		
		/*
		 * Test3:Methods can't attach new objects to object parameters 
		 * 方法不能将新对象附加到对象参数
		 */
		System.out.println("\n");
		System.out
				.println("Test3:Methods can't attach new objects to object parameters");
		System.out.println("Testing swaping:");
		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) {
		// TODO Auto-generated method stub
		x = 3 * x;
		System.out.println("End of method: x =" + x);
	}

	public static void tripleSalary(Employee x) {
		// TODO Auto-generated method stub
		x.raiseSalary(200);
		System.out.println("End of method:salary =" + x.getSalary());
	}
	
	private static void swap(Employee x, Employee y) {
		// TODO Auto-generated method stub
		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 n, double s) {
		name = n;
		salary = s;
	}

	public String getName() {
		return name;
	}

	public double getSalary() {
		return salary;
	}

	public void raiseSalary(double byPrecent) {
		// TODO Auto-generated method stub
		double raise = salary * byPrecent / 100;
		salary += raise;
	}
}

输出:

Test1:Methods can't modify numeric parameters
Testing tripleValue:
Before :precent=10.0
End of method: x =30.0
After :precent=10.0


Test2:Methods can change the state of object parameters
Testing tripleSalary:
Before:salary =50000.0
End of method:salary =150000.0
After:salary =150000.0


Test3:Methods can't attach new objects to object parameters
Testing swaping:
Before:a =Alice
Before:b =Bob
End of method:x =Bob
End of method:y =Alice
After:a =Alice
After:b =Bob


猜你喜欢

转载自blog.csdn.net/u011296723/article/details/52816360