Java从属性类

时间:2016-04-13 18:21:46

标签: java

我是java中对象和类的新手。我想知道如何从属性对象中获取数据。我有4个班级

public class Personnel {
  private ArrayList<Employee> employees = new ArrayList<Employee>();

  public void addEmployee(Employee employee){
    employees.add(employee);
  }

  public ArrayList<Employee> getEmployees() {
    return this.employees;
  }

}

public class Employee{
  /**DATAFIELDS*/
  private String name;
  Address address;
  PayInfo pay;

  //Constructor
  //methods
  .....
}

public class PayInfo {
  /**DATAFIELDS*/
  private double salary;
  private boolean isFullTime;

  //Constructors
  //Methods
  ....
}

public class Address {
  /**DATAFIELDS*/
  private String streetAddress;
  private String city;
  private String state;
  private String zipCode;

  //Constructors
  //Methods
  .....
}

我创建了payInfo和地址对象并添加到了employee对象中。最后将该employee对象添加到Personnel对象并保存在ArrayList中。但我的问题是,是否有任何方法可以从Personnel对象中检索所有这些信息,而不是通过每个对象?提前致谢 !! 我的意思是我想从我的Main方法中得到类似的东西,

public static void main(String[] args) {
    //Create CODPersonnel Object
    CODPersonnel personnelEmployee = new CODPersonnel();
    //Address Object for emp1 Object
    Address addressEmp1 = new Address();
    addressEmp1.setStreetAddress("637 67th Place");
    addressEmp1.setCity("Willowbrook");
    addressEmp1.setState("IL");
    addressEmp1.setZipCode("60527");

    //PayInfo for emp1 object
    PayInfo emp1PayInfo = new PayInfo();
    emp1PayInfo.setFullTime(true);
    emp1PayInfo.setSalary(85000.0);

    //Create the Employee object
    //and set attributes
    Employee emp1 = new Employee();
    emp1.setName("Samantha Simmons");
    emp1.setAddress(addressEmp1);
    emp1.setPay(emp1PayInfo);

    //Add objects to CODPersonnel objects
    personnelEmployee.addEmployee(emp1);
}

我想通过Personnel对象检索所有信息,例如,
        的System.out.println(personnelEmployee.getEmployees(......);

,或者      System.out.println(personnelEmployee.getAddress.streetName..etc,)之类的东西。我知道这可能听起来很愚蠢但是有什么办法吗?

2 个答案:

答案 0 :(得分:0)

不确定你在问什么。您可以从Personnel中的ArrayList中的Employee对象访问信息,假设您在Employee中有适当的getter和setter,返回其PayInfo和Address。

答案 1 :(得分:0)

您可以将AT+CMGF? +CMGF: 1 OK AT+CSCS? +CSCS: "GSM" OK PayInfo放入Address课程。每个Details可以由Employee个对象组成。然后,您可以从Details类访问Details对象:

Personnel

我将您的class Personnel { private Map<String, Employee> employees = new HashMap<>(); public void addEmployee(Employee employee) { employees.put(employee.getName(), employee); } public Details getEmployeeDetails(String name) { return employees.get(name).getDetails(); } } class Employee { private String name; //could be stored in Details private Details details; public Employee(String name, Details details) { this.details = details; } public Details getDetails() { return details; } public String getName() { return name; } } class Details { private PayInfo payInfo; private Address address; public Details(PayInfo payInfo, Address address) { this.payInfo = payInfo; this.address = address; } public PayInfo getPayInfo() { return payInfo; } public Address getAddress() { return address; } } 切换为List,因此您可以轻松地按姓名访问每位员工。然后你可以这样做:

Map

此设计强烈违反了Law of Demeter,因此,如果您正在寻找一种不违反设计的设计,我会非常乐意在更强大的设计中进行编辑。