What is cool about generics, why use them?

2019-01-01 15:10发布

I thought I'd offer this softball to whomever would like to hit it out of the park. What are generics, what are the advantages of generics, why, where, how should I use them? Please, keep it fairly basic. Thanks.

28条回答
唯独是你
2楼-- · 2019-01-01 15:41

Using generics for collections is just simple and clean. Even if you punt on it everywhere else, the gain from the collections is a win to me.

List<Stuff> stuffList = getStuff();
for(Stuff stuff : stuffList) {
    stuff.do();
}

vs

List stuffList = getStuff();
Iterator i = stuffList.iterator();
while(i.hasNext()) {
    Stuff stuff = (Stuff)i.next();
    stuff.do();
}

or

List stuffList = getStuff();
for(int i = 0; i < stuffList.size(); i++) {
    Stuff stuff = (Stuff)stuffList.get(i);
    stuff.do();
}

That alone is worth the marginal "cost" of generics, and you don't have to be a generic Guru to use this and get value.

查看更多
孤独总比滥情好
3楼-- · 2019-01-01 15:42

The primary advantage, as Mitchel points out, is strong-typing without needing to define multiple classes.

This way you can do stuff like:

List<SomeCustomClass> blah = new List<SomeCustomClass>();
blah[0].SomeCustomFunction();

Without generics, you would have to cast blah[0] to the correct type to access its functions.

查看更多
人气声优
4楼-- · 2019-01-01 15:42

Another advantage of using Generics (especially with Collections/Lists) is you get Compile Time Type Checking. This is really useful when using a Generic List instead of a List of Objects.

查看更多
不再属于我。
5楼-- · 2019-01-01 15:42

I use them for example in a GenericDao implemented with SpringORM and Hibernate which look like this

public abstract class GenericDaoHibernateImpl<T> 
    extends HibernateDaoSupport {

    private Class<T> type;

    public GenericDaoHibernateImpl(Class<T> clazz) {
        type = clazz;
    }

    public void update(T object) {
        getHibernateTemplate().update(object);
    }

    @SuppressWarnings("unchecked")
    public Integer count() {
    return ((Integer) getHibernateTemplate().execute(
        new HibernateCallback() {
            public Object doInHibernate(Session session) {
                    // Code in Hibernate for getting the count
                }
        }));
    }
  .
  .
  .
}

By using generics my implementations of this DAOs force the developer to pass them just the entities they are designed for by just subclassing the GenericDao

public class UserDaoHibernateImpl extends GenericDaoHibernateImpl<User> {
    public UserDaoHibernateImpl() {
        super(User.class);     // This is for giving Hibernate a .class
                               // work with, as generics disappear at runtime
    }

    // Entity specific methods here
}

My little framework is more robust (have things like filtering, lazy-loading, searching). I just simplified here to give you an example

I, like Steve and you, said at the beginning "Too messy and complicated" but now I see its advantages

查看更多
登录 后发表回答