【发布时间】:2011-07-15 08:45:25
【问题描述】:
这可能是不可能的,但我正在尝试创建一个只有共享超类的类才能访问的构造函数,这几乎是受保护修饰符的反向逻辑。我假设没有修饰符可以直接完成此操作,但是知道我要完成什么,有什么建议吗?
public Account extends SomeEntity {
//default public
public Account() {
}
// I am wanting this constructor to be only available to sibling classes.
// (those that share the same super class )
<modifier> Account(Element accountElement) {
}
}
public Accounts extends SomeEntity {
private List<Account> accountList;
//default public
public Accounts() {
Account newAcct = new Account(element);
//looped loading up Generic list of Account
this.accountList.add(newAcct);
}
我正在使用 RESTful Web 服务并根据 XML 响应构建对象,问题是如果我 GET 一个帐户列表,要将其构建到一个帐户对象列表中,我必须查询 Web 服务每个单独的帐户,即使我已经拥有这些信息,这似乎完全没有效率。
但是
我不想让我正在构建的 API 的普通用户能够以这种方式实例化帐户对象。 (使用Element)
【问题讨论】:
-
那你为什么要这样做?
-
已更新以解释原因...@Thomas
-
好吧,在这种情况下,这确实支持 bmargulies 的回答。获取您自己的专用包并创建最终课程!
标签: java oop inheritance access-modifiers siblings