關于面向對象和封裝的個人理解
類和對象
類:對事物的一種描述(具有共同屬性和行為的事物的抽象),例如手機,屬性:品牌價格,行為:玩游戲,刷vx;
對象:客觀存在(在java中體現就是mian方法里面用類定義一個對象,然后用對象去調用方法或者調用成員變量)
二者關系:類為屬性行為抽象,對象則為實體。
對象內存圖理解:堆內存開辟空間,成員變量出現 并產生默認初始化值,將對象地址值記錄以便于通過對象名調用成員變量。
成員變量和局部變量的區別:類中位置不同,內存中位置不同,生命周期不同,初始化值不同(成員變量(有默認初始化值)局部變量(沒有默認初始化值,必須先定義,賦值才能使用)。
封裝
private關鍵字:被private修飾的成員,只能在本類進行訪問,針對private修飾的成員變量,如果需要被其他類使用,提供相應的操作(get,set方法)
this關鍵字:this修飾的變量用于指代成員變量,其主要作用是(區分局部變量和成員變量的重名問題)。
封裝理解: 將類的某些信息隱藏在類內部,不允許外部程序直接訪問,而是通過該類提供的方法來實現對隱藏信息的操作和訪問(private修飾和get,set方法)
封裝的好處以及作用: 把代碼用方法進行封裝,提高了代碼的復用性, 通過方法來控制成員變量的操作,提高了代碼的安全性。
難題匯總
銀行賬戶
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package test3; public class bank { public static void main(String[] args) { //在測試類Bank中創建銀行賬戶類對象和用戶類對象, // 并設置信息,與顯示信息 Customer customer = new Customer( "李華" , "123456789" , "987456321" , "新華小區" ); Account account = new Account( 1111115646 , 1000000 ,customer); customer.say(); account.withdraw( 10000 ); account.save( 9999999 ); System.out.println(customer.say()); System.out.println(account.getinfo()); if (account.withdraw( 10000 )== true ){ System.out.println( "取錢成功" ); System.out.println( "余額還有" +account.getBalance()); } else { System.out.println( "取款失敗" ); } if (account.save( 444444 )== true ){ System.out.println( "存款成功" ); System.out.println( "余額還有" +account.getBalance()); } else { System.out.println( "存款失敗" ); } } } package test3; /*2.定義銀行賬戶類Account,有屬性:卡號cid,余額balance,所屬用戶Customer 銀行賬戶類Account有方法:(1)getInfo(),返回String類型,返回卡的詳細信息 (2)取錢方法withdraw(),參數自行設計,如果取錢成功返回true,失敗返回false (3)存錢方法save(),參數自行設計,如果存錢成功返回true,失敗返回false 其中Customer類有姓名、身份證號、聯系電話、家庭地址等屬性 Customer類有方法say(), 返回String類型,返回他的個人信息。?在測試類Bank中創建銀行賬戶類對象和用戶類對象, 并設置信息,與顯示信息*/ public class Account { private int cid ; private int balance; private Customer customer; public Account(Customer customer) { this .customer = customer; } public int getCid() { return cid; } public void setCid( int cid) { this .cid = cid; } public int getBalance() { return balance; } public void setBalance( int balance) { this .balance = balance; } public Account(String s, int balance, Customer customer){ } public Account( int cid, int balance, Customer customer){ this .cid=cid; this .balance=balance; this .customer=customer; } //(1)getInfo(),返回String類型,返回卡的詳細信息 號cid,余額balance,所屬用戶Customer public String getinfo(){ String info = "卡號" +cid+ "\n余額" +balance+ "\n用戶" +customer.getName(); return info; } //取錢方法withdraw(),參數自行設計,如果取錢成功返回true,失敗返回false public boolean withdraw( int out_balance) { if (out_balance <= balance) { balance -= out_balance; return true ; } return false ; } //存錢方法save(),參數自行設計,如果存錢成功返回true,失敗返回false public boolean save ( int in_banlance){ if (in_banlance >= 0 ){ balance += in_banlance; return true ; } return false ; } } package test3; //其中Customer類有姓名、身份證號、聯系電話、家庭地址等屬性 public class Customer { private String name; private String idcard; private String call; private String adress; public String getName() { return name; } public void setName(String name) { this .name = name; } public String isIdcard() { return idcard; } public void setIdcard(String idcard) { this .idcard = idcard; } public String getCall() { return call; } public void setCall(String call) { this .call = call; } public String getAdress() { return adress; } public void setAdress(String adress) { this .adress = adress; } public Customer(){ } public Customer(String name, String idcard,String call,String adress){ this .name=name; this .idcard=idcard; this .call = call; this .adress=adress; } public String say(){ String info = "姓名" +name+ "\n身份證號" +idcard+ "\n電話" +call+ "\n地址" +adress; return info; } } |
理解類中引用類就是再寫一個就行,不用想的太復雜。
坐標點
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package test2; //定義一個類,用于描述坐標點 // //? 0——————>X // //? | // //? | // //? | P(X,Y) // //? | // //? | // //? Y // // // //(1)具有計算當前點到原點距離的功能 // //(2)求到任意一點(m,n)的距離 // //(3)求到任意一點(Point p)的距離 // //(4)具有坐標點顯示功能,顯示格式(x,y) // //(5)提供無參的構造器和一個有參的構造器 public class test2 { public static void main(String[] args) { point w= new point( 10 , 20 ); w.oxy(); w.mnxy( 66 , 77 ); w.ponitp( 14 , 16 ); w.show(); } } public class point { int x ; int y ; int m ; int n ; public int getM() { return m; } public void setM( int m) { this .m = m; } public int getN() { return n; } public void setN( int n) { this .n = n; } public int getX() { return x; } public void setX( int x) { this .x = x; } public int getY() { return y; } public void setY( int y) { this .y = y; } public point(){ } public point( int x , int y){ this .y=y; this .x = x; } public void oxy(){ System.out.println(Math.sqrt( x*x+y*y )); } //(2)求到任意一點(m,n)的距離 public void mnxy ( int m , int n ){ this .m=m; this .n=n; System.out.println(Math.sqrt( ((m-x)*(m-x)+(n-y)*(n-y)) )); } //(3)求到任意一點(Point p)的距離 public void ponitp ( int z , int k ){ System.out.println(Math.sqrt( ((z-x)*(z-x)+(k-y)*(k-y)) )); } //(4)具有坐標點顯示功能,顯示格式(x,y) public void show(){ System.out.println( "(" +x+ "," +y+ ")" ); } } |
學生隨機數排序
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// An highlighted block var foo = 'bar' ; package test1; //定義類Student,包含三個屬性:學號number(int),年級state(int),成績 score(int)。 //創建20個學生對象,學號為1到20,年級和成績都由隨機數確定。 //問題一:打印出3年級(state值為3)的學生信息。 //問題二:使用冒泡排序按學生成績排序,并遍歷所有學生信息 //提示: 1) 生成隨機數:Math.random(),返回值類型double; (Matn為工具類)([0,1}) // 2) 四舍五入取整:Math.round(double d),返回值類long 型 public class demo5 { public static void main(String[] args) { Students [] stu = new Students[ 20 ]; for ( int i = 0 ; i < stu.length; i++) { //給數組元素賦值 stu[i]= new Students(); //給Student的對象的屬性賦值 stu[i].number = i + 1 ; //學號 stu[i].state = ( int )(Math.random() * ( 6 - 1 + 1 ) + 1 ); //(6 + 1));//年級[1,6] stu[i].score = ( int )(Math.random() * ( 100 - 0 + 1 )); //(100 - 0 + 1));//成績[0,100] } //遍歷學生數組 for ( int i = 0 ; i < stu.length; i++) { //System.out.println(stu[i].number + "," + stu[i].state + "," // + stu[i].score); System.out.println(stu[i].info()); } System.out.println( "*******************" ); //問題一:打印出3年級(state值為3)的學生信息。 for ( int i = 0 ; i < stu.length; i++) { if (stu[i].state == 3 ) { System.out.println(stu[i].info()); } } System.out.println( "\t" ); //問題二:使用冒泡排序按學生成績排序,并遍歷所有學生信息。 for ( int i = 0 ; i < stu.length - 1 ; i++) { for ( int j = 0 ; j < stu.length - 1 - i; j++) { if (stu[j].score > stu[j + 1 ].score){ //如果需要換序,交換的是數組的元素,Student對象!!! Students temp = stu[j]; stu[j] = stu[j + 1 ]; stu[j + 1 ] = temp; } } } for ( int i = 0 ; i < stu.length; i++) { System.out.println(stu[i].info()); } } } public class Students { //學號number(int),年級state(int),成績 score(int)。 int number; int state; int score; public int getNumber() { return number; } public void setNumber( int number) { this .number = number; } public int getState() { return state; } public void setState( int state) { this .state = state; } public int getScore() { return score; } public void setScore( int score) { this .score = score; } public String info(){ return "學號:" + number + ",年級:" + state + ",成績" + score; } |
到此這篇關于Java 面向對象和封裝全面梳理總結的文章就介紹到這了,更多相關Java 面向對象 內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/m0_62265391/article/details/120965410