一、java中的執行順序
1. 父類靜態塊
2. 自身靜態塊
3. 父類塊
4. 父類構造器
5. 自身塊
6. 自身構造器
2. *DOG父類
3. */
4.public class Dog {
5. public Dog() {
6. System.out.println("Dog");
7. }
8. static{
9. System.out.println("super static block");
10. }
11.
12. {
13. System.out.println("super block");
14. }
15.}
2. * 子類藏獒
3. */
4.public class Mastiff extends Dog {
5. public Mastiff() {
6. System.out.println("Mastiff");
7. }
8.
9. {
10. System.out.println("block");
11.
12. }
13. static {
14. System.out.println("static block");
15. }
16.
17. public static void main(String[] args){
18. Mastiff mastiff=new Mastiff();
19. }
20.}
運行的結果爲:
super static block
static block
super block
Dog
block
Mastiff
二、JAVA中賦值順序
1.父類的靜態變量賦值
2.自身的靜態變量賦值
3.父類成員變量賦值
4.父類塊賦值
5.父類構造器賦值
6.自身成員變量賦值
7.自身塊賦值
8.自身構造器賦值
2. *DOG父類
3. */
4.public class Dog {
5. public String type="父類成員變量賦的值";
6. public Dog() {
7. System.out.println("父類構造器--type-->"+type);
8. type="父類構造器賦的值";
9. System.out.println("父類構造器----type--->"+type);
10. }
11.
12. {
13. System.out.println("block---type--->"+type);
14. type="父類塊賦的值";
15. }
16.}
2. * 子類藏獒
3. */
4.public class Mastiff extends Dog {
5. public String type="成員變量賦的值";
6. public Mastiff() {
7. System.out.println("構造器---type--->"+type);
8. type="構造器賦的值";
9. }
10.
11. public void say(){
12. System.out.println("say---type---->"+type);
13. }
14.
15. {
16. System.out.println("block---type--->"+type);
17. type="塊賦的值";
18.
19. }
20.
21. public static void main(String[] args){
22. Mastiff mastiff=new Mastiff();
23. mastiff.say()</span><span style="font-size: medium;">;</span><span style="font-size: medium;">
24. }
25.}
執行結果如下:
block---type--->父類成員變量賦的值
父類構造器--type-->父類塊賦的值
父類構造器----type--->父類構造器賦的值
block---type--->成員變量賦的值
構造器---type--->塊賦的值
say---type---->構造器賦的值