2015年9月16日 星期三

Override 用法

Java 的Override 用法:
public class Employee {
    
    private int empId;
    private String name;
    private String ssn;
    private double salary;

    public Employee(int empId, String name, String ssn, double salary) {
        this.empId = empId;
        this.name = name;
        this.ssn = ssn;
        this.salary = salary;
    }

    public String getDetail(){
        return ("Employee ID : " + getEmpId() +
                " Employee Name: " + getName());
    }

    //......一堆 getter & setter ......
}

public class Manager extends Employee{
    
    private String deptName;

    public Manager(int empId, String name, String ssn, double salary, String deptName) {
        super(empId, name, ssn, salary);
        this.deptName = deptName;
    }
    public String getDetail(){
        return (super.getDetail() + 
                " Department: " + getDeptName());
    }
 //......一堆 getter & setter ......
}

public class Demo {
    public static void main(String[] args){
    
        Employee e = new Employee(101, "Jim Smith", "011-12-2345", 100_000.00);
        Manager m = new Manager(102, "Joan Kern", "012-23-4567",110_450.54, "Marketing");
        System.out.println(e.getDetail());
        System.out.println(m.getDetail());
    }
}