[Java] 类库例题
例1 字符串操作
定义一个StringBuffer类对象,然后通过append()方法向对象中添加26个小写字母,每次只添加一次,共添加26次,然后按逆序方式输出,并且可以删除前5个字符
- 面向过程实现
1 public class TestDemo { 2 public static void main(String[] args) throws Exception{ 3 StringBuffer buffer = new StringBuffer(); 4 for(int x = 'a' ; x <= 'z' ; x++) { 5 buffer.append((char)x); 6 } 7 System.out.println("[初始化数据]" + buffer); 8 System.out.println("[逆序输出]" + buffer.reverse()); 9 System.out.println("[删除前五个字符]" + buffer.delete(0, 5)); 10 } 11 }
- 面向对象实现(接口先行--子类实现--工厂解耦)
1 interface IContent{ 2 public String content(); 3 public String reverse(); 4 public String delete(int index); 5 } 6 class StringContent implements IContent{ 7 private StringBuffer buffer = new StringBuffer(); 8 public StringContent() { 9 for(int x = 'a' ; x <= 'z' ; x++) { 10 buffer.append((char)x); 11 } 12 } 13 @Override 14 public String content() { 15 return this.buffer.toString(); 16 } 17 @Override 18 public String reverse() { 19 return this.buffer.reverse().toString(); 20 } 21 @Override 22 public String delete(int index) { 23 return this.buffer.delete(0, index).toString(); 24 } 25 } 26 class Factory{ 27 private Factory() {} 28 public static IContent getInstance() { 29 return new StringContent(); 30 } 31 } 32 public class TestDemo { 33 public static void main(String[] args) throws Exception{ 34 IContent content = Factory.getInstance(); 35 System.out.println("[初始化数据]" + content.content()); 36 System.out.println("[逆序输出]" + content.reverse()); 37 System.out.println("[删除前五个字符]" + content.delete(5)); 38 } 39 }
- 先定义出公共处理标准,随后依据此标准进行项目的具体实现
- 对于接口子类的获取采用工厂模式
例2 生成随机数
利用Random类产生5个1~30之间(包括1和30)的随机整数
- 接口:描述最终目的
- 异常类:继承RuntimeException,业务异常,不希望此异常直接出现在方法的定义声明处(如果继承Exception会强制处理)
1 package javaSE; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 6 interface INumber{ 7 public int[] getData(); 8 } 9 class InvalidArrayLengthException extends RuntimeException{ 10 public InvalidArrayLengthException() {} 11 public InvalidArrayLengthException(String str) { 12 super(str); 13 } 14 } 15 class RandomNumber implements INumber{ 16 private int data[]; 17 public RandomNumber() { 18 this(3); 19 } 20 public RandomNumber(int len) { 21 if(len <= 0) { 22 throw new InvalidArrayLengthException("数组长度不正确!"); 23 } 24 this.data = new int[len]; 25 this.createRandom(); 26 } 27 public void createRandom() { 28 Random random = new Random(); 29 for(int x = 0 ; x < this.data.length; x++) { 30 this.data[x] = random.nextInt(30) + 1; 31 } 32 } 33 @Override 34 public int[] getData() { 35 return this.data; 36 } 37 } 38 class Factory{ 39 private Factory() {} 40 public static INumber getInstance(int ...args) { 41 if(args.length == 0) { 42 return new RandomNumber(); 43 }else { 44 return new RandomNumber(args[0]); 45 } 46 } 47 } 48 public class TestDemo { 49 public static void main(String[] args) throws Exception{ 50 INumber content = Factory.getInstance(5); 51 System.out.println(Arrays.toString(content.getData())); 52 } 53 }
例3 Email验证
输入一个Email地址,用正则表达式验证该Email是否正确
1 import java.util.Arrays; 2 import java.util.Random; 3 4 interface IValidator{ 5 public boolean test() ; 6 } 7 class EmailValidator implements IValidator{ 8 public static final String DEFAULT_EMAIL_REGEX = "\\w+@\\w+\\.\\w+"; 9 private String regex; 10 private String content; 11 public EmailValidator(String content) { 12 this(content,DEFAULT_EMAIL_REGEX); 13 this.content = content; 14 this.regex = DEFAULT_EMAIL_REGEX; 15 } 16 public EmailValidator(String content,String regex) { 17 this.content = content; 18 this.regex = regex; 19 } 20 @Override 21 public boolean test() { 22 if(this.content == null || "".equals(this.content)) { 23 return false; 24 } 25 return this.content.matches(this.regex); 26 } 27 } 28 class Factory{ 29 private Factory() {} 30 public static IValidator getInstance(String content,String ...regex) { 31 if(regex.length == 0) { 32 return new EmailValidator(content); 33 }else { 34 return new EmailValidator(content,regex[0]); 35 } 36 } 37 } 38 public class TestDemo { 39 public static void main(String[] args) throws Exception{ 40 {IValidator validator = Factory.getInstance("muyan@yootk.com"); 41 System.out.println(validator.test() ? "验证通过":"Email数据输入错误");} 42 {IValidator validator = Factory.getInstance("muyan@yootk.org", 43 "[a-zA-Z][a-zA-Z0-9_\\-\\.]+@\\w+\\.(com|com\\.cn|net\\.cn|gov|edu|org)"); 44 System.out.println(validator.test() ? "验证通过":"Email数据输入错误");} 45 } 46 }
- 即可使用默认的正则表达式,也可根据需要扩充
例4 IP地址验证
编写正则表达式,判断给定的IP地址是否合法
1 interface IValidator{ 2 public boolean test() ; 3 } 4 class IPHostValidator implements IValidator{ 5 public static final String DEFAULT_EMAIL_REGEX = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; 6 private String regex; 7 private String content; 8 public IPHostValidator(String content) { 9 this(content, DEFAULT_EMAIL_REGEX); 10 } 11 public IPHostValidator(String content,String regex) { 12 this.content = content; 13 this.regex = regex; 14 } 15 @Override 16 public boolean test() { 17 if(this.content == null || "".equals(this.content)) { 18 return false; 19 } 20 if("0.0.0.0".equals(this.content)||"255.255.255.255".equals(this.content)) { 21 return false; 22 } 23 return this.content.matches(this.regex); 24 } 25 } 26 class Factory{ 27 private Factory() {} 28 public static IValidator getInstance(String content,String ...regex) { 29 if(regex.length == 0) { 30 return new IPHostValidator(content); 31 }else { 32 return new IPHostValidator(content,regex[0]); 33 } 34 } 35 } 36 public class TestDemo { 37 public static void main(String[] args) throws Exception{ 38 IValidator validator = Factory.getInstance("192.168.1.2"); 39 System.out.println(validator.test() ? "验证通过":"IP数据输入错误"); 40 } 41 }
- 实际开发中除了核心代码,最常见的就是数据验证功能
例5 HTML拆分
给定一段HTML代码:“<font face="Arial,Serif" size="+2" color="red">”,拆分为
face=Arial,Serif
size=+2
color=red
1 import java.util.regex.Matcher; 2 import java.util.regex.Pattern; 3 4 interface IStringHandle{ 5 public String[] split(); 6 } 7 class HTMLStringHandle implements IStringHandle{ 8 private String content; 9 private String regex; 10 public HTMLStringHandle(String content,String regex) { 11 this.content = content; 12 this.regex = regex; 13 } 14 @Override 15 public String[] split() { 16 Matcher matcher = Pattern.compile(this.regex).matcher(this.content); 17 StringBuffer buffer = new StringBuffer(this.content.length()); 18 while(matcher.find()) { 19 String value = matcher.group(0); 20 String temp[] = value.split("="); 21 buffer.append(temp[0]).append(":").append(temp[1].replaceAll("\"","")).append("|"); 22 } 23 return buffer.toString().split("\\|"); 24 } 25 } 26 class Factory{ 27 private Factory() {} 28 public static IStringHandle getInstance(String content,String regex) { 29 return new HTMLStringHandle(content,regex); 30 } 31 } 32 public class TestDemo { 33 public static void main(String[] args) throws Exception{ 34 String html = "<font face=\"Arial,Serif\" size=\"+2\" color=\"red\">"; 35 String regex = "\\w+=\"[a-zA-Z0-9,\\+]+\""; 36 IStringHandle handle = Factory.getInstance(html, regex); 37 String result[] = handle.split(); 38 for(String temp:result) { 39 System.out.println(temp); 40 } 41 } 42 }
- 可使用HTMLParser+XPath
例6 国际化应用
从命令行输入国家代号,1代表中国,2代表美国,然后根据不同代号调用不同的资源文件显示信息
- 编写资源文件
- 加载资源文件
- 通过IMessage接口,根据key获取数据
1 import java.util.Locale; 2 import java.util.ResourceBundle; 3 4 interface IMessage{ 5 public String get(String key); 6 } 7 8 class ResourceMessage implements IMessage{ 9 private static final String BASE_NAME = "Message"; 10 private ResourceBundle resourceBundle; 11 public ResourceMessage(Locale locale){ 12 this.resourceBundle = ResourceBundle.getBundle(BASE_NAME,locale); 13 } 14 15 @Override 16 public String get(String key){ 17 try{ 18 return this.resourceBundle.getString(key); 19 }catch (Exception e){ 20 return null; 21 } 22 } 23 } 24 25 class ParameterException extends RuntimeException{ 26 public ParameterException(){} 27 public ParameterException(String msg){ 28 super(msg); 29 } 30 } 31 32 class Factory{ 33 private Factory(){} 34 public static IMessage getInstance(String args[]){ 35 if(args.length != 1 || !args[0].matches("\\d")){ 36 throw new ParameterException("初始参数配置错误,无法执行"); 37 } 38 int select = Integer.parseInt(args[0]); 39 switch (select){ 40 case 1: 41 return new ResourceMessage(Locale.CHINESE); 42 case 2: 43 return new ResourceMessage(Locale.US); 44 default: 45 return null; 46 } 47 } 48 } 49 50 public class YootkDemo { 51 public static void main(String[] args) throws Exception{ 52 IMessage message = Factory.getInstance(args); 53 System.out.println(message.get("welcome.info")); 54 } 55 }
>> Hello
参考
idea:Maven项目,pom.xml中的resources标签
https://blog.csdn.net/qq_36134376/article/details/90762263