0

Conider I have a Class called Entity that has 3 children :
ClientEntity, EmployeeEntity and ProductEntity
I want to have a static attribute _nextId (that gives the new id for a new record) _nextId has a different value in each entity, but its a common attribute to all Entity children.

So my question is how can I put _nextId in Entity class and have methods that change this value, while getting a different value for each child class.

Thank you.

4

2 回答 2

2

假设Entity是抽象的,您可以创建一个:

protected abstract getNextId();

每个具体子类都需要实现的方法。例如,该实现可以依赖于静态计数器。

于 2012-05-31T10:01:55.440 回答
0

你将不得不隐藏它。这意味着您也需要在子类中具有相同的变量。

如果你在超类中声明一个静态变量,所有子类都会访问同一个变量,直到它们通过自己声明一个同名的变量来隐藏它。

于 2012-05-31T10:17:24.017 回答