public class Connection { private String host; private String user; private String password; public String getHost() { return host; } public Connection setHost(String host) { this.host = host; return this; } public String getUser() { return user; } public Connection setUser(String user) { this.user = user; return this; } public String getPassword() { return password; } public Connection setPassword(String password) { this.password = password; return this; } // 返回实体 public static Connection build(){ return new Connection(); } @Override public String toString() { return "Connection [host=" + host + ", user=" + user + ", password=" + password + "]"; } } public class FluentTest { public static void main(String[] args) { Connection conn = Connection.build().setHost("192.168.100.1:3306").setUser("root").setPassword("123456"); System.out.println(conn); } }
时间: 2024-11-05 14:50:22