# 第四十三周ARTS总结
# Algorithm
0ms | 100.00% Run time
37.1MB | 5.88% Memory
public double myPow(double x, int n) {
if (x == 0 || x == 1) {
return x;
}
if (n < 0) {
return pow(1 / x, n);
} else {
return pow(x, n);
}
}
/**
* 此时n的正负不影响结果
*
* @param x
* @param n
* @return
*/
private double pow(double x, int n) {
if (n == 0) {
return 1;
}
// x^n=(x^2)^(n/2)
return (pow(x * x, n / 2) * (n % 2 == 0 ? 1 : x));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Review
# Tip
- 获取Class对象的三种方式 [1] (opens new window):
Class class1 = Apple.class;
Apple apple = new Apple; Class class2 = apple.getClass();
1
2Class class3 = Class.forName("XXX");
- 反射常见的使用方法 [2] (opens new window):
getDeclaredFields
:获取所有参数,不管何种级别都能获取getDeclaredField("color")
:获得指定的参数getFields
:获取所有public类型的参数getField
:获得指定public类型的参数getDeclaredMethods
:获取所有方法,不管何种级别都能获取getDeclaredMethod("getColor")
:获得指定方法名的方法,不能获取父类中的方法getMethods
:获取所有public类型的方法getMethod("toString")
:获取指定名称的public类型方法getDeclaredConstuctors
:获取所有的构造方法getDeclaredConstructor
:获取指定的带某些参数的构造方法getConstructors
:获取所有public类型的构造方法getConstructor
:获取指定的带某些参数的public类型构造方法newInstance
:通过无参构造方法创建类的实例newInstance(XXXX)
:通过有参构造方法创建类的实例getName
:获取类的全定名getSimpleName
:获取类名invoke(实例)
:通过反射调用方法isAccessible
:判断方法是否启用访问安全检查setAccessible
:设置安全检查开关
- Lombok的使用 [3] (opens new window):
- IDE中安装Lombok插件
- 导入相关依赖
- 代码中使用注解:
@Data
、@Getter/@Setter
等
- Lombok的缺点 [4] (opens new window):
- 同事需要安装相关插件,否则会报错
- 代码可读性以及可调试性差
- 底层原理不了解会有坑
- 代码侵入性较大,影响升级
- 影响封装性
- Lombok的好处 [5] (opens new window):
- 不需要手写get()/set()
- 更优雅的生成对象:
Course course = Course.builder().id(123l).name("高等数学").score(100).build()
- 不需要手写全参/无参/部分参构造方法
- 不需要手写非空判断
# Share
暂无内容