Skip to content

Commit 7ab7b0e

Browse files
committed
初始化相关的代码
1 parent ee28fe3 commit 7ab7b0e

12 files changed

Lines changed: 677 additions & 0 deletions

File tree

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
package InitializationAndCleanup;
2+
3+
/**
4+
* 重载基本类型
5+
*
6+
* @author :chenxin
7+
* @date :Created in 2020-03-18 14:16
8+
* @description:重载基本类型
9+
* @modified By:
10+
* @version: $
11+
*/
12+
public class PrimitiveOverloading {
13+
void f1(char x) {
14+
System.out.print("f1(char)");
15+
}
16+
17+
void f1(byte x) {
18+
System.out.print("f1(byte)");
19+
}
20+
21+
void f1(short x) {
22+
System.out.print("f1(short)");
23+
}
24+
25+
void f1(int x) {
26+
System.out.print("f1(int)");
27+
}
28+
29+
void f1(long x) {
30+
System.out.print("f1(long)");
31+
}
32+
33+
void f1(float x) {
34+
System.out.print("f1(float)");
35+
}
36+
37+
void f1(double x) {
38+
System.out.print("f1(double)");
39+
}
40+
41+
void f2(byte x) {
42+
System.out.print("f2(byte)");
43+
}
44+
45+
void f2(short x) {
46+
System.out.print("f2(short)");
47+
}
48+
49+
void f2(int x) {
50+
System.out.print("f2(int)");
51+
}
52+
53+
void f2(long x) {
54+
System.out.print("f2(long)");
55+
}
56+
57+
void f2(float x) {
58+
System.out.print("f2(float)");
59+
}
60+
61+
void f2(double x) {
62+
System.out.print("f2(double)");
63+
}
64+
65+
void f3(short x) {
66+
System.out.print("f3(short)");
67+
}
68+
69+
void f3(int x) {
70+
System.out.print("f3(int)");
71+
}
72+
73+
void f3(long x) {
74+
System.out.print("f3(long)");
75+
}
76+
77+
void f3(float x) {
78+
System.out.print("f3(float)");
79+
}
80+
81+
void f3(double x) {
82+
System.out.print("f3(double)");
83+
}
84+
85+
void f4(int x) {
86+
System.out.print("f4(int)");
87+
}
88+
89+
void f4(long x) {
90+
System.out.print("f4(long)");
91+
}
92+
93+
void f4(float x) {
94+
System.out.print("f4(float)");
95+
}
96+
97+
void f4(double x) {
98+
System.out.print("f4(double)");
99+
}
100+
101+
void f5(long x) {
102+
System.out.print("f5(long)");
103+
}
104+
105+
void f5(float x) {
106+
System.out.print("f5(float)");
107+
}
108+
109+
void f5(double x) {
110+
System.out.print("f5(double)");
111+
}
112+
113+
void f6(float x) {
114+
System.out.print("f6(float)");
115+
}
116+
117+
void f6(double x) {
118+
System.out.print("f6(double)");
119+
}
120+
121+
void f7(double x) {
122+
System.out.print("f7(double)");
123+
}
124+
125+
void testConstVal() {
126+
System.out.print("5: ");
127+
f1(5);
128+
f2(5);
129+
f3(5);
130+
f4(5);
131+
f5(5);
132+
f6(5);
133+
f7(5);
134+
System.out.println();
135+
}
136+
137+
void testChar() {
138+
char x = 'x';
139+
System.out.print("char: ");
140+
f1(x);
141+
f2(x);
142+
f3(x);
143+
f4(x);
144+
f5(x);
145+
f6(x);
146+
f7(x);
147+
System.out.println();
148+
}
149+
150+
void testByte() {
151+
byte x = 0;
152+
System.out.print("byte: ");
153+
f1(x);
154+
f2(x);
155+
f3(x);
156+
f4(x);
157+
f5(x);
158+
f6(x);
159+
f7(x);
160+
System.out.println();
161+
}
162+
163+
void testShort() {
164+
short x = 0;
165+
System.out.print("short: ");
166+
f1(x);
167+
f2(x);
168+
f3(x);
169+
f4(x);
170+
f5(x);
171+
f6(x);
172+
f7(x);
173+
System.out.println();
174+
}
175+
176+
void testInt() {
177+
int x = 0;
178+
System.out.print("int: ");
179+
f1(x);
180+
f2(x);
181+
f3(x);
182+
f4(x);
183+
f5(x);
184+
f6(x);
185+
f7(x);
186+
System.out.println();
187+
}
188+
189+
void testLong() {
190+
long x = 0;
191+
System.out.print("long: ");
192+
f1(x);
193+
f2(x);
194+
f3(x);
195+
f4(x);
196+
f5(x);
197+
f6(x);
198+
f7(x);
199+
System.out.println();
200+
}
201+
202+
void testFloat() {
203+
float x = 0;
204+
System.out.print("float: ");
205+
f1(x);
206+
f2(x);
207+
f3(x);
208+
f4(x);
209+
f5(x);
210+
f6(x);
211+
f7(x);
212+
System.out.println();
213+
}
214+
215+
void testDouble() {
216+
double x = 0;
217+
System.out.print("double: ");
218+
f1(x);
219+
f2(x);
220+
f3(x);
221+
f4(x);
222+
f5(x);
223+
f6(x);
224+
f7(x);
225+
System.out.println();
226+
}
227+
228+
public static void main(String[] args) {
229+
PrimitiveOverloading p = new PrimitiveOverloading();
230+
p.testConstVal();
231+
p.testChar();
232+
p.testByte();
233+
p.testShort();
234+
p.testInt();
235+
p.testLong();
236+
p.testFloat();
237+
p.testDouble();
238+
}
239+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package InitializationAndCleanup.initialize;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
/**
7+
* @author :chenxin
8+
* @date :Created in 2020-03-20 11:51
9+
* @description:
10+
* @modified By:
11+
* @version: $
12+
*/
13+
public class ArratNew {
14+
public static void main(String[] args) {
15+
int[] a;
16+
Random random = new Random(47);
17+
System.out.println(random.nextInt(10));
18+
a = new int[random.nextInt(20)];
19+
int[] b = new int[random.nextInt(60)];
20+
System.out.println(Arrays.toString(a));
21+
System.out.println(Arrays.toString(b));
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package InitializationAndCleanup.initialize;
2+
3+
/**
4+
* @author :chenxin
5+
* @date :Created in 2020-03-20 11:36
6+
* @description:
7+
* @modified By:
8+
* @version: $
9+
*/
10+
public class ArraysOfPrimitives {
11+
12+
public static void main(String[] args) {
13+
int[] a1 = {1, 2, 3, 4, 5, 6};
14+
int[] a2;
15+
a2 = a1;
16+
for (int i = 0; i < a2.length; i++) {
17+
a2[i] += 1;
18+
}
19+
for (int i : a1) {
20+
System.out.println(i);
21+
}
22+
}
23+
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package InitializationAndCleanup.initialize;
2+
3+
/**
4+
* static域初始化
5+
*
6+
* @author :chenxin
7+
* @date :Created in 2020-03-20 10:31
8+
* @description:static域初始化
9+
* @modified By:
10+
* @version: $
11+
*/
12+
public class ExplicitStatic {
13+
public static void main(String[] args) {
14+
System.out.println("Inside main()");
15+
Cups.cup1.f(99);
16+
}
17+
18+
}
19+
20+
class Cup {
21+
public Cup(int marker) {
22+
System.out.println("Cup(" + marker + ")");
23+
}
24+
25+
void f(int marker) {
26+
System.out.println("f(" + marker + ")");
27+
}
28+
}
29+
30+
class Cups {
31+
static Cup cup1;
32+
static Cup cup2 = new Cup(2);
33+
34+
static {
35+
cup1 = new Cup(1);
36+
// cup2 = new Cup(2);
37+
}
38+
39+
public Cups() {
40+
System.out.println("Cups()");
41+
}
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package InitializationAndCleanup.initialize;
2+
3+
/**
4+
* 参数调用方法初始化
5+
*
6+
* @author :chenxin
7+
* @date :Created in 2020-03-19 14:58
8+
* @description:参数调用方法初始化
9+
* @modified By:
10+
* @version: $
11+
*/
12+
public class MethodInitParamter {
13+
int i = f();
14+
15+
int f() {
16+
return 10;
17+
}
18+
}
19+
20+
class MethodIni2 {
21+
int i = f();
22+
23+
int k = g(i);
24+
25+
int f() {
26+
return 10;
27+
}
28+
29+
int g(int n) {
30+
return 10 * n;
31+
}
32+
}
33+
34+
35+
class MethodIni3 {
36+
// int k = g(i);
37+
38+
int i = f();
39+
40+
int f() {
41+
return 10;
42+
}
43+
44+
int g(int n) {
45+
return 10 * n;
46+
}
47+
}

0 commit comments

Comments
 (0)