X32专项练习部分09
- 双向链表的搜索速度方差
- 适合存放数据元素的结构
- 有无符号位运算
- 类变量和局部变量
- (important)依赖注入和控制反转
- Integer自动装箱
- 构造方法赋值
- 代码块执行顺序
- 总目录
双向链表的搜索速度方差
适合存放数据元素的结构
有无符号位运算
public static void main(String[] args) {
System.out.println(20 >>> 2); // 5
System.out.println(-20 >> 2); // -5
System.out.println(-20 >>> 2); // 1073741819
}
类变量和局部变量
class Test {
public static int a = 1;
public static void main(String[] args) {
int a = 10;
a++; // 类变量+1
Test.a++; // 局部变量+1
Test t=new Test();
System.out.println("a=" + a + " t.a=" + t.a);
// a = 11 t.a = 2
}
}
(important)依赖注入和控制反转
// 解耦前
class Main {
public static void main(String[] args) {
Chinese chinese = new Chinese();
chinese.sayHelloWorld("张三");
American american = new American();
american.sayHelloWorld("Jack");
}
}
interface Human {
public void sayHelloWorld(String name);
}
class Chinese implements Human {
public void sayHelloWorld(String name) {
String helloWorld = "你好," + name;
System.out.println(helloWorld);
}
}
class American implements Human {
public void sayHelloWorld(String name) {
String helloWorld = "Hello," + name;
System.out.println(helloWorld);
}
}
// 解耦后
class Main {
public static void main(String[] args) {
HumanFactory humanFactory = new HumanFactory();
Human human1 = humanFactory.getHuman("chinese");
human1.sayHelloWorld("张三");
Human human2 = humanFactory.getHuman("american");
human2.sayHelloWorld("Jack");
}
}
interface Human {
public void sayHelloWorld(String name);
}
class HumanFactory {
public Human getHuman(String type) {
if ("chinese".equals(type)) {
return new Chinese();
} else {
return new American();
}
}
}
class Chinese implements Human {
public void sayHelloWorld(String name) {
String helloWorld = "你好," + name;
System.out.println(helloWorld);
}
}
class American implements Human {
public void sayHelloWorld(String name) {
String helloWorld = "Hello," + name;
System.out.println(helloWorld);
}
}
Integer自动装箱
Integer a1 = 1;
Integer b1 = 1;
Integer c1 = 500;
Integer d1 = 500;
System.out.print(a1 == b1); // true
System.out.print(c1 == d1); // false
构造方法赋值
class TestDemo{
private int count;
public static void main(String[] args) {
TestDemo test=new TestDemo(88);
System.out.println(test.count); // 88
}
TestDemo(int a) {
count=a;
}
}
代码块执行顺序
class HelloA {
public HelloA() {
System.out.println("A的构造函数");
}
{
System.out.println("A的构造代码块");
}
static {
System.out.println("A的静态代码块");
}
public static void main(String[] args) {
HelloA a = new HelloA();
}
}
总目录