⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boss.java

📁 源码为科学出版社出版的英文<java设计模式>(影印版)所用的所有例子程序
💻 JAVA
字号:
import java.util.*;

public class Boss extends Employee {
    Vector employees; 

    public Boss(String _name, long _salary) {
        super(_name, _salary);
        leaf = false;
        employees = new Vector();
    }
//--------------------------------------
    public Boss(Employee _parent, String _name, long _salary) {
        super(_parent, _name, _salary);
        leaf = false;
        employees = new Vector();
    }
    //--------------------------------------
    public Boss(Employee emp) {
        //promotes an employee position to a Boss
        //and thus allows it to have employees
        super(emp.getName (), emp.getSalary());
        employees = new Vector();
        leaf = false;
    }
    //--------------------------------------
    public boolean add(Employee e) throws NoSuchElementException {
        employees.add(e); 
        return true;
    }
    //--------------------------------------
    public void remove(Employee e) throws NoSuchElementException {
        employees.removeElement(e);
    }
    //--------------------------------------
    public Enumeration subordinates () {
        return employees.elements ();
    }
    //--------------------------------------
    public Employee getChild(String s) throws NoSuchElementException {

        Employee newEmp = null;

        if (getName().equals(s))
            return this;
        else {
            boolean found = false;
            Enumeration e = subordinates();
            while (e.hasMoreElements() && (! found)) {
                newEmp = (Employee)e.nextElement();
                found = newEmp.getName().equals(s);
                if (! found) {
                    if (! newEmp.isLeaf ()) {
                        newEmp = newEmp.getChild(s);
                    } else
                        newEmp = null;
                    found =(newEmp != null);
                }
            }
            if (found)
                return newEmp;
            else
                return null;
        }
    }
    //--------------------------------------
    public long getSalaries() {
        long sum = salary;
        for (int i = 0; i < employees.size(); i++) {
            sum += ((Employee)employees.elementAt(i)).getSalaries();
        }
        return sum;
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -