多态(动态调度投标)

时间:2015-11-09 16:45:36

标签: java

 /* To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

class Employee {

    private String name;
    private String address;
    private int number;

    public Employee(Employee emp) {
        System.out.println("Constructing an Employee");
        this.name = emp.name;
        this.address = emp.address;
        this.number = emp.number;
    }

    /*public Employee(String name, String address, int number) {
        System.out.println("Constructing an Employee");
        this.name = name;
        this.address = address;
        this.number = number;
    }*/

    public void mailCheck() {
        System.out.println("Mailing a check to " + this.name
                + " " + this.address);
    }

    public String toString() {
        return name + " " + address + " " + number;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String newAddress) {
        address = newAddress;
    }

    public int getNumber() {
        return number;
    }
}

class Salary extends Employee {

    private double salary; //Annual salary

    public Salary(Salary obj) {
        super(obj);
        setSalary(obj.salary);
    }

    /*public Salary(String name, String address, int number, double Salary) {
        super(name, address, number);
        setSalary(Salary);
    }*/

    public void mailCheck() {
        System.out.println("Within mailCheck of Salary class ");
        System.out.println("Mailing check to " + getName()
                + " with salary " + salary);
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double newSalary) {
        if (newSalary >= 0.0) {
            salary = newSalary;
        }
    }

    public double computePay() {
        System.out.println("Computing salary pay for " + getName());
        return salary / 52;
    }
}

public class JavaApplication3 {

    public static void main(String[] args) {

        Salary s = new Salary("Mohd Mohtashim", "Ambehta,UP", 3, 2000.00); // error why
        System.out.println("Call mailCheck using Salary reference --");
        s.mailCheck();

    }
}

如果我们从构造函数中删除了注释,那么它就没问题了。为什么呢?

1 个答案:

答案 0 :(得分:0)

Salary s = new Salary("Mohd Mohtashim", "Ambehta,UP", 3, 2000.00);你正在调用一个带有多个参数的构造函数,但由于你已将它注释掉,所以它不存在。