英文:
How to set enum DTO object using enum VO object?
问题
如何将一个对象转换为另一个对象?例如,我有3个类Car(等同于CarVO),CarDTO和CarFactory。问题是,我无法通过Car对象设置CarDTO对象。我应该转换为字符串还是其他什么方法?我会感谢任何帮助!
public class Car {
private Long id;
private String model;
private Engine engine;
private Integer manufacturedYear;
private enum Engine {Petrol, Diesel, Gas}
// 构造函数,setter和getter方法
}
public class CarDTO {
private Long id;
private String model;
private Engine engine;
private Integer manufacturedYear;
private enum Engine {Pertol, Diesel, Gas}
// 构造函数,setter和getter方法
}
public enum CarFactory {
INSTANCE;
CarFactory() {}
public static CarFactory getInstance() {return INSTANCE;}
public CarDTO createCarDTO(Car car) {
CarDTO carDTO = new CarDTO();
carDTO.setId(car.getId());
carDTO.setModel(car.getModel());
carDTO.setEngine(car.getEngine()); // 这里有一个问题
carDTO.setManufacturedYear(car.getManufacturedYear());
return carDTO;
}
}
英文:
How to convert one object to another? For example, I have 3 classes Car (equals to CarVO), CarDTO and CarFactory. The issue in that I can not set an object of CarDTO by an object of Car. Should I convert it to string or what? I would appreciate any help!
public class Car {
private Long id;
private String model;
private Engine engine;
private Integer manufacturedYear;
private enum Engine {Petrol, Diesel, Gas}
// Constructor, setters & getters
}
public class CarDTO {
private Long id;
private String model;
private Engine engine;
private Integer manufacturedYear;
private enum Engine {Pertol, Diesel, Gas}
// Constructor, setters & getters
}
public enum CarFactory {
INSTANCE;
CarFactory() {}
public static CarFactory getInstance() {return INSTANCE;}
public CarDTO createCarDTO(Car car) {
CarDTO carDTO = new CarDTO();
carDTO.setId(car.getId());
carDTO.setModel(car.getModel());
carDTO.setEngine(car.getEngine()); // Here is an issue
carDTO.setManufacturedYear(car.getManufacturedYear());
return carDTO;
}
}
答案1
得分: 0
你只需在工厂方法中对不同的引擎枚举进行映射:
public CarDTO createCarDTO(Car car) {
CarDTO carDTO = new CarDTO();
carDTO.setId(car.getId());
carDTO.setModel(car.getModel());
switch (car.getEngine()) {
case Diesel:
carDTO.setEngine(CarDTO.Engine.Diesel);
case Gas:
carDTO.setEngine(CarDTO.Engine.Gas);
case Petrol:
carDTO.setEngine(CarDTO.Engine.Petrol);
}
carDTO.setManufacturedYear(car.getManufacturedYear());
return carDTO;
}
需要注意的是,为了实现这一点,我不得不将两个 Engine 枚举的可见性更改为包范围。
一个更清晰、更可复用的方法是编写一个单独的方法,将一个 Engine 类型转换为另一个类型,然后在工厂方法中使用它。
……当然,你当然可以采用其他答案中提到的使用映射库或其他更健壮的方法。我只是想向你展示,你直接尝试的方法是可行的。
英文:
You can just do the mapping between the different Engine enums in the factory method:
public CarDTO createCarDTO(Car car) {
CarDTO carDTO = new CarDTO();
carDTO.setId(car.getId());
carDTO.setModel(car.getModel());
switch (car.getEngine()) {
case Diesel:
carDTO.setEngine(CarDTO.Engine.Diesel);
case Gas:
carDTO.setEngine(CarDTO.Engine.Gas);
case Petrol:
carDTO.setEngine(CarDTO.Engine.Pertol);
}
carDTO.setManufacturedYear(car.getManufacturedYear());
return carDTO;
}
Note that I had to change the visibility of the two Engine enums to package scope to do this.
A cleaner, more reusable way would be to write a separate method that converted the one Engine
type to the other, and then use that in the factory method.
...and sure, you can certainly go the way of the other answers of bringing in a mapper library or something else that is more robust. I just wanted to show you that what you were trying to do directly was doable.
答案2
得分: 0
CarDTO
和 Car
看起来像是数据传输对象和模型,例如带有 Hibernate 注解。这些类看起来大致相似,但不能互相替代使用。
要将一个转换为另一个,您有几个选项:
-
创建自己的转换器,将一个类的所有字段复制到另一个类
-
MapStruct 来帮忙!- 使用 MapStruct 可以实现相同的功能,但代码量要少得多。
英文:
CarDTO
and Car
look like data transfer object and model, e.g. with Hybernate annotations. These classes lools mostly similar but cannot be uses one for the other.
To convert one to another you have several options:
- Create your own converters with copy all fields from one class to another
- MapStruct is your friend! - use MapStruct that does the same but with much less code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论