码迷,mamicode.com
首页 > 其他好文 > 详细

泛型接口Dao层应用

时间:2015-06-02 23:15:46      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

 

 

class User {
    private int id;
    private String name;
    private int age;

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

interface GenericDao<T> {
    List<T> all();

}

interface UserDao extends GenericDao<User> {


}

class UserDaoImpl implements UserDao {
   List<User>userList = new ArrayList<User>();

    public UserDaoImpl() {  //构造添加3行数据
        userList.add( new User(1, "张三", 25));
        userList.add(new User(2, "李四", 30));
        userList.add(new User(3, "王五", 36));
    }


    @Override
    public List all() {
      List<User>list = userList;
      return list;
    }
}

public class InterfaceDemo01 {
    public static void main(String[] args) {
        List<User> list = new UserDaoImpl().all();
        for (User user : list) {
            System.out.println(user.getId() + " " + user.getName() + " " + user.getAge());
        }
    }
}

 

泛型接口Dao层应用

标签:

原文地址:http://www.cnblogs.com/sflik/p/4547893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!