一.枚舉
枚舉:
1. 概述: 引用數據類型
類 數組 接口 註解 [ 枚舉]
2. 關鍵字: enum - > java. lang. Enum類,是所有枚舉的父類。
3. 枚舉:裏面一般都定義常量( 默認的修飾符 public static final 但是定義的時候寫上報錯)
4. 特點: 定義的常量都是這個枚舉類的對象
一個沒枚舉類中有多個對象( 多例)
5. 問題: 我們定義出來的常量, 數據類型應該是什麼: 本類類型
6. 使用場景: 一般用在表示狀態上( 如果用1234 表示狀態不太好, 用枚舉是最好的)
提交訂單: 未付款
提交訂單: 已付款
提交訂單: 未發貨
提交訂單: 已發貨( 發貨中)
提交訂單: 派件中
提交訂單: 已簽收
7. 枚舉中定義構造: ( 瞭解)
a. 無參構造: 默認權限修飾符是private
b. 有參構造: private , 要是賦值的話, 我們可以利用常量直接賦值 : RED ( "紅色" )
public enum Color{
RED, GREEN, YELLOW;
}
public enum Color {
RED ( "紅色" ) , GREEN ( "綠色" ) , YELLOW;
private Color ( ) {
}
private String color;
private Color ( String color) {
this . color = color;
}
public String getColor ( ) {
return color;
}
}
public class Test {
public static void main ( String[ ] args) {
System. out. println ( "-----------------------" ) ;
Color red = Color. RED;
System. out. println ( red. getColor ( ) ) ;
}
}
二.正則表達式
1.正則表達式的概念及演示
package com. itheima. demo04Regex;
import java. util. Scanner;
public class Demo01Regex {
public static void main ( String[ ] args) {
System. out. println ( "請輸入一個QQ號:" ) ;
String qq = new Scanner ( System. in) . nextLine ( ) ;
boolean b1 = checkQQ ( qq) ;
System. out. println ( "b1:" + b1) ;
boolean b2 = checkQQRegex ( qq) ;
System. out. println ( "b2:" + b2) ;
}
public static boolean checkQQ ( String qq) {
if ( qq. length ( ) < 5 || qq. length ( ) > 15 ) {
return false ;
}
char [ ] chars = qq. toCharArray ( ) ;
for ( char c : chars) {
if ( c< '0' || c> '9' ) {
return false ;
}
}
if ( "0" . equals ( qq. charAt ( 0 ) + "" ) ) {
return false ;
}
return true ;
}
public static boolean checkQQRegex ( String qq) {
return qq. matches ( "[1-9][0-9]{4,14}" ) ;
}
}
2.正則表達式-字符類
package com. itheima. demo04Regex;
public class Demo02Regex {
public static void main ( String[ ] args) {
String str = "ead" ;
String regex = "h[aeiou]d" ;
str = "hid" ;
str = "Hud" ;
boolean b1 = str. matches ( regex) ;
System. out. println ( "b1:" + b1) ;
regex = "h[^aeiou]d" ;
str = "h1d" ;
str = "had" ;
boolean b2 = str. matches ( regex) ;
System. out. println ( "b2:" + b2) ;
regex = "[a-z]ad" ;
str = "wad" ;
str = "1ad" ;
boolean b3 = str. matches ( regex) ;
System. out. println ( "b3:" + b3) ;
regex = "[a-dm-p]ad" ;
str = "cad" ;
str = "ead" ;
boolean b4 = str. matches ( regex) ;
System. out. println ( "b4:" + b4) ;
}
}
3.正則表達式-邏輯運算符
package com. itheima. demo04Regex;
public class Demo03Regex {
public static void main ( String[ ] args) {
String str = "had" ;
str = "ead" ;
str = "Had" ;
String regex = "[[a-z]&&[^aeiou]]ad" ;
boolean b1 = str. matches ( regex) ;
System. out. println ( "b1:" + b1) ;
regex = "[a||e||i||o||u]ad" ;
str = "aad" ;
str = "1ad" ;
boolean b2 = str. matches ( regex) ;
System. out. println ( "b2:" + b2) ;
}
}
4.正則表達式-預定義字符
package com. itheima. demo04Regex;
public class Demo04Regex {
public static void main ( String[ ] args) {
String str = "250" ;
str = "25a" ;
String regex = "[0-9][0-9][0-9]" ;
regex = "\\d\\d\\d" ;
boolean b1 = str. matches ( regex) ;
System. out. println ( "b1:" + b1) ;
regex = "1[358]\\d\\d\\d\\d\\d\\d\\d\\d\\d" ;
str = "13800138000" ;
str = "23800138000" ;
str = "138001380001" ;
str = "13800138a00" ;
boolean b2 = str. matches ( regex) ;
System. out. println ( "b2:" + b2) ;
regex = "h.d" ;
str = "h&d" ;
str = "h中d" ;
str = "1Ad" ;
boolean b3 = str. matches ( regex) ;
System. out. println ( "b3:" + b3) ;
regex = "had\\." ;
str = "hadA" ;
str = "had." ;
boolean b4 = str. matches ( regex) ;
System. out. println ( "b4:" + b4) ;
}
}
5. 正則表達式-數量詞
package com. itheima. demo04Regex;
public class Demo05Regex {
public static void main ( String[ ] args) {
String str = "" ;
str = "123" ;
str = "1234" ;
String regex = "\\d{3}" ;
boolean b1 = str. matches ( regex) ;
System. out. println ( "b1:" + b1) ;
regex = "\\d+" ;
str = "12312413221312" ;
str = "1" ;
str = "" ;
str = "111a" ;
boolean b2 = str. matches ( regex) ;
System. out. println ( "b2:" + b2) ;
regex = "1[358]\\d{9}" ;
str = "13800138000" ;
str = "1380013800a0" ;
boolean b3 = str. matches ( regex) ;
System. out. println ( "b3:" + b3) ;
System. out. println ( d) ;
regex = "\\d*\\.\\d+" ;
str = "1.1" ;
str = ".1" ;
str = "1." ;
str = "aa.1" ;
str = "1.1.1" ;
boolean b4 = str. matches ( regex) ;
System. out. println ( "b4:" + b4) ;
regex = "\\d*\\.?\\d*" ;
str = "10000" ;
str = "10000.13213123" ;
str = ".1212312" ;
str = "" ;
str = "a" ;
str = "a" ;
str = "11..11" ;
boolean b5 = str. matches ( regex) ;
System. out. println ( "b5:" + b5) ;
regex = "[1-9]\\d{4,14}" ;
str = "11111" ;
str = "111" ;
str = "111111a" ;
boolean b7 = str. matches ( regex) ;
System. out. println ( "b7:" + b7) ;
}
}
6.正則表達式-分組括號( )
package com. itheima. demo04Regex;
public class Demo06Regex {
public static void main ( String[ ] args) {
String regex = "(abc)*" ;
boolean b1 = str. matches ( regex) ;
String str = "abc" ;
str = "" ;
str = "abcabc" ;
str = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc" ;
str = "abcaaabbbccc" ;
System. out. println ( "b1:" + b1) ;
str = "DG8FV-B9TKY-FRT9J-99899-XPQ4G" ;
str = "DG8FV-B9TKY-FRT9J-99899-XPQ4G-" ;
regex = "([A-Z0-9]{5}-){4}[A-Z0-9]{5}" ;
boolean b2 = str. matches ( regex) ;
System. out. println ( "b2:" + b2) ;
}
}
7.String類中和正則表達式相關的方法
package com. itheima. demo04Regex;
public class Demo07Regex {
public static void main ( String[ ] args) {
show04 ( ) ;
}
private static void show01 ( ) {
String s = "11-22-33-44-55" ;
String[ ] arr = s. split ( "-" ) ;
for ( String str : arr) {
System. out. println ( str) ;
}
}
private static void show02 ( ) {
String s = "192.168.1.100" ;
String[ ] arr = s. split ( "\\." ) ;
System. out. println ( arr. length) ;
for ( String str : arr) {
System. out. println ( str) ;
}
}
private static void show03 ( ) {
String s = "192 168 1 100" ;
s = "192 168 1 100" ;
String[ ] arr = s. split ( " +" ) ;
System. out. println ( arr. length) ;
for ( String str : arr) {
System. out. println ( str) ;
}
}
public static void show04 ( ) {
String s = "12312asfdsa12123asa11c1fzvs1v*&&3242fsfa11" ;
System. out. println ( "原字符串:" + s) ;
String reg = "\\d" ;
String s1 = s. replaceAll ( reg, "@_@" ) ;
System. out. println ( "替換後的字符串:" + s1) ;
reg = "\\d+" ;
String s2 = s. replaceAll ( reg, "@_@" ) ;
System. out. println ( "替換後的字符串:" + s2) ;
}
}
8.正則表達式生成網址:
https://www.sojson.com/regex/generate
三.Base64加密和解密(JDK8的新特性)
java. util. Base64
該類僅由用於獲得Base64編碼方案的編碼器和解碼器的靜態方法組成。
作用:
使用Base64裏邊的編碼器對數據進行編碼( 加密)
使用Base64裏邊的解碼器對數據進行解碼( 解密)
靜態成員方法:
static Base64. Encoder getEncoder( ) 獲取加密器
static Base64. Decoder getDecoder( ) 獲取解密器
java. util. Base64. Encoder: 是Base64的內部類, 用於對數據進行加密
成員方法: ( 編碼)
String encodeToString( byte [ ] src) 使用Base64編碼方案將指定的字節數組編碼爲字符串。
java. util. Base64. Decoder: 是Base64的內部類, 用於對數據進行解密
成員方法: ( 解碼)
byte [ ] decode( String src) 使用Base64編碼方案將Base64編碼的字符串解碼爲新分配的字節數組。
public class Demo01Base64 {
public static void main ( String[ ] args) {
String s1 = "國家的絕密文件!" ;
System. out. println ( "原字符串:" + s1) ;
String s2 = Base64. getEncoder ( ) . encodeToString ( s1. getBytes ( ) ) ;
System. out. println ( "加密後的字符串:" + s2) ;
byte [ ] bytes = Base64. getDecoder ( ) . decode ( s2) ;
String s3 = new String ( bytes) ;
System. out. println ( "解密後的字符串:" + s3) ;
}
}