什么是抽象Abstract

抽象Abstract:【新手可忽略不影響繼續(xù)學(xué)習(xí)】    很多java 的書中都談到了抽象abstract的概念,到底什么是抽象?馬克-to-win:抽取關(guān)鍵相關(guān)特性(屬性和方法)構(gòu)成對象,用程序的方法邏輯和數(shù)據(jù)結(jié)構(gòu) 屬性模擬現(xiàn)實的世界對象。比如上節(jié)的例子,現(xiàn)實世界的計算機里的window很復(fù)雜,那么多像素,那么多顏色,那我們?nèi)绾屋腿〕龊臀覀兿嚓P(guān)的屬性和方法完 成我們的客戶的需求呢?這個過程就叫抽象。上例中我們只抽象出了title屬性和close方法就可以滿足用戶需求。
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。


【新手可忽略不影響繼續(xù)學(xué)習(xí)】參見以上例子,width就是對象的屬性,close就是對象的方法,簡單來講,所有對象的方法都一樣,就寫在類中,只寫一份。對象屬性的值,每個對象和每個對象都不一樣。既然對于所有對象來講,方法都一樣,而只有屬性不一樣,能區(qū)分對象的,就只有屬性了,這樣來講,觀察屬性,就顯得尤為重要。我們可以認為,方法就是用來改變屬性的。就拿上個例子來講: @馬克-to-win對于baoFengObject和xunLeiObject來講,開始時,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è):做一個類叫兩個數(shù)。類里有兩個屬性,x和y,類里有一個方法叫設(shè)置x,它可以設(shè)置x的大小。類里還有一個方法,是打印xy??梢园褁y兩個值都打印出來。提高部分:類里還有一個方法叫做增加x。這個方法可以把x和輸入?yún)?shù)相加返回。做測試類測試。



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.百度翻譯:做一個類叫window,他有兩個屬性叫x,y,1個方法,叫做拖拽drag , 方法拖拽能夠改變x,y的坐標(biāo),也需要創(chuàng)建另外一個方法叫做print, 它能夠打印當(dāng)前的x,y坐標(biāo),在你的主函數(shù)當(dāng)中,你創(chuàng)建兩個對象, 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) 百度翻譯:做一個類叫做精靈英雄,他有兩個屬性叫x,y和經(jīng)驗值和三個方法叫做移動,點擊,打印方法,能夠打印出所有的屬性, 當(dāng)點擊的時候,經(jīng)驗值就被增加一, 然后做一個主函數(shù)測試,它們經(jīng)驗值是5。x,y是30,30。  

 




實驗

本章源碼
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