开发者

Setting the type of a field in a superclass from a subclass (Java)

I am writing a project on Google App Engine, within it I have a number of abstract classes that I hope I will be able to use in my future projects, and a number of concrete classes inheriting from them. Among other abstract classes I have an abstract servlet that does user management, and I hava an abstract user. The AbstractUser has all the necessary fields and methods for storing it in the datastore and telling whether the user is registered with my service or not. It does not implement any project specific functionality. The abstract servlet that manages users, refers only to the methods declared in the AbstractUser class, which allows it to generate links for logging in, logging out and registering (for unregistered users). In order to implement the project-specific user functionality I need to subclass the Abstract user. The servlets I use in my project are all indirect descendants from that abstract user management servlet, and the user is a protected field in it, so the descendant servlets can use it as their own field. However, whenever i want to access any project specific method of the concrete user, i need to cast it to that type. i.e.

(abstract user managing servlet)
...
AbstractUser user = getUser();
...
abstract protected AbstractUser getUser();


(project-specific abstract servlet)
@Override
protected AbstractUser getUser() {
   return MyUserFactory.getUser();
}

any other project specific servlet:

int a = ((ConcreteUser) user).getA();

Well, what i'd like to do is to somehow make the type of “user” in the superclass depend on something in the project-specific abstract class. Is it at all possible?

And i don't want to move all the user-management stuff 开发者_如何学编程into a project-specific layer, for i would like to have it for my future projects already written :)

Thank you for your help.


Make your AbstractServlet generic:

public class AbstractServlet<T extends AbstractUser> { 
    private T user;
    protected abstract T getUser();
    ...

Then you can make the implementations use a specific subclass of AbstractUser:

public class ConcreteServlet extends AbstractServlet<ConcreteUser> { ...

Then you will no longer need to cast, as any place you have T in the base class, your implementation will now refer to ConcreteUser:

@Override
protected abstract ConcreteUser getUser() { .. }


I think what you need is generic class:

class AbstractUserManager<UserType extends AbstractUser> {
    protected UserType user;    

    public UserType getUser() {
        return user;
    }

    public void addUser(UserType u) {
        this.user = u; 
    }

    ...some other methods that use AbstractUser and dont need specific functionality...
}

//Class used for managing AbstractUsers with no extra features
class SimpleUserManager extends AbstractUserManager<AbstractUser> {
//No body as AbstractUserManager is enough
}

//Class that manages CrazyUsers
class CrazyUserManager extends AbstractUserManager<CrazyUser> {
    public CrazyUserManager() {
        this.setUser(new CrazyUser());
    }

    public someNewFunction() {
        this.getUser().someCrazyActionOnlyCrazyUsersHave();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜