動手動腦①

1 package test_1;
2
3 public class Test {
4
5 public static void main(String[] args) {
6 // TODO 自動生成的方法存根
7 Foo obj1=new Foo();
8 Foo obj2=new Foo();
9 System.out.println(obj1==obj2);
10 }
11 }
12 class Foo{
13 int value=https://www.cnblogs.com/rainbow-1/archive/2020/10/07/100;
14 }
動手動腦②:類欄位的初始化順序:
1 package test_1;
2
3 public class Test {
4
5 public static void main(String[] args) {
6 // TODO 自動生成的方法存根
7 InitializeBlockClass obj=new InitializeBlockClass();
8 System.out.println(obj.getField());
9
10 obj=new InitializeBlockClass(300);
11 System.out.println(obj.getField());
12 }
13 }
14 class InitializeBlockClass{
15
16 {
17 setField(200); //代碼塊定義值是200
18 }
19 private int field=100; //定義初始值是 100
20 InitializeBlockClass(int value) { //傳入引數是300
21 this.setField(value);
22 }
23 InitializeBlockClass() {
24 }
25 public int getField() {
26 return field;
27 }
28 public void setField(int field) {
29 this.field = field;
30 }
31 }
1 package test_1;
2
3 public class Test {
4
5 public static void main(String[] args) {
6 // TODO 自動生成的方法存根
7 InitializeBlockClass obj=new InitializeBlockClass();
8 System.out.println(obj.getField());
9
10 obj=new InitializeBlockClass(300);
11 System.out.println(obj.getField());
12 }
13 }
14 class InitializeBlockClass{
15 private int field=100; //定義初始值是 100
16 {
17 setField(200); //代碼塊定義值是200
18 }
19
20 InitializeBlockClass(int value) { //傳入引數是300
21 this.setField(value);
22 }
23 InitializeBlockClass() {
24 }
25 public int getField() {
26 return field;
27 }
28 public void setField(int field) {
29 this.field = field;
30 }
31 }
1 package test_1;
2
3 class Root
4 {
5 static
6 {
7 System.out.println("Root的靜態初始化塊");
8 }
9
10 {
11 System.out.println("Root的普通初始化塊");
12 }
13
14 public Root()
15 {
16 System.out.println("Root的無引數的構造器");
17 }
18 }
19 class Mid extends Root
20 {
21 static
22 {
23 System.out.println("Mid的靜態初始化塊");
24 }
25 {
26 System.out.println("Mid的普通初始化塊");
27 }
28 public Mid()
29 {
30 System.out.println("Mid的無引數的構造器");
31 }
32 public Mid(String msg)
33 {
34 //通過this呼叫同一類中多載的構造器
35 this();
36 System.out.println("Mid的帶引數構造器,其引數值:" + msg);
37 }
38 }
39 class Leaf extends Mid
40 {
41 static
42 {
43 System.out.println("Leaf的靜態初始化塊");
44 }
45 {
46 System.out.println("Leaf的普通初始化塊");
47 }
48 public Leaf()
49 {
50 //通過super呼叫父類中有一個字串引數的構造器
51 super("Java初始化順序演示");
52 System.out.println("執行Leaf的構造器");
53 }
54
55 }
56
57 public class TestStaticInitializeBlock
58 {
59 public static void main(String[] args)
60 {
61 new Leaf();
62
63
64 }
65 }
規律總結:
一個父類后繼承兩個子類,呼叫順序為:總順序先static代碼塊后普通代碼塊 最后構造代碼塊,先父類后子類,先無參構造,后帶參構造!
*****************************************************
動手動腦④:

1 package test_1; 2 3 public class StaticStudy { 4 static int staticnum=1; 5 private int num; 6 public static void display() { 7 StaticStudy test=new StaticStudy(); 8 System.out.println("在靜態方法中輸出靜態變數:"+test.staticnum); 9 System.out.println("在靜態方法中輸出非靜態變數:"+test.num); 10 } 11 public int getNum() { 12 return num; 13 } 14 public void setNum(int num) { 15 this.num = num; 16 } 17 public StaticStudy(int num) { 18 this.num = num; 19 } 20 public StaticStudy() { 21 22 } 23 public static void main(String[] args) { 24 // TODO 自動生成的方法存根 25 StaticStudy test0=new StaticStudy(100); 26 test0.display(); 27 } 28 29 }
//只需要在靜態變數里實體化一個本類的物件,通過這個物件取用本物件的資料成員!
動手動腦⑤:
Java中的包裝類
1 package test_1;
2
3 public class StrangeIntegerBehavior
4 {
5 public static void main(String[] args)
6 {
7 Integer i1=100;
8
9 Integer j1=100;
10
11 System.out.println(i1==j1);
12
13 Integer i2=129;
14
15 Integer j2=129;
16
17 System.out.println(i2==j2);
18
19 }
20 }



-128 --- 127 范圍內是相等的,超出范圍一定會重新生成物件!所以地址值就會不同!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/161762.html
標籤:其他
上一篇:Python匯入模塊的幾種方法
下一篇:Python匯入模塊的幾種方法
