我有一个用户是某个职业的情况,所以我有类似的情况:
public abstract class Job
{
private static String name;
public static String getJobName() { return name; }
}
和
public class Baker extends Job
{
// I realize this is probably wrong, but not the main point
Baker() { name = "Baker"; }
}
然后我想将 User->Profession 存储在像这样的地图中
Map<User, Class<? extends Job>> uJobs = HashMap<User, Class<? extends Job>>();
uJobs.put(user, Baker.class); // This seems to work
String jobName = uJobs.get(user).getJobName(); // This not so much, and the IDE (eclipse) just somes 'class' specific stuff, not Job specific stuff.
任何建议表示赞赏。这里的核心目标是将 1 个对象映射到一个类,我可以从中调用静态方法,而不需要每个对象的实例。