什么是抽象Abstract
抽象Abstract:【新手可忽略不影響繼續(xù)學(xué)習(xí)】 很多java 的書(shū)中都談到了抽象abstract的概念,到底什么是抽象?馬克-to-win:抽取關(guān)鍵相關(guān)特性(屬性和方法)構(gòu)成對(duì)象,用程序的方法邏輯和數(shù)據(jù)結(jié)構(gòu) 屬性模擬現(xiàn)實(shí)的世界對(duì)象。比如上節(jié)的例子,現(xiàn)實(shí)世界的計(jì)算機(jī)里的window很復(fù)雜,那么多像素,那么多顏色,那我們?nèi)绾屋腿〕龊臀覀兿嚓P(guān)的屬性和方法完 成我們的客戶的需求呢?這個(gè)過(guò)程就叫抽象。上例中我們只抽象出了title屬性和close方法就可以滿足用戶需求。
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
【新手可忽略不影響繼續(xù)學(xué)習(xí)】參見(jiàn)以上例子,width就是對(duì)象的屬性,close就是對(duì)象的方法,簡(jiǎn)單來(lái)講,所有對(duì)象的方法都一樣,就寫(xiě)在類中,只寫(xiě)一份。對(duì)象屬性的值,每個(gè)對(duì)象和每個(gè)對(duì)象都不一樣。既然對(duì)于所有對(duì)象來(lái)講,方法都一樣,而只有屬性不一樣,能區(qū)分對(duì)象的,就只有屬性了,這樣來(lái)講,觀察屬性,就顯得尤為重要。我們可以認(rèn)為,方法就是用來(lái)改變屬性的。就拿上個(gè)例子來(lái)講: @馬克-to-win對(duì)于baoFengObject和xunLeiObject來(lái)講,開(kāi)始時(shí),width屬性都為0, baoFengObject.width=999;和xunLeiObject.width=111;以后,baoFengObject的width等于 999, 而xunLeiObject的width等于111。
本章源碼
class MyTestDate {
int year;
int month;
void setDate(int y, int m) {
year = y;
month = m;
}
String toStringabc() {
return "" + year + "/" + month ;
}
}
public class Test {
public static void main(String[] args) {
/* make the coin(硬幣) based on the template(模板).@馬克-to-win */
MyTestDate date = new MyTestDate();
MyTestDate date1 = new MyTestDate();
System.out.println("The initial date is:" + date.toStringabc());
date.setDate(2009, 7);
System.out.println("After setting, the date is:" + date.toStringabc());
System.out.println("The initial date1 is:" + date1.toStringabc());
}
}
result is:
The initial date is:0/0
After setting, the date is:2009/7
The initial date1 is:0/0
作業(yè):做一個(gè)類叫兩個(gè)數(shù)。類里有兩個(gè)屬性,x和y,類里有一個(gè)方法叫設(shè)置x,它可以設(shè)置x的大小。類里還有一個(gè)方法,是打印xy??梢园褁y兩個(gè)值都打印出來(lái)。提高部分:類里還有一個(gè)方法叫做增加x。這個(gè)方法可以把x和輸入?yún)?shù)相加返回。做測(cè)試類測(cè)試。
Assignment(作業(yè)): make a class called Window which has two properties called x,y and a method called drag(dx,dy), the method of drag can change the coordination of x,y, x=x+dx; also you need to create another method called print(), which can print the current x,y coordination. In your main method, you create two objects window1,window2, then you can drag window1 or window2, then print their x,y coordination.百度翻譯:做一個(gè)類叫window,他有兩個(gè)屬性叫x,y,1個(gè)方法,叫做拖拽drag , 方法拖拽能夠改變x,y的坐標(biāo),也需要?jiǎng)?chuàng)建另外一個(gè)方法叫做print, 它能夠打印當(dāng)前的x,y坐標(biāo),在你的主函數(shù)當(dāng)中,你創(chuàng)建兩個(gè)對(duì)象, window1,window2,然后你能拖拽window1,window2,然后打印他們的坐標(biāo)。
Assignment2(作業(yè)): make a class of SpriteHero which has two properties called x,y,experience, and a method called move(dx,dy) and hit(), print method can print out all of the properties. when hit, then experience is increased by 1. then make main to test.( initially experience value is 5. x, y is 30,30) 百度翻譯:做一個(gè)類叫做精靈英雄,他有兩個(gè)屬性叫x,y和經(jīng)驗(yàn)值和三個(gè)方法叫做移動(dòng),點(diǎn)擊,打印方法,能夠打印出所有的屬性, 當(dāng)點(diǎn)擊的時(shí)候,經(jīng)驗(yàn)值就被增加一, 然后做一個(gè)主函數(shù)測(cè)試,它們經(jīng)驗(yàn)值是5。x,y是30,30。
實(shí)驗(yàn)
本章源碼
class HelloClass {
int i;
int getI() {
return i;
}
void setI(int ii) {
i = ii;
}
}
public class Test {
public static void main(String[] args) {
HelloClass hc = new HelloClass();
hc.setI(9);
int ri = hc.getI();
System.out.println(ri);
}
}
result is: 9