【问题标题】:how to add two objects?如何添加两个对象?
【发布时间】:2020-05-19 07:39:22
【问题描述】:

我有两个班级:-

public class Employee {

    private String name;
    private String DOB;
    private String techicalSkill;

    Employee(){

    }
    Employee(String name, String DOB, String techicalSkill){
        this.name=name;
        this.DOB=DOB;
        this.techicalSkill=techicalSkill;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDOB() {
        return DOB;
    }

    public void setDOB(String dOB) {
        DOB = dOB;
    }

    public String getTechicalSkill() {
        return techicalSkill;
    }

    public void setTechicalSkill(String techicalSkill) {
        this.techicalSkill = techicalSkill;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((DOB == null) ? 0 : DOB.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((techicalSkill == null) ? 0 : techicalSkill.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (DOB == null) {
            if (other.DOB != null)
                return false;
        } else if (!DOB.equals(other.DOB))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (techicalSkill == null) {
            if (other.techicalSkill != null)
                return false;
        } else if (!techicalSkill.equals(other.techicalSkill))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", DOB=" + DOB + ", techicalSkill=" + techicalSkill + "]";
    }


}

package learning;

public class Person {

    private String address;
    private int age;
    private int weight;

    Person(){

    }
    public Person(String address, int age, int weight) {
        super();
        this.address = address;
        this.age = age;
        this.weight = weight;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((address == null) ? 0 : address.hashCode());
        result = prime * result + age;
        result = prime * result + weight;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (address == null) {
            if (other.address != null)
                return false;
        } else if (!address.equals(other.address))
            return false;
        if (age != other.age)
            return false;
        if (weight != other.weight)
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "Person [address=" + address + ", age=" + age + ", weight=" + weight + "]";
    }



}

现在我创建了一个主类,其中包含详细信息:-

import java.util.ArrayList;

public class Main {

    Employee e1 = new Employee();
    Person p1 = new Person();

    public static void main(String[] args) {

        ArrayList<Employee> arraylist = new ArrayList<>();
        arraylist.add(new Employee("Somduti", "31-08-1992", "Java"));
        arraylist.add(new Employee("abc", "30-01-1995", "Android"));
        arraylist.add(new Employee("xyz", "24-12-1988", "DotNet"));
        arraylist.add(new Employee("Sanj", "01-10-1986", "IOS"));
        arraylist.add(new Employee("Pink", "19-07-1991", "ETL"));

        System.out.println(arraylist);

        ArrayList<Person> arraylist1 = new ArrayList<>();
        arraylist1.add(new Person("India", 27, 57));
        arraylist1.add(new Person("US", 22, 64));
        arraylist1.add(new Person("Australia", 31, 69));
        arraylist1.add(new Person("France", 33, 77));
        arraylist1.add(new Person("Germany", 28, 55));

        System.out.println(arraylist1);

    }

}

我想添加两个对象并打印结果如下:-

name=Somduti, DOB=31-08-1992, techicalSkill=Java address=India, age=27, weight=57

我该怎么做?

【问题讨论】:

  • 员工始终是一个人,定义两者之间的继承,您可以做任何想做的事情。

标签: java list object arraylist


【解决方案1】:

我认为你想要实现的是员工和人之间的关系。有多种方法可以做到这一点。以下是两种常见的解决方案:

  • 关联:将人员字段添加到员工类。这看起来像:“私人人;”在员工类中。
  • 继承:员工是特定类型的人员,因此您可以让员工“扩展”人员类。这看起来像:公共类 Employee 扩展 Person ...

这两种方式各有利弊。例如:继承是一种牢固的关系,在这种情况下您可能需要这种关系。关联是一种较弱的关系类型,因此您可以“替换”员工的个人信息(这可能不是您想要的)。

【讨论】:

    【解决方案2】:

    在 Employee 类中添加以下附加字段,如下所示:

    public class Employee {
    
      private String name;
      private String DOB;
      private String techicalSkill;
      private Person person; // Additional field
    
      Employee() {
    
      }
    
    
      /**
       * @param name
       * @param dOB
       * @param techicalSkill
       * @param person
       */
      public Employee(final String name, final String dOB, final String techicalSkill, final Person person) {
        super();
        this.name = name;
        this.DOB = dOB;
        this.techicalSkill = techicalSkill;
        this.person = person; //additional argument in Constructor
      }
    
    }
    

    PS:Person 类没有变化

    主测试:

     Person person = new Person("India", 27, 57);
     Employee employee = new Employee("Somduti", "31-08-1992", "Java", person);
    
     System.out.println("name= " + employee.getName() + ", DOB= " + employee.getDOB() + ",techicalSkill= " +
     employee.getTechicalSkill() + " address= " + employee.getPerson().getAddress() + ", age= " +
     employee.getPerson().getAge() + " weight= " + employee.getPerson().getWeight());
    

    输出

    name= Somduti, DOB= 31-08-1992,techicalSkill= Java address= India, age= 27 weight= 57
    

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      相关资源
      最近更新 更多