受保护的构造函数可在兄弟类中访问

时间:2017-01-09 13:58:57

标签: java oop inheritance access-modifiers

我有1个抽象类:ItemModel,包含2个实现:ItemLogModelItemLogModelItemModel的构造函数受到保护,但仍可从ItemLogModel访问。我希望受保护的构造函数只能由public abstract class Model<E> implements IModel { private final Repository<? extends IModel, E> _repository; protected E _entity; protected Model(Repository<? extends IModel, E> repository, E entity) this._repository = repository; this._entity = entity; } ... } public class ItemModel extends Model<Item> implements IItem { ... @Override public void setStatus(Status status, String logMessage) throws Exception { super._entity.setStatus(status.toString()); super.update(); // CALLING protected constructor here! new ItemLogModel(this, logMessage); } } public class ItemLogModel extends Model<ItemLog> implements IItemLog { protected ItemLogModel(ItemModel itemModel, String message) throws Exception { super(REPOSITORY, new ItemLog()); super._entity.setAccessPointBean(itemModel.getEntity()); super._entity.setStatus(itemModel.getStatus().toString()); super._entity.setMessage(message); super.save(); } } 的子类访问。有人可以向我解释为什么这是可能的吗?

代码:

istanbul report cobertura --root $WORKSPACE/coverage/coverage.json --dir coverage/

0 个答案:

没有答案