forked from CPU-Code/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSonTest.java
More file actions
49 lines (41 loc) · 998 Bytes
/
Copy pathSonTest.java
File metadata and controls
49 lines (41 loc) · 998 Bytes
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.cpucode.java.bytecode.structure;
/**
* 成员变量(非静态的)的赋值过程:
* ① 默认初始化
* ② 显式初始化 /代码块中初始化
* ③ 构造器中初始化
* ④ 有了对象之后,可以“对象.属性”或"对象.方法"的方式对成员变量进行赋值。
*
* @author : cpucode
* @Date : 2021/1/21
* @Time : 21:42
* @Github : https://github.com/CPU-Code
* @CSDN : https://blog.csdn.net/qq_44226094
*/
public class SonTest {
public static void main(String[] args) {
Father f = new Son();
System.out.println(f.x);
}
}
class Father{
int x = 10;
public Father(){
this.print();
x = 20;
}
public void print(){
System.out.println("Father.x = " + x);
}
}
class Son extends Father{
int x = 30;
public Son(){
this.print();
x = 40;
}
@Override
public void print(){
System.out.println("Son.x = " + x);
}
}