【发布时间】:2014-09-23 16:13:29
【问题描述】:
我知道 Java 接口的以下所有规则:
- 您不能实例化接口。
- 接口不包含任何构造函数。
- 接口中的所有方法都是抽象的。
- 接口不能包含实例字段。可以出现在接口中的唯一字段必须声明为 static 和 final。
- 接口未被类扩展;它由一个类实现。
- 一个接口可以扩展多个接口。
现在我的问题是我们如何创建接口 EntityManager 的变量并使用它的方法,如下面的代码:
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateful
public class Movies {
@PersistenceContext(unitName = "movie-unit")
private EntityManager em; // Here declaring a variable of an Interface
public void addMovie(Movie movie) throws Exception {
em.persist(movie); // Here using variable of an Interface to call its method
}
}
请对此稍加说明,以便我清楚了解这段代码的工作原理!
【问题讨论】:
标签: java jakarta-ee interface entitymanager