如何为不同的类写一个接口实现?
How to write one interface implementation for different classes?
我想为不同类型的类编写一个实现。
这是 interface
:
public interface ResourcesInterface<T> {
T readJsonContent(String fileName/*, maybe there also must be class type?*/);
}
这是 interface
Student.class
的实现。
在下面的示例中,我尝试读取 JSON 文件并从中接收 Student.class
对象:
import com.fasterxml.jackson.databind.ObjectMapper;
public class StudentResources implements ResourcesInterface<Student> {
@Override
public Student readJsonContent(String fileName) {
Student student = new Student();
ObjectMapper objectMapper = new ObjectMapper();
try {
URL path = getClass().getClassLoader().getResource(fileName);
if (path == null) throw new NullPointerException();
student = objectMapper.readValue(path, Student.class);
} catch (IOException exception) {
exception.printStackTrace();
}
return student;
}
}
所以我不想为每个 class
类型实现这个 interface
我想使用方法 readJsonContent(String)
像这样:
Student student = readFromJson(fileName, Student.class);
AnotherObject object = readFromJson(fileName, AnotherObject.class);
有没有可能只写一个实现?而不是为每个不同的 class
多次实施 interface
?任何想法如何做到这一点?
如果我理解正确的话,您需要一个能够将 JSON 文件解码为对象的通用方法,对吗?如果是这样,那么您不需要接口。您只需要使用如下静态方法创建一个 class:
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Objects;
public class JsonUtil {
private JsonUtil(){}
public static <T> T readJsonContent(String fileName, Class<T> clazz) {
ObjectMapper objectMapper = new ObjectMapper();
try {
URL path = Objects.requireNonNull(clazz.getResource(fileName));
return objectMapper.readValue(path, clazz);
} catch (IOException ex) {
throw new UncheckedIOException("Json decoding error", ex);
}
}
public static void main(String[] args) {
Student s = JsonUtil.readJsonContent("", Student.class);
}
}
我想为不同类型的类编写一个实现。
这是 interface
:
public interface ResourcesInterface<T> {
T readJsonContent(String fileName/*, maybe there also must be class type?*/);
}
这是 interface
Student.class
的实现。
在下面的示例中,我尝试读取 JSON 文件并从中接收 Student.class
对象:
import com.fasterxml.jackson.databind.ObjectMapper;
public class StudentResources implements ResourcesInterface<Student> {
@Override
public Student readJsonContent(String fileName) {
Student student = new Student();
ObjectMapper objectMapper = new ObjectMapper();
try {
URL path = getClass().getClassLoader().getResource(fileName);
if (path == null) throw new NullPointerException();
student = objectMapper.readValue(path, Student.class);
} catch (IOException exception) {
exception.printStackTrace();
}
return student;
}
}
所以我不想为每个 class
类型实现这个 interface
我想使用方法 readJsonContent(String)
像这样:
Student student = readFromJson(fileName, Student.class);
AnotherObject object = readFromJson(fileName, AnotherObject.class);
有没有可能只写一个实现?而不是为每个不同的 class
多次实施 interface
?任何想法如何做到这一点?
如果我理解正确的话,您需要一个能够将 JSON 文件解码为对象的通用方法,对吗?如果是这样,那么您不需要接口。您只需要使用如下静态方法创建一个 class:
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Objects;
public class JsonUtil {
private JsonUtil(){}
public static <T> T readJsonContent(String fileName, Class<T> clazz) {
ObjectMapper objectMapper = new ObjectMapper();
try {
URL path = Objects.requireNonNull(clazz.getResource(fileName));
return objectMapper.readValue(path, clazz);
} catch (IOException ex) {
throw new UncheckedIOException("Json decoding error", ex);
}
}
public static void main(String[] args) {
Student s = JsonUtil.readJsonContent("", Student.class);
}
}