Skip to content

Commit 432c9d9

Browse files
committed
Update final,static,this,super.md
1 parent 6e6d9da commit 432c9d9

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

docs/java/basic/final,static,this,super.md

+32-26
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public class Sub extends Super {
121121

122122
调用格式:
123123

124-
- 类名.静态变量名
125-
- 类名.静态方法名()
124+
- `类名.静态变量名`
125+
- `类名.静态方法名()`
126126

127127
如果变量或者方法被 private 则代表该属性或者该方法只能在类的内部被访问而不能在类的外部被访问。
128128

@@ -132,15 +132,15 @@ public class Sub extends Super {
132132
public class StaticBean {
133133

134134
String name;
135-
静态变量
135+
//静态变量
136136
static int age;
137137

138138
public StaticBean(String name) {
139139
this.name = name;
140140
}
141-
静态方法
141+
//静态方法
142142
static void SayHello() {
143-
System.out.println(Hello i am java);
143+
System.out.println("Hello i am java");
144144
}
145145
@Override
146146
public String toString() {
@@ -202,11 +202,11 @@ Example(静态内部类实现单例模式)
202202
```java
203203
public class Singleton {
204204

205-
声明为 private 避免调用默认构造方法创建对象
205+
//声明为 private 避免调用默认构造方法创建对象
206206
private Singleton() {
207207
}
208208

209-
声明为 private 表明静态内部该类只能在该 Singleton 类中被访问
209+
// 声明为 private 表明静态内部该类只能在该 Singleton 类中被访问
210210
private static class SingletonHolder {
211211
private static final Singleton INSTANCE = new Singleton();
212212
}
@@ -230,12 +230,10 @@ public class Singleton {
230230
```java
231231

232232

233-
Math. --- Math中的所有静态资源导入,这时候可以直接使用里面的静态方法,而不用通过类名进行调用
234-
如果只想导入单一某个静态方法,只需要将换成对应的方法名即可
233+
//将Math中的所有静态资源导入,这时候可以直接使用里面的静态方法,而不用通过类名进行调用
234+
//如果只想导入单一某个静态方法,只需要将换成对应的方法名即可
235235

236-
import static java.lang.Math.;
237-
238-
换成import static java.lang.Math.max;具有一样的效果
236+
import static java.lang.Math.;//换成import static java.lang.Math.max;具有一样的效果
239237

240238
public class Demo {
241239
public static void main(String[] args) {
@@ -291,44 +289,52 @@ class Foo {
291289
292290
一般情况下,如果有些代码比如一些项目最常用的变量或对象必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的。如果我们想要设计不需要创建对象就可以调用类中的方法,例如:Arrays类,Character类,String类等,就需要使用静态方法, 两者的区别是 静态代码块是自动执行的而静态方法是被调用的时候才执行的.
293291
294-
Example
292+
Example
295293
296294
```java
297295
public class Test {
298296
public Test() {
299-
System.out.print(默认构造方法!--);
297+
System.out.print("默认构造方法!--");
300298
}
301299
302-
非静态代码块
300+
//非静态代码块
303301
{
304-
System.out.print(非静态代码块!--);
302+
System.out.print("非静态代码块!--");
305303
}
306-
静态代码块
304+
305+
//静态代码块
307306
static {
308-
System.out.print(静态代码块!--);
307+
System.out.print("静态代码块!--");
309308
}
310309
311-
public static void test() {
312-
System.out.print(静态方法中的内容! --);
310+
private static void test() {
311+
System.out.print("静态方法中的内容! --");
313312
{
314-
System.out.print(静态方法中的代码块!--);
313+
System.out.print("静态方法中的代码块!--");
315314
}
316315
317316
}
318-
public static void main(String[] args) {
319317
320-
Test test = new Test();
321-
Test.test();静态代码块!--静态方法中的内容! --静态方法中的代码块!--
318+
public static void main(String[] args) {
319+
Test test = new Test();
320+
Test.test();//静态代码块!--静态方法中的内容! --静态方法中的代码块!--
322321
}
322+
}
323+
```
324+
325+
上述代码输出:
326+
327+
```
328+
静态代码块!--非静态代码块!--默认构造方法!--静态方法中的内容! --静态方法中的代码块!--
323329
```
324330
325-
当执行 `Test.test();` 时输出:
331+
当只执行 `Test.test();` 时输出:
326332
327333
```
328334
静态代码块!--静态方法中的内容! --静态方法中的代码块!--
329335
```
330336
331-
当执行 `Test test = new Test();` 时输出:
337+
当只执行 `Test test = new Test();` 时输出:
332338
333339
```
334340
静态代码块!--非静态代码块!--默认构造方法!--

0 commit comments

Comments
 (0)