一.枚举
枚举:
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) ;
}
}