代码改变世界

17_8_14 回调函数

2017-08-14 17:18  小歪1991  阅读(102)  评论(0编辑  收藏  举报

Class A 中 call() 方法 调用 接口 I 的 方法ss(),而 I的方法的实现分为三种:

1.匿名实现 a.call(new I(){public void ss(){.....}})
2.另一个类 B 来实现 I
3.lambda,直接用接口中参数和方法
4.其他方法可以创建 接口,类似:AAA.createDataSource

eg:

	public List<Country> selectCountries() {
                //方法3.
		return jdbcTemplate.query("select * from country", (ResultSet rs, int rowNum)->{
			Country country = new Country();
			country.setId(rs.getInt("id"));
			country.setName(rs.getString("name"));
			return country;
		});
		
                //方法1
		return  jdbcTemplate.query("s", new RowMapper<Country>(){
			@Override
			public Country mapRow(ResultSet rs, int rowNum) throws SQLException {
				return null;
			}
		});
		
	}

参考