[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:
wugangca
2019-09-07 03:17:33 -06:00
committed by Josh Cummings
parent 648ef77f5d
commit f24fef9ec6
4 changed files with 164 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}