有保障的广州网站建设,成全看免费观看,电工培训内容,wordpress可视化编辑器不见首先先介绍回调函数的概念。比如客户端client#xff0c;想要调用服务器端server的某个函数为你提供服务。比如炒西红柿炒蛋#xff0c;cookTomato() 这时候#xff0c;服务器端说#xff0c;你先付钱才能为你服务#xff0c;这时候服务器端要调用客户端的getMoney().才能…首先先介绍回调函数的概念。比如客户端client想要调用服务器端server的某个函数为你提供服务。比如炒西红柿炒蛋cookTomato() 这时候服务器端说你先付钱才能为你服务这时候服务器端要调用客户端的getMoney().才能为你继续服务。这里的getMoney就是回调函数了。而且这}getMoney()方法是一个接口方法因为客户端只要给钱就好不管是人民币还是美元我都接受。 public interface Money(){ //要被回调的方法 void float getMoney(); } public class Server { //用Money接口作为参数 public boolean cookTomatoWithEgg(Money money){ System.out.println(有人要西红柿操蛋了,先给我钱,8元以上我才给你炒); if( money.getMoney() 8){ System.out.println(开始洗西红柿); System.out.println(开始炒菜); System.out.println(完成了); return true; } return false; } } public class Client { //吃中饭了想要西红柿操蛋 public static void haveLunch(Server server){ boolean result server.cookTomatoWithEgg(new Money() { Override public float getMoney() { System.out.println(回调方法我付5元人民币); return 5; } }); if(!result){ System.out.println(钱不够); } result server.cookTomatoWithEgg(new Money() { Override public float getMoney() { System.out.println(回调方法我付10元人民币); return 10; } }); if(result){ System.out.println(终于吃到吃西红柿操蛋); } } /** * param args */ public static void main(String[] args) { Client client new Client(); Server server new Server(); client.haveLunch(server); } } 分享http://jxg198168.iteye.com/blog/1885921转载于:https://www.cnblogs.com/lippeixian/p/5027316.html