------------------------------------------------------------------------------20230703
mockito.when 不生效-掘金
Java 单元测试(3)mock进阶 - 静态、final、私有方法mock - 掘金
Mockito与PowerMock的使用基础教程 - 掘金
---------------------------------------------------------------------------------------
System.out.println(System.getProperty("user.name"));
System.out.println("dfdsf"+System.getProperty("line.separator")+"fdsfas");
System.getProperty("line. separator")获取运行时环境的换行符。
若程序中需要获取操作系统登录用户名,由 JVM 属性获取:System.getProperty("user.name")
硬编码的指令可以直接作为 system.exec(),system()和 popen()函数的入参。
序列化对象中的 HashMap、HashSet 或 HashTable 等集合不能包含对象自身的引用
Java程序支持跨平台运行,因此路径分隔符要慎用。
为了解决这个隐患,File类提供了一个常量:
public static final String separator
:根据操作系统,动态的提供分隔符。
File file2 = new File("d:" + File.separator + "atguigu" + File.separator + "info.txt");
验证文件路径的时候应该使用 File.getCanonicalPath() 来获取其路径
File file = new File("1.txt");
System.out.println(file.getCanonicalPath());
D:\workspace\test1\1.txt
JAAS全称为 Java Authentication Authorization Service,中文含义即Java认证和授权服务。使用可插入方式将认证和授权逻辑和应用程序分离开。
https://www.cnblogs.com/kukudi/p/17344898.html
这个链接可以真多东西。
Java 序列化详解 | JavaGuide(Java面试 + 学习指南)
Java基础常见面试题总结(中) | JavaGuide(Java面试 + 学习指南)
RandomAccessFile
Java IO 基础知识总结 | JavaGuide(Java面试 + 学习指南)
Java IO流详解(三)——RandomAccessFile - 简书
https://www.cnblogs.com/greyzeng/p/14878108.html
class Father {
protected String pStr;
public Father() {
invokePrint();
pStr = "parent";
}
void invokePrint() {
System.out.println("Parent print, pStr = " + pStr);
}
}
class Child extends Father {
private String cStr;
public Child() {
super();
cStr = "child";
}
public static void main(String[] args) {
Child c = new Child();
c.invokePrint();
Father p = new Father();
p.invokePrint();
Father father = new Child();
father.invokePrint();
}
void invokePrint() {
System.out.println("Child print, pStr = " + pStr + ", cStr = " + cStr);
}
}
Child print, pStr = null, cStr = null
Child print, pStr = parent, cStr = child
Parent print, pStr = null
Parent print, pStr = parent
Child print, pStr = null, cStr = null
Child print, pStr = parent, cStr = child
class StaticTest {
public static void main(String[] args) {
System.out.println(Const.NAME); // i am a const
}
}
class Const {
public static final String NAME = "i am a const";
static {
System.out.println("init Const class");
}
}
i am a const
class StaticTest {
public static void main(String[] args) {
System.out.println(Child.m);
//System.out.println(new Child().m); // 如果是这句,且删除上一行。则输出init father \n init child \n 35
}
}
class Father {
public static int m = 34;
static {
System.out.println("init father");
}
}
class Child extends Father {
static {
m = 35;
System.out.println("init child");
}
}
init father
34
Java 静态属性与实例属性的初始化 - 简书
关于java中静态属性、静态方法的继承问题_西瓜游侠的博客-CSDN博客
class StaticTest {
public static void main(String[] args) {
System.out.println(Child.m);
}
}
/**
* 子类的静态属性 m 覆盖了父类的静态属性。Child.m引用的是子类的静态属性。
* java中静态属性和静态方法可以被继承,但是没有被重写(overwrite)而是被隐藏。
* Father.m 被子类隐藏了。
*/
class Father {
public static int m = 34;
static {
System.out.println("init father");
}
}
class Child extends Father {
static {
m = 35;
System.out.println("init child");
}
public static int m = 36;
}
init father
init child
36
class Test {
static {
i = 1;
System.out.println(i); // 编译不通过
}
private static int i = 2;
}
// 静态代码中只能访问定义在静态代码块之前的变量
package AAA.BBB;
class StaticTest {
public static void main(String[] args) {
Father child = new Child(40);
}
}
class Father {
public static int fatherStatic = 34;
public int father = 35;
static {
System.out.println("father static block init"); // 1
}
public Father(int count) {
father = count; // 40
System.out.println("father construct : " + father + ", " + fatherStatic); // 40, 35
}
{
System.out.println("father block init"); // 3
fatherStatic++; // 35
father++; // 36
}
}
class Child extends Father {
public static int childStatic = 37;
public int child = 38;
static {
System.out.println("child static block init"); // 2
}
{
childStatic++; // 38
System.out.println("child block init");
child++;
}
public Child(int count) {
super(count);
father = count;
System.out.println("child construct : " + child + ", " + childStatic);
}
}
father static block init
child static block init
father block init
father construct : 40, 35
child block init
child construct : 39, 38
=======================
各个代码块调用顺序
- 父类静态代变量显式赋值、父类静态代码块(按定义顺序)
- 子类静态变量显式赋值、子类静态代码块(按定义顺序)
- 父类非静态变量显式赋值(父类实例成员变量)、父类非静态代码块(按定义顺序)
- 父类构造函数
- 子类非静态变量(子类实例成员变量)、子类非静态代码块(按定义顺序)
- 子类构造函数。
Java子类可以继承父类的静态变量和静态方法吗? - 知乎
=============================20230606
JVM类加载器
通俗易懂 启动类加载器、扩展类加载器、应用类加载器 - 知乎
启动类加载器属于虚拟机的一部分,它是用C++写的,看不到源码;其他类加载器是用Java写的,说白了就是一些Java类,一会儿就可以看到了,比如扩展类加载器、应用类加载器。
- 启动类加载器:BootstrapClassLoader
- 扩展类加载器:ExtentionClassLoader
- 应用类加载器:AppClassLoader (也叫做“系统类加载器”)
既然只是把class文件装进虚拟机,为什么要用多种加载器呢?因为Java虚拟机启动的时候,并不会一次性加载所有的class文件(内存会爆),而是根据需要去动态加载。
泛型
擦拭法 - 廖雪峰的官方网站
====================================20230609
时间相关的处理
16 用好Java 8的日期时间类,少踩一些“老三样”的坑.md
https://www.jianshu.com/p/6652c0e08e0f
https://www.cnblogs.com/cgy-home/p/15718166.html
Java8 新日期时间类使用总结(LocalDateTime、LocalDate、LocalTime、Instant、DateTimeFormatter等)_locadate和new date_调侃而已的博客-CSDN博客
java8引入了一套全新的时间日期API
新的时间及日期API位于java.time中,包中的是类是不可变且线程安全的。
ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则
Instant: 用来表示时间线上的一个点(瞬时)
LocalDate: 表示没有时区的日期, LocalDate是不可变并且线程安全的
LocalTime: 表示没有时区的时间, LocalTime是不可变并且线程安全的
LocalDateTime: 表示没有时区的日期时间, LocalDateTime是不可变并且线程安全的
ZonedDateTime——这是一个包含时区的完整的日期时间,偏移量是以UTC/格林威治时间为基准的
Clock: 用于访问当前时刻、日期、时间,用到时区
Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔
Period: 用于计算两个“日期”间隔