接口

一:为啥要使用接口?不适用接口不一样可以编程吗?

二:解耦,更加灵活;

三:接口就是一种规范,只要是符合规范的都可以使用;

例:

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

public class MyTest {
    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();
        UserServiceImpl service = (UserServiceImpl) userService;
        service.setUserDao(new UserDaoImpl());
        service.setUserDao(new StudyDaoImpl());
    }
}

 

posted @ 2020-12-08 15:15  nbg  阅读(40)  评论(0编辑  收藏  举报