我仍然不清楚Java中註釋的用途。起初我以爲他們只是作爲文件。但從Google App Engine Datastore看這個文檔,我不太確定。 @PersistenceCapable(identityType = IdentityType.APPLICATION)看起來更像是一個方法簽名。這些Java註釋的功能是什麼?
這種類型的註釋的目的是什麼?它有什麼作用?
import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String firstName;
@Persistent
private String lastName;
@Persistent
private Date hireDate;
public Employee(String firstName, String lastName, Date hireDate) {
this.firstName = firstName;
this.lastName = lastName;
this.hireDate = hireDate;
}
// Accessors for the fields. JDO doesn't use these, but your application does.
public Long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
// ... other accessors...
}
好吧,那麼這個類就是定義你的模型的一種速記方式。在過去,如您所說,您可能在Struts中有一個配置文件,您可以使用文本符號來配置您的模型。但是在這裏我們用註釋做同樣的事情。我假設註釋的工作方式取決於您使用的庫以及該庫如何定義註釋工作。所以,@Persistent可能意味着JDO庫中的一件事,但在另一些庫中則是另一件事。 – Bijou 2009-04-30 19:58:21