[BAEL-3210] - Java Copy Constructor (#7706)
* [BAEL-3210] - Copy constructors in Java * [BAEL-3210] - Copy constructors in Java * Change indentation space based on code style instructions.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Employee {
|
||||
|
||||
protected int id;
|
||||
protected String name;
|
||||
protected Date startDate;
|
||||
|
||||
public Employee(int id, String name, Date startDate) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.startDate = startDate;
|
||||
}
|
||||
|
||||
public Employee(Employee employee) {
|
||||
this.id = employee.id;
|
||||
this.name = employee.name;
|
||||
this.startDate = new Date(employee.startDate.getTime());
|
||||
}
|
||||
|
||||
Date getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
public Employee copy() {
|
||||
return new Employee(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Manager extends Employee {
|
||||
|
||||
private List<Employee> directReports;
|
||||
|
||||
public Manager(int id, String name, Date startDate, List<Employee> directReports) {
|
||||
super(id, name, startDate);
|
||||
this.directReports = directReports;
|
||||
}
|
||||
|
||||
public Manager(Manager manager) {
|
||||
super(manager.id, manager.name, manager.startDate);
|
||||
this.directReports = manager.directReports.stream()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee copy() {
|
||||
return new Manager(this);
|
||||
}
|
||||
|
||||
List<Employee> getDirectReport() {
|
||||
return this.directReports;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user