成员变量与属性的关系
为Person类的中变量和方法
public class Person {private String name;private int age;private String gender;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public float getHeight() {return 1.75f;}public void setHeight(float height) {}}
- 3个字段name,age,gender就是成员变量
set/get方法名,去掉set/get后,将剩余部分首字母小写得到的字符串就是这个类的属性,即name,age,height就是属性
监听机制(观察者模式)
事件源:触发事件的对象
- 事件:要触发的行为,其中封装了事件源
- 监听器:事件的行为触发了,将要执行的操作
理解案例:
如果我方[水晶]被[摧毁]了,我就[骂队友]
如果敌方[水晶]被[摧毁]了,我就[夸自己]
[水晶]是事件源,[摧毁]是事件,[骂队友][夸自己]是监听器
加密工具
import org.apache.commons.codec.digest.DigestUtils;String s = DigestUtils.md5Hex("加密内容");
枚举
为什么要引入枚举?
自定义季节变量
public class Season {public static final int SEASON_SPRING = 1;public static final int SEASON_SUMMER = 2;public static final int SEASON_AUTUMN = 3;public static final int SEASON_WINTER = 4;}
问题点
代码不够简洁
不同类型的数据是通过名字区分的
不安全,由于是数字类型,就有可能使用这几个值做一些运算操作;比比如:SEASON_SPRING + SEASON_SUMMER
概述
为了简洁的表示一些固定的值,Java就给我们提供了枚举
枚举是指将变量的值一一列出来,变量的值只限于列举出来的值的范围内
枚举类定义格式
public enum s {枚举项1,枚举项2,枚举项3;}
注意
定义枚举类要用关键字enum
示例代码
// 定义一个枚举类,用来表示春,夏,秋,冬这四个固定值public enum Season {SPRING,SUMMER,AUTUMN,WINTER;}
特点
- 所有枚举类都是Enum的子类
- 我们可以通过”枚举类名.枚举项名称”去访问指定的枚举项
- 每一个枚举项其实就是该枚举的一个对象
- 枚举也是一个类,也可以去定义成员变量
- 枚举类的第一行上必须是枚举项,最后一个枚举项后的分号是可以省略的,但是如果枚举类有其他的东西,这个分号就不能省略。建议不要省略
- 枚举类可以有构造器,但必须是private的,它默认的也是private的,枚举项的用法比较特殊:枚举(“”);
- 枚举类也可以有抽象方法,但是枚举项必须重写该方法
方法介绍
| 方法名 | 说明 |
|---|---|
| String name() | 获取枚举项的名称 |
| int ordinal() | 返回枚举项在枚举类中的索引值 |
| int compareTo(E o) | 比较两个枚举项,返回的是索引值的差值 |
| String toString() | 返回枚举常量的名称 |
| static |
获取指定枚举类中的指定名称的枚举值 |
| values() | 获得所有的枚举项 |
序列化
集合转数组
集合转String数组
List<String> medias = record.getMedias();String[] strings = medias.toArray(new String[]{});
单个对象转换程集合
new的对象如何在栈不在堆
开启逃逸分析,如果new的对象,没有在方法外被使用,未发生逃逸,则会在栈上创建,不会触发垃圾回收机制
静态嵌套类与非静态嵌套类的区别
OuterClass.java
public class OuterClass {String outerField = "Outer field";static String staticOuterField = "Static outer field";class InnerClass {void accessMembers() {System.out.println(outerField);System.out.println(staticOuterField);}}static class StaticNestedClass {void accessMembers(OuterClass outer) {// Compiler error: Cannot make a static reference to the non-static// field outerField// System.out.println(outerField);System.out.println(outer.outerField);System.out.println(staticOuterField);}}public static void main(String[] args) {System.out.println("Inner class:");System.out.println("------------");OuterClass outerObject = new OuterClass();OuterClass.InnerClass innerObject = outerObject.new InnerClass();innerObject.accessMembers();System.out.println("\nStatic nested class:");System.out.println("--------------------");StaticNestedClass staticNestedObject = new StaticNestedClass();staticNestedObject.accessMembers(outerObject);System.out.println("\nTop-level class:");System.out.println("--------------------");TopLevelClass topLevelObject = new TopLevelClass();topLevelObject.accessMembers(outerObject);}}
TopLevelClass.java
public class TopLevelClass {void accessMembers(OuterClass outer) {// Compiler error: Cannot make a static reference to the non-static// field OuterClass.outerField// System.out.println(OuterClass.outerField);System.out.println(outer.outerField);System.out.println(OuterClass.staticOuterField);}}
输出结果
Inner class:------------Outer fieldStatic outer fieldStatic nested class:--------------------Outer fieldStatic outer fieldTop-level class:--------------------Outer fieldStatic outer field
时间转换
@Testpublic void testJava8DateFormat() {DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");LocalDateTime localDateTime = LocalDateTime.parse("2019-07-31 00:00:00", dateTimeFormatter1);LocalDate localDate = LocalDate.parse("2019-07-31", dateTimeFormatter2);Date date = Date.from(LocalDateTime.parse("2019-07-31 00:00:00", dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant());String strDateTime = "2019-07-31 00:00:00";String strDate = "2019-07-31";Long timestamp = 1564502400000L;/* LocalDateTime 转 LocalDate */System.out.println("LocalDateTime 转 LocalDate: " + localDateTime.toLocalDate());/* LocalDateTime 转 Long */System.out.println("LocalDateTime 转 Long: " + localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());/* LocalDateTime 转 Date */System.out.println("LocalDateTime 转 Date: " + Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));/* LocalDateTime 转 String */System.out.println("LocalDateTime 转 String: " + localDateTime.format(dateTimeFormatter1));System.out.println("-------------------------------");/* LocalDate 转 LocalDateTime */System.out.println("LocalDate 转 LocalDateTime: " + LocalDateTime.of(localDate, LocalTime.parse("00:00:00")));/* LocalDate 转 Long */System.out.println("LocalDate 转 Long: " + localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());/* LocalDate 转 Date */System.out.println("LocalDate 转 Date: " + Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));/* LocalDate 转 String */System.out.println("LocalDateTime 转 String: " + localDateTime.format(dateTimeFormatter2));System.out.println("-------------------------------");/* Date 转 LocalDateTime */System.out.println("Date 转 LocalDateTime: " + LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));/* Date 转 Long */System.out.println("Date 转 Long: " + date.getTime());/* Date 转 LocalDate */System.out.println("Date 转 LocalDateTime: " + LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate());/* Date 转 String */SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");System.out.println("Date 转 String: " + sdf.format(date));System.out.println("-------------------------------");/* String 转 LocalDateTime */System.out.println("String 转 LocalDateTime: " + LocalDateTime.parse(strDateTime, dateTimeFormatter1));/* String 转 LocalDate */System.out.println("String 转 LocalDate: " + LocalDateTime.parse(strDateTime, dateTimeFormatter1).toLocalDate());System.out.println("String 转 LocalDate: " + LocalDate.parse(strDate, dateTimeFormatter2));/* String 转 Date */System.out.println("String 转 Date: " + Date.from(LocalDateTime.parse(strDateTime, dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant()));System.out.println("-------------------------------");/* Long 转 LocalDateTime */System.out.println("Long 转 LocalDateTime:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));/* Long 转 LocalDate */System.out.println("Long 转 LocalDate:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).toLocalDate());}
当日最早和最晚时间
// 最早LocalDateTime.now().with(LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()// 最晚LocalDateTime.now().with(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
大写字符串转驼峰
private String getCamelCaseName(String name) {String lowerCaseName = name.toLowerCase();char[] chars = lowerCaseName.toCharArray();StringBuilder builder = new StringBuilder();int temp = 0;for (int i = 0; i < chars.length; i++) {if (temp != 0 && temp == i) continue; // 跳过'_'字符的添加if ('_' == (chars[i]) && i < chars.length - 1) {// 将下一个字符转换成大写char aChar = chars[i + 1];aChar -= 32;builder.append(aChar);temp = i + 1;} else {builder.append(chars[i]);}}return builder.toString();}
