[英]How is this interface and class being implemented?
我在这个领域相当陌生。
这是一个代码片段:
JAXBContext jobj = JAXBContext.newInstance(Employee.class);
Marshaller mobj = jobj.createMarshaller();
Employee emp1=new Employee(1,"Jordan",50000);
mobj.marshal(emp1, new FileOutputStream("employee.xml"));
现在我知道 JAXBContext 是一个抽象的 class 而 Marshaller 是一个接口。
现在 createMarshaller()(Return type Marshaller) 如何返回 Marshaller object? JAXBContext 是否实现了 Marshaller? 即使实施,它又是如何返回 object 的? 因为我试图在 class 中实现一个接口,然后创建了一个返回该接口的 object 的方法,但它抛出了错误。
试图模拟:
接口:公共接口
Marshall {
public Marshall marshaller();
public int m();
}
Class:
public class JaxB implements Marshall{
@Override
public Marshall marshaller() {
return new Marshall(); //error
}
public int m(){
return 1;
}
}
你能纠正我的 Class 吗?
只需在该 class 中创建一个方法,并添加带有您要发送的参数的返回语句,然后在您要调用该方法的 Class 中创建一个 object,该方法将接收该方法的 output
下面是如何让一个方法返回一个接口的实例:
interface Foo {}
class FooImplementation implements Foo {}
class FooFactory {
Foo getFoo() {
return new FooImplementation();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.