##Exceptions 处理异常及错误。 ###举例,电话类 ```Java public class Phone { private String phoneType; private String phoneNumber; public Phone(String phoneType, String phoneNumber) { if (phoneType == null || phoneNumber == null) { throw new IllegalArgumentException("The phone number or type can't be null"); } this.phoneType = phoneType; this.phoneNumber = phoneNumber; } public String getPhoneType(){ return phoneType; } public void setPhoneType(String phoneType){ this.phoneType=phoneType; } public String getPhoneNumber(){ return phoneNumber; } public void setPhoneNumber(String phoneNumber){ this.phoneNumber=phoneNumber; } @Override public String toString(){ return "Phone number: "+phoneNumber+" Phone type: "+phoneType; } } ``` ###异常实例举例 ```Java public class PhoneExceptionTester { public static void main(String[] args) { String[] types = new String[]{"Blackberry", "iPhone", "HuaWei", null}; String[] numbers = new String[]{"123-4567-78", "456-7899-98", null, "852-456-966"}; for (String i : types ) { for (String k : numbers ) { try { System.out.println(new Phone(i, k)); } catch (IllegalArgumentException e) { System.out.println(e.getLocalizedMessage()); } } } } } ``` ##Enums 枚举固定变量。 ###举例,红绿灯类 ```Java public class Light { public Light() { } public void changeColor(LightColor currentLight) { if (currentLight==LightColor.RED) { System.out.println("Hey! Stop! Put Your hands on the air!"); } else if (currentLight==LightColor.GREEN) { System.out.println("Go away!"); } else if (currentLight==LightColor.YELLOW) { System.out.println("Hey! Slow down! Take it easy."); } } } ``` ###实现一个灯光颜色的具体对象类 ```Java public class LightChanger { public static void main(String[] args) { Light hld=new Light(); hld.changeColor(LightColor.YELLOW); hld.changeColor(LightColor.RED); hld.changeColor(LightColor.GREEN); } } ``` ###枚举 ```Java public enum LightColor { RED,YELLOW,GREEN; } ``` ####还可以有另一种写法 以房间类型选择为例,返回枚举的类型列表 ```Java public enum RoomType { SINGLE("1"), TWIN("2"), DOUBLE("3"), TRIPLE("4"), SUITE("5"), VILLA("6"); public final String typeLabel; private RoomType(String typeLabel) { this.typeLabel = typeLabel; } public static RoomType valueOfType(String typeLabel) { for (RoomType roomType : RoomType.values()) { if (roomType.typeLabel.equals(typeLabel)) { return roomType; } } throw new IllegalArgumentException(); } } ``` ##Dates 和 Calendar 存储和检索日期。 ```Java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DatesAndCalendar { public static void main(String[] args) { DatesAndCalendar.displayCurrentDate(); DatesAndCalendar.displaySetDate(); } private static void displayCurrentDate() { Calendar calendar = Calendar.getInstance(); System.out.println(calendar); Date date = new Date(); calendar.setTime(date); System.out.println(calendar.getTime()); } private static void displaySetDate() { Calendar calendar = Calendar.getInstance(); calendar.set(2189, 2, 27); Date date = calendar.getTime(); // System.out.println(date); SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd Sat Jan"); System.out.println(timeFormat.format(date)); } } ``` ##Regular Expressions (RegEx) 正则表达式(RegEx),查找字符串模式。 ###举例,person类 ```Java import java.util.regex.Pattern; public class Person { private final String name; private final String email; public Person(String name, String email) { this.name = name; this.email = email; String emailRegex = "^(.+)@(.+).[a-z]"; Pattern pattern = Pattern.compile(emailRegex); if (!pattern.matcher(email).matches()) { throw new IllegalArgumentException("Error, Invalid email"); } } @Override public String toString() { return "Person{" + "Name='" + name + '\'' + ", Email='" + email + '\'' + "}"; } } ``` ###测试实例 ```Java public class RegExTest { public static void main(String[] args) { String[] email = new String[]{"ciel@gmail.com", "@gmail.com", "jeff@example.com","asdguh@asdf.com"}; Person anyone = new Person("Ciel", email[3]); System.out.println(anyone.toString()); } } ``` ##高级字符串特性 更有效地操作和处理字符串。 ###Scanner类 ```Java import java.util.Scanner; public class UserInputTester { public static void main(String[] args) { Scanner sc =new Scanner(System.in); try { System.out.println("Enter a String:"); String str=sc.nextLine(); System.out.println("Your input string is: "+str); }catch (Exception ex){ ex.getLocalizedMessage(); }finally { sc.close(); } } } ``` ###String类基础 ```Java public class StringFunction { public static void main(String[] args) { String str = "Keep going,Do not give up!"; System.out.println(str); String anotherStr = "Keep going,Do not give up!"; System.out.println(anotherStr); str = "absolutely"; System.out.println(str); System.out.println("charAt: " + anotherStr.charAt(5)); System.out.println("equalsIgnoreCase: " + anotherStr.equalsIgnoreCase("keep going,do not give up!")); System.out.println("replace: " + anotherStr.replace("going", "goaaaing")); String[] temp = anotherStr.split(" "); for (String tempStr : temp ) { System.out.println("anotherStr.split(\" \"): " + tempStr); } System.out.println("substring: " + anotherStr.substring(2, 5)); System.out.println("contains: " + anotherStr.contains("not")); } } ``` Last modification:January 19, 2022 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 If you think my article is useful to you, please feel free to appreciate