本文概览:把java常用的工具和方法记录下来,下次就不需要再进行从网络查询了,提高效率。
1 Object之toString()
介绍了通过org.apache.commons.lang3包的ToStringBuilder来实现toString方法。介绍了ToStringStyle中提供的几种格式对应的输出样式。
1. 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; public class Model { private String name; private String status; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String toString_MULI_LINE_STYLE() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } public String toString_SIMPLE_STYLE() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE); } public String toString_DEFAULT_STYLE() { return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE); } public String toString_SHORT_PREFIX_STYLE() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } public String toString_NO_FIELD_NAMES_STYLE() { return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_FIELD_NAMES_STYLE); } public static void main(String[] args){ Model model = new Model(); model.setName("lili"); model.setStatus("s"); System.out.println("ToStringStyle.MULTI_LINE_STYLE: "+model.toString_DEFAULT_STYLE()); System.out.printf("ToStringStyle.SIMPLE_STYLE: "+model.toString_SIMPLE_STYLE()); System.out.println("ToStringStyle.DEFAULT_STYLE: "+model.toString_DEFAULT_STYLE()); System.out.println("ToStringStyle.SHORT_PREFIX_STYLE: "+model.toString_SHORT_PREFIX_STYLE()); System.out.printf("ToStringStyle.NO_FIELD_NAMES_STYLE: "+model.toString_NO_FIELD_NAMES_STYLE()); } } |
2. 打印结果为
ToStringStyle.MULTI_LINE_STYLE: tostring.Model@51d7aa64[name=lili,status=s]
ToStringStyle.SIMPLE_STYLE: lili,sToStringStyle.DEFAULT_STYLE: tostring.Model@51d7aa64[name=lili,status=s]
ToStringStyle.SHORT_PREFIX_STYLE: Model[name=lili,status=s]
ToStringStyle.NO_FIELD_NAMES_STYLE: tostring.Model@51d7aa64[lili,s]
2 UrlEncode编码
1、编码
1 2 3 |
import java.net.URLEncoder; String ecode = URLEncoder.encode(json, "UTF-8"); |
(全文完)