Skip to content

Commit f9ef530

Browse files
committed
疯狂Java
1 parent 6a953de commit f9ef530

File tree

6 files changed

+243
-4
lines changed

6 files changed

+243
-4
lines changed

Diff for: .idea/misc.xml

+1-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/Main.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import crazy.CrazyTest;
2+
13
public class Main {
24

35
public static void main(String[] args) {
6+
7+
CrazyTest test1 = new CrazyTest();
8+
test1.testMethod();
9+
410
System.out.println("Hello World!");
511
}
612
}

Diff for: src/crazy/CacheImmutale.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package crazy;
2+
3+
/**
4+
* Created by Gaol on 2018/6/13.
5+
*/
6+
public class CacheImmutale {
7+
8+
private static int MAX_SIZE = 10;
9+
10+
//使用数组来缓存已有的实例
11+
private static CacheImmutale[] cache = new CacheImmutale[MAX_SIZE];
12+
//记录缓存实例在缓存中的位置,cache[pos-1]是最新缓存的实例
13+
private static int pos = 0;
14+
private final String name;
15+
16+
private CacheImmutale(String name) {
17+
18+
this.name = name;
19+
}
20+
21+
public String getName() {
22+
23+
return name;
24+
}
25+
26+
public static CacheImmutale valueOf(String name) {
27+
28+
//遍历已缓存的对象
29+
for(int i = 0; i< MAX_SIZE; i++) {
30+
31+
//如果已有相同实例,则直接返回该缓存实例
32+
if(cache[i] != null && cache[i].getName().equals(name)) {
33+
return cache[i];
34+
}
35+
}
36+
37+
if(pos == MAX_SIZE) {
38+
39+
//把缓存的第一个对象覆盖,即把刚刚生成的对象放在缓存池最开始的位置
40+
cache[0] = new CacheImmutale(name);
41+
//把pos设为1
42+
pos = 1;
43+
44+
} else {
45+
46+
//把新创建的对象缓存起来,pos 加 1
47+
cache[pos++] = new CacheImmutale(name);
48+
}
49+
return cache[pos-1];
50+
}
51+
52+
public boolean equals(Object obj) {
53+
54+
if(this == obj) {
55+
return true;
56+
}
57+
58+
if(obj != null && obj.getClass() == CacheImmutale.class) {
59+
CacheImmutale ci = (CacheImmutale)obj;
60+
return name.equals(ci.getName());
61+
}
62+
63+
return false;
64+
}
65+
66+
public int hashCode() {
67+
68+
return name.hashCode();
69+
}
70+
71+
72+
}

Diff for: src/crazy/CrazyTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package crazy;
2+
3+
/** 疯狂Java 讲义
4+
* Created by Gaol on 2018/6/13.
5+
*/
6+
public class CrazyTest {
7+
8+
Singleton s1 = Singleton.getInstance();
9+
Singleton s2 = Singleton.getInstance();
10+
11+
public void testMethod() {
12+
13+
System.out.println(s1 == s2);
14+
}
15+
16+
}

Diff for: src/crazy/Singleton.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package crazy;
2+
3+
/**
4+
* Created by Gaol on 2018/6/13.
5+
*/
6+
public class Singleton {
7+
8+
final String str = "Hello";
9+
10+
private static Singleton instance;
11+
12+
private Singleton() {}
13+
14+
public static Singleton getInstance() {
15+
16+
if(instance == null) {
17+
18+
instance = new Singleton();
19+
}
20+
21+
return instance;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)