this和static
生活随笔
收集整理的這篇文章主要介紹了
this和static
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 【this】
2 指的是明確的標記本類的結構
3 當前正在調用類中方法的對象,不是一個固定的
4 java中以“{}”為界限。如果現在屬性名稱和參數名稱重名,那么默認情況下,如果沒有加任何的限制,指的是最近的“{}”內的變量名稱。
5 在這種情況下明確找到要訪問的變量屬于類中的屬性的時候,需要在變量前加this,這樣就可以準確的進行屬性的標記
6 1.★★在以后的程序中,只要訪問類中的屬性,前面必須加“this”★★
7 范例:
8 public class Book {
9 private String title;
10 private double price;
11
12 public Book(String title,double price){
13 /*同名的屬性,指的都是系統默認值*/
14 title = title;
15 price = price;
16 /*this.屬性,后面是傳入的參數*/
17 this.title = title;
18 this.price = price;
19 }
20
21 public String getInfo(){
22 return "書名" + this.title + ",價格" + this.price;
23 }
24
25 public String getTitle() {
26 return title;
27 }
28
29 public void setTitle(String title) {
30 this.title = title;
31 }
32
33 public double getPrice() {
34 return price;
35 }
36
37 public void setPrice(double price) {
38 this.price = price;
39 }
40
41 public static void main(String[] args) {
42 Book book = new Book("java",89.5);
43 System.out.println(book.getInfo());
44 }
45 }
46
47 調用方法:普通方法,構造方法
48 構造方法之間進行互相調用,形式this.(參數,參數)
49 2.★嚴謹地,調用本類方法,一定加上this★
50 注意:1.this 一定要放在構造方法首行,也盡量放在普通方法首行
51 2.類中構造方法間的弧線調用,一定要保留出口
52 即,this 互調構造方法時,一定要至少保留一個構造方法沒有使用this調用其他構造方法
53 范例:
54 public class Emp {
55 private int empno;
56 private String ename;
57 private double sal;
58 private String dept;
59
60 //構造函數可以快捷生成(source)
61 public Emp(){
62 this(0,"無名氏",0.0,"未定");
63 }
64
65 public Emp(int empno){
66 this(empno, "臨時工", 800, "后勤部");
67 }
68
69 public Emp(int empno,String ename){
70 this(empno, ename, 2000, "技術部");
71 }
72 /*保留了一個(構造方法)的出口*/
73 public Emp(int empno,String ename,double sal,String dept){
74 this.empno = empno;
75 this.ename = ename;
76 this.sal = sal;
77 this.dept = dept;
78 }
79
80 public void print(){
81 System.out.println("**************************");
82 }
83
84 public String getInfo(){
85 this.print(); //調用本類的方法
86 return "雇員編號:" + this.empno + ",\t姓名:" + this.ename + ",\t工資:" + this.sal + ",\t部門:" + this.dept;
87 }
88
89 public static void main(String[] args) {
90 Emp emp = new Emp();
91 Emp emp2 = new Emp(1);
92 Emp emp3 = new Emp(2, "李四");
93 Emp emp4 = new Emp(3, "張三", 1000.8, "行政部");
94 System.out.println(emp.getInfo());
95 System.out.println(emp2.getInfo());
96 System.out.println(emp3.getInfo());
97 System.out.println(emp4.getInfo());
98
99 }
100 }
101
102
103 【static】
104 ★★編寫類時,很少用到★★,除非類中只有方法,這時可以把該方法定義為靜態方法
105 類.靜態屬性
106 類.靜態方法
107 1.屬性
108 范例:
109 public class Book {
110 private String title;
111 private double price;
112
113 static String pub = "清華大學出版社";
114
115 public Book(String title,double price){
116 this.title = title;
117 this.price = price;
118 }
119
120 public void print(){
121 System.out.println("********************");
122 }
123
124 public String getInfo1(){
125 this.print(); //調用本類方法
126 return "書名:" + this.title + ",價格:" + this.price + ",出版社:" + this.pub;
127 }
128
129 public String getTitle() {
130 return title;
131 }
132
133 public void setTitle(String title) {
134 this.title = title;
135 }
136
137 public double getPrice() {
138 return price;
139 }
140
141 public void setPrice(double price) {
142 this.price = price;
143 }
144
145 public static void main(String[] args) {
146 Book book1 = new Book("java",89.5);
147 Book book2 = new Book("Android",12);
148 Book book3 = new Book("Oracle",20);
149 /*靜態屬性,只要一個對象修改了屬性的內容,所有對象的static屬性的內容就都將一起改變*/
150 //book1.pub = "北京大學出版社";
151 Book.pub = "北京大學出版社";
152 System.out.println(book1.getInfo1());
153 System.out.println(book2.getInfo1());
154 System.out.println(book3.getInfo1());
155 }
156 }
157
158 2.方法
159 范例:
160 package march_21;
161
162 public class Book {
163 private String title;
164 private double price;
165
166 static String pub = "清華大學出版社";
167
168 public Book(String title,double price){
169 this.title = title;
170 this.price = price;
171 }
172 /*靜態方法*/
173 public static void setPub(String p){
174 pub = p;//this不能點靜態屬性
175 }
176
177 public void print(){
178 System.out.println("********************");
179 }
180
181 public String getInfo1(){
182 this.print(); //調用本類方法
183 return "書名:" + this.title + ",價格:" + this.price + ",出版社:" + this.pub;
184 }
185
186 public String getTitle() {
187 return title;
188 }
189
190 public void setTitle(String title) {
191 this.title = title;
192 }
193
194 public double getPrice() {
195 return price;
196 }
197
198 public void setPrice(double price) {
199 this.price = price;
200 }
201
202 public static void main(String[] args) {
203 /*沒實例化對象,也可以利用類名直接調用*/
204 Book.setPub("北京大學出版社");
205
206 Book book1 = new Book("java",89.5);
207 Book book2 = new Book("Android",12);
208 Book book3 = new Book("Oracle",20);
209 System.out.println(book1.getInfo1());
210 System.out.println(book2.getInfo1());
211 System.out.println(book3.getInfo1());
212 }
213 }
214
215 ★static可以不new,直接調用;非static一定要new,才能調用★
216 1.static方法不能直接訪問非static屬性或者方法,只能調用static屬性或方法
217 2.非static方法可以調用static的屬性或方法,不受限制
218
219 main函數中String[] args:程序運行的時候傳遞的參數,形式:TestDemo 參數 參數 參數
220 如果參數本身有空格,形式:TestDemo "Hello World" "Hello MLDN"
221
222 功能一:實現類實例化對象個數的統計
223 希望每當實例化一個類對象的時候都可以打印一個信息:產生的第x個實例化對象。
224 因為只要是新的實例化對象產生了,那么一定會去調用類中的構造方法,
225 所以可以在構造方法里面增加一個統計數據的操作,每當新對象產生之后統計的內容就自增一個。
226 范例:
227 public class BookStatic {
228 private static int num = 0;
229
230 public BookStatic(){
231 num ++;
232 System.out.println("這是第" + num + "個產生的對象");
233 }
234
235 public static void main(String[] args) {
236 new BookStatic();new BookStatic();new BookStatic();
237 new BookStatic();new BookStatic();new BookStatic();
238 }
239 }
240
241 功能二:實現屬性的自動設置
242 例如,現在某一個類有一個無參構造方法,一個有參構造方法,有參構造主要的目的是傳遞一個title屬性,
243 但是希望不管調用的是無參的還是有參的構造,都可以為title設置內容(盡量不使用重復的內容設置)。
244 范例:
245 public class BookStatic {
246 private static int num = 0;
247 private String title;
248
249 public BookStatic(){
250 this("NOTITLE -- " + num++);
251 // num++;
252 }
253
254 public BookStatic(String title) {
255 this.title = title;
256 }
257
258 public String getTitle(){
259 return this.title;
260 }
261
262 public static void main(String[] args) {
263 System.out.println(new BookStatic("java").getTitle());
264 System.out.println(new BookStatic().getTitle());
265 System.out.println(new BookStatic().getTitle());
266 }
267 }
268
269 /*執行先后順序:靜態塊>靜態代碼>靜態的構造方法*/
270 public class StaticDemo {
271 public StaticDemo(){
272 System.out.println("靜態的構造方法");
273 }
274
275 //數據庫用的比較多
276 {
277 System.out.println("這是靜態代碼");
278 }
279 /*程序運行前,加載進去的,不管有么有實例化對象*/
280 static{
281 System.out.println("這是靜態塊");
282 }
283
284 public static void main(String[] args) {
285 StaticDemo staticDemo = new StaticDemo();
286 }
287 }
?
轉載于:https://www.cnblogs.com/ivy-xu/p/5304096.html
總結
以上是生活随笔為你收集整理的this和static的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Studio中获取SHA
- 下一篇: 【Poj1017】Packets