Implicit and explicit parameters Parameter

Excerpt from Java core technology Volume I, 10th edition 108

A method for operating an object instance field and the access thereof. Methods such as:

public void raiseSalary(double byPercent) {
	double raise = salary * byPercent / 100;
	salary += raise; 
}

salary instance field will call this method the object to the new value. Consider the following call:

number007.raiseSalary(5) ;

It results number007.salary 5 percent increase in the value field. Specifically, the call will execute the following command:

double raise = nuaber007.salary * 5 / 100;
nuiber007.salary += raise;

raiseSalary method has two parameters.
The first parameter a parameter called implicit (implicit), Employee class object is in front of the method name appears.
The second parameter is the value in parentheses following the name of the method, which is an explicit argument (Explicit) (some implicit parameter called the target or method call recipient.)
It can be seen clearly explicit parameter listed in the statement of the method, for example double byPercent, a hidden parameter does not appear in the method declaration.
In each method, the keyword this represents a hidden parameter. If desired, you can write raiseSalary method in the following way:

public void raiseSalary(double byPercent) {
	this.salary += raise; 
}	

Some programmers prefer this style, because this instance fields and local variables can be clearly separate areas.

// 完整Employee类

public class Employee {
    private String name;
    private double salary;
    private LocalDate hireDay;

    public Employee(String n, double s, int year, int month, int day) {
        name = n;
        salary = s;
        hireDay = LocalDate.of(year, month, day);
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getHireDay() {
        return hireDay;
    }

    public void raiseSalary(double byPercent) {
        double raise = salary * byPercent / 100;
        salary += raise;
    }
}
Published 40 original articles · won praise 10 · views 4024

Guess you like

Origin blog.csdn.net/qq_41693150/article/details/104292495