package com.imooc; /** * 用户身份认证标记接口 */ @FunctionalInterface public interface IUserCredential { /** * 通过用户账号,验证用户身份信息的接口 * @param username 要验证的用户账号 * @return 返回身份信息[系统管理员、用户管理员、普通用户] */ String verifyUser(String username); // boolean test(); default String getCredential(String username) { // 模拟方法 if ("admin".equals(username)) { return "admin + 系统管理员用户"; } else if("manager".equals(username)){ return "manager + 用户管理员用户"; } else { return "commons + 普通会员用户"; } } }
package com.imooc; /** * 消息传输格式化转换接口 */ @FunctionalInterface public interface IMessageFormat { /** * 消息转换方法 * @param message 要转换的消息 * @param format 转换的格式[xml/json..] * @return 返回转换后的数据 */ String format(String message, String format); // boolean test(); String toString(); /** * 消息合法性验证方法 * @param msg 要验证的消息 * @return 返回验证结果 */ static boolean verifyMessage(String msg) { if (msg != null) { return true; } return false; } }
package com.imooc.impl; import com.imooc.IUserCredential; /** * Copyright (C), 2018-2019, copyright info. DAMU., Ltd. * FileName: com.imooc.impl UserCredentialImpl * <p>TODO</p> * * @author <a href="http://blog.csdn.net/muwenbin_flex">大牧莫邪</a> * @version 1.00 */ public class UserCredentialImpl implements IUserCredential { @Override public String verifyUser(String username) { if ("admin".equals(username)) { return "系统管理员"; } else if("manager".equals(username)) { return "用户管理员"; } return "普通会员"; } }
package com.imooc.impl.impl; import com.imooc.IMessageFormat; /** * Copyright (C), 2018-2019, copyright info. DAMU., Ltd. * FileName: com.imooc.impl.impl MessageFormatImpl * <p>TODO</p> * * @author <a href="http://blog.csdn.net/muwenbin_flex">大牧莫邪</a> * @version 1.00 */ public class MessageFormatImpl implements IMessageFormat { @Override public String format(String message, String format) { System.out.println("消息转换..."); return message; } }
package imooclambda; import com.imooc.IMessageFormat; import com.imooc.IUserCredential; import com.imooc.impl.UserCredentialImpl; import com.imooc.impl.impl.MessageFormatImpl; import java.util.Random; import java.util.UUID; import java.util.function.*; /** * Hello world! * * 需求改动: * 所有的用户验证,可以同时获取用户的验证信息[是否认证成功|成功~返回用户|null] * * Lambda表达式 基本语法 */ public class App { String welcome = "慕课网欢迎您."; public static void main( String[] args ) { // 一、接口的静态方法和默认方法 // 1. 默认方法 IUserCredential ic = new UserCredentialImpl(); System.out.println(ic.verifyUser("admin")); System.out.println(ic.getCredential("admin")); // 2. 静态方法 String msg = "hello world"; if (IMessageFormat.verifyMessage(msg)) { IMessageFormat format = new MessageFormatImpl(); format.format(msg, "json"); } // 匿名内部类,实现接口的抽象方法 IUserCredential ic2 = new IUserCredential() { @Override public String verifyUser(String username) { return "admin".equals(username)?"管理员":"会员"; } }; // 二、lambda表达式 是 函数式接口的一种实现 System.out.println(ic2.verifyUser("manager")); System.out.println(ic2.verifyUser("admin")); // lambda表达式,针对函数式接口的简单实现 IUserCredential ic3 = (String username) -> { return "admin".equals(username)?"lbd管理员": "lbd会员"; }; System.out.println(ic3.verifyUser("manager")); System.out.println(ic3.verifyUser("admin")); } }