gpt4 book ai didi

java - 如何添加两个对象?

转载 作者:行者123 更新时间:2023-11-30 05:18:08 27 4
gpt4 key购买 nike

我有两门课:-

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

我该怎么做?

最佳答案

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

  • 关联:将人员字段添加到员工类中。这看起来像:“私有(private)人;”在员工阶层内。
  • 继承:员工是特定类型的人员,因此您可以让员工“扩展”人员类别。这看起来像:public class Employee extends Person ...

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

关于java - 如何添加两个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60041089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com