我可以从类中的方法调用向量

时间:2018-05-24 17:43:45

标签: java class vector methods

enter image description here

abstract class Person
{
    String name;
    int age;
}
class Employee extends Person
{
    String subordinate_id;
    String department;
    int rank;

    public Employee(String name, int age, String subordinate_id, String department, int rank)
    {
        this.name = name;
        this.age = age;
        this.subordinate_id = subordinate_id;
        this.department = department;
        this.rank = rank;
    }
    public void print()
    {
        System.out.println("I am a Employee");
        System.out.printf("My name is %s,I am %d years old.\n",name, age);
        System.out.printf("I am a %d level Employee.My employeeNumber is %s.I am working in %s\n",rank, subordinate_id, department);
    }
}
class Manager extends Employee
{
    Vector subordinate =  new Vector<Object>();

    public Manager(String name, int age, String subordinate_id, String department, int rank)
    {
        this.name = name;
        this.age = age;
        this.subordinate_id = subordinate_id;
        this.department = department;
        this.rank = rank;
    }

    public void print()
    {
        System.out.println("I am a Employee");
        System.out.printf("My name is %s,I am %d years old.",name, age);
        System.out.printf("I am a %d level Employee.My employeeNumber is %s.I am working in %s",rank, subordinate_id, department);
        System.out.println("My subordinate has:");
    }
}
//the main was given to me in the question to solve for the rest of the code
class Main
{
    public static void main(String[] args)
    {
        Employee e1 = new Employee("zhangsan", 20, "s101", "d01", 3);
        e1.print();
        System.out.println();

        Employee e2 = new Employee("lisi", 20, "s202", "d02", 4);
        e2.print();
        System.out.println();

        e2 = new Employee("maliu", 20, "s102", "d01", 5);
        e2.print();

        Manager m1 = new Manager("wangwu", 30, "s100", "d01", 9);

        m1.getSubordinates().addElement(e1);
        m1.getSubordinates().addElement(e2);
        m1.print();
        System.out.println();
    }
}
  

我得到了一个main方法,并被要求拿出剩下的代码,有三个类Person类Employee和class Manager。类Manager根据问题有一个向量类型下属.....我无法理解getsubordinate()。addElement();主要是什么意思?

1 个答案:

答案 0 :(得分:0)

首先,我要说这是质量很差的代码(缺少私有访问级别修饰符,新的Vector()等)。

返回代码:

1)你缺少Vector subordinate = new Vector()的getter(getSubordinate());

2)我认为它应该是Vector subordinate = new Vector()

3)我还会考虑使用任何列表实现而不是vector

4)在将所提到的getter添加到Manager类之后,此行m1.getSubordinates()。addElement(e2)只是将Employee添加到vector对象。