[英]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.