【发布时间】:2022-01-09 11:10:10
【问题描述】:
我正在学习 Java,想知道是否可以在一个构造函数中转换下面的两个构造函数,但可以使用泛型或类似的东西。
public CustomPage(User userInput) {
List<User> user = new ArrayList<User>();
user.add(userInput);
this.content = (List<T>) user;
this.totalElements = (long) 1;
this.totalPages = 1;
this.pageSize = 1;
this.pageNumber = 1;
this.timestamp = new Date().toInstant().toString();
}
public CustomPage(Person personInput) {
List<Person> person = new ArrayList<Person>();
person.add(personInput);
this.content = (List<T>) person;
this.totalElements = (long) 1;
this.totalPages = 1;
this.pageSize = 1;
this.pageNumber = 1;
this.timestamp = new Date().toInstant().toString();
}
【问题讨论】:
-
我用正确的代码编辑了我的问题。