方法重载,就是在类中可以创建多个方法,它们具有相同的名字,但具有不同的参数和不同的定义。调用方法时通过传递给它们的不同个数和类型的参数来决定具体使用哪个方法。- public class Employees {
- private String name;
- private double salary;
- public Employees(String n, double s) {
- this.name = n;
- this.salary = s;
- }
- public Employees(double s,String n) {
- this.name = n;
- this.salary = s;
- }
- public void raiseSalary(double p) {
- double raise = salary * p / 100;
- salary = salary + raise;
- }
- public static void main(String args[]) {
- Employees[] ee = new Employees[3];
- ee[0] = new Employees("张三", 1000);
- ee[1] = new Employees("李四", 2000);
- ee[2] = new Employees(1800,"王五");
- for (int i = 0; i < ee.length; i++) {
- ee[i].raiseSalary(7);
- System.out.println("名字 " + ee[i].getName() + "薪水"
- + ee[i].getSalay(i));
- }
- }
- private double getSalay(int i) {
- // TODO Auto-generated method stub
- return salary;
- }
- private String getName() {
- // TODO Auto-generated method stub
- return null;
- }
- }
复制代码 用到了构造函数的方法重载 |