面向?qū)ο蟪跫?ppt_第1頁
面向?qū)ο蟪跫?ppt_第2頁
面向?qū)ο蟪跫?ppt_第3頁
面向?qū)ο蟪跫?ppt_第4頁
面向?qū)ο蟪跫?ppt_第5頁
已閱讀5頁,還剩61頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、第5章,面向?qū)ο蟮某绦蛟O(shè)計概念,一、面向?qū)ο蟮幕靖拍?抽象; 封裝; 繼承; 多態(tài)。,面向?qū)ο蟪绦蛟O(shè)計的基本特征:,對象與類,簡單地說: 對象是表示現(xiàn)實世界中某個具體的事物; 類是對對象的抽象描述。,對象 (object) 我們可以把生活所在的真實世界(Real World)當作是由許多大小不同的對象所組成的。對象可以是有生命的個體,比如一個人或一只鳥。,對象,對象也可以是無生命的個體,比如一輛汽車或一臺計算機。 對象也可以是一件抽象的概念,如天氣的變化或鼠標所產(chǎn)生的事件。,對象(object),計算機世界中的對象是把數(shù)據(jù)及其相關(guān)的操作封裝在一起所構(gòu)成的實體。 封裝的實體 = 數(shù)據(jù)+方法(行

2、為) 數(shù)據(jù):屬性或狀態(tài) 方法: 作用于數(shù)據(jù)上的操作 封裝:屬性的訪問和變化通過方法完成,對象的特征,對象有兩個特征:狀態(tài)(state)和行為(behavior)。例如:一個人有他的身高或體重作狀態(tài),并有他的行為如唱歌、打球、騎摩托車、開汽車。一只狗有它的顏色作狀態(tài),也有它的行為,如吠叫或跳躍。 而在程序設(shè)計中,軟件對象的概念由真實世界對象而來。對象的概念是面向?qū)ο筌浖ο髮顟B(tài)保存在變量(variables)或稱數(shù)據(jù)字段(data field)里。而行為則借助方法(methods)為工具來實現(xiàn) 。,軟件對象的定義,我們可以對軟件對象作以下的定義,定義:對象是由數(shù)據(jù)字段(變量)及相關(guān)方法所組成的

3、軟件包,汽車對象,以汽車為例,我們可定義其狀態(tài)與方法如:,通過換檔(方法)改變當前檔位(屬性),當前檔位,面向?qū)ο笤O(shè)計思想,1、抽象:是從特定的對象(實例)中抽取共同的性質(zhì)以形成類(一般化概念)的過程。,二、類(class)的概念,類:是一種抽象的數(shù)據(jù)類型,它是所有具有一定共性的對象的抽象。本質(zhì)上可以認為是對對象的描述,使創(chuàng)建對象的“模板”。 類的某一個對象則被稱為是類的一個實例,是類的實例化結(jié)果。,實例化,抽象,類 (class),在真實世界里,有許多同“種類”的對象。而這些同“種類”的對象可被歸類為一個“類”。 例如我們可將世界上所有的汽車歸類為汽車類,所有的動物歸為動物類。,實例 (in

4、stance),汽車類有些共同的狀態(tài)(汽缸排氣量,排檔數(shù),顏色,輪胎數(shù))和行為(換檔,開燈,開冷氣),但每一臺汽車個別的狀態(tài)及方法可不同于且獨立于其他汽車 。 你的汽車只是這世界中許多汽車中的一個。我們就稱你的汽車對象是汽車類中的一個實例(instance)。,方法(method),方法 是對象的行為方式(操作) 對象與外界的接口 作用 改變對象的屬性 返回對象的屬性,object1,object2,面向?qū)ο蠓椒ǖ闹饕獌?yōu)點,與人類習慣的思維方法一致 可重用性 可擴展性 可管理性,三、定義類,修飾符 class 類名 extends 父類 implements 接口名 類成員變量聲明; 類方法聲

5、明; ,任務(wù)5-1 定義類,public class Student String name; int No; String dorm; String tel; void checkIn(String a,int b) name=a; No=b; void assignDorm(String a) dorm=a; void provideTel(String b) tel=b; ,四、對象的定義,修飾符 類名 對象名=new 類名(實參列表); 或 修飾符 類名 對象名; 對象名=new 類名(實參列表);,創(chuàng)建對象,類名 對象名; Student zhang; 注意:類屬于復合數(shù)據(jù)類型,因此

6、,在聲明對象時,系統(tǒng)并沒有為對象分配空間,用戶需要應用new完成分配空間的任務(wù)。 zhang =new Student( );,與數(shù)組的對比性 int score; score=new int10;,合二為一:Student zhang=new Student();,對象的引用,引用成員變量 對象名.成員變量名 引用方法 對象名.方法名(參數(shù)列表),5.1.2對象的生命周期,1.對象的生成 2.對象的使用 3對象的清除,5.1.3由類的定義產(chǎn)生對象,【任務(wù)5-2】使用Student類,public class TermBegins public static void main(String

7、args) Student zhang; zhang=new Student(); zhang.checkIn(張三, 001); zhang.assignDorm(A-101); videTel; System.out.println(姓名:+); System.out.println(學號:+zhang.No); System.out.println(宿舍:+zhang.dorm); System.out.println(聯(lián)系電話:+zhang.tel); ,姓名:張三 學號:1 宿舍:A-101 聯(lián)系電話:130123456

8、78,創(chuàng)建對象,演示,定義一個cube類,代表一個立方體 包含的屬性有:長、寬、高,分別保存在變量x,y,z里面 方法有:求表面積 求體積 在main函數(shù)里定義一個cube類對象a,并設(shè)置立方體a的長寬高分別為3,4,5,并求出立方體a的表面積和體積打印出來。,上機作業(yè)1,設(shè)計一個交通工具Vehicle,屬性包括 最大速度 speed、顏色 color、類別 kind、 方法包括設(shè)置速度,顏色,類別,取得速度。顏色,類別 創(chuàng)建Vehicle對象,并為其設(shè)置新最大速度和顏色和類別,并顯示其最大速度、顏色、類別,上機作業(yè)2,(1)編寫一個位置類Position,包含兩個成員變量:橫坐標x,縱坐標y

9、;一個方法:printInfo,打印出橫坐標和縱坐標; (2) 編寫Position類的測試程序,創(chuàng)建一個點類的對象a,橫坐標、縱坐標分別為3,4,要求輸出點a的相關(guān)信息;,舉例:汽車的例子,package ch5.week2; public class Car int speed; String color; String no; void drive(int a) speed=speed+a; void print() System.out.println(speed=+speed); System.out.println(color=+color); System.out.println

10、(no =+no); void changeColor(String aColor) color=aColor; void changeNo(String aNo) no=aNo; ,package ch5.week2; public class Cube /與main方法平行的地方寫cube的屬性和方法 int x,y,z; int area() int tmp=2*(x*y+y*z+z*x); return tmp; int volumn() int tmp=x*y*z; return tmp; public static void main(String args) Cube b1=ne

11、w Cube(); b1.x=3; b1.y=4; b1.z=5; System.out.println(表面積+b1.area(); System.out.println(體積+b1.volumn(); ,舉例:立方體的例子,package ch5.week2; public class Dog int age; String name; float weight; boolean isHungry; void grow() age=age+1; void eat(float foodWeight) weight=weight+foodWeight; ,舉例:小狗的例子,測試各種類的程序,p

12、ackage ch5.week2; public class test /* * param args */ public static void main(String args) / TODO Auto-generated method stub /dog /car /*Car myCar=new Car(); /組裝新車 myCar.color=red; myCar.no=gz9090; myCar.speed=0; myCar.print(); /改裝車 myCar.drive(40); myCar.changeColor(blue); myCar.changeNo(888888);

13、myCar.print(); Car yourCar=new Car(); /組裝新車 yourCar.color=green; yourCar.no=gz9091; yourCar.speed=0; yourCar.print(); /改裝車 yourCar.drive(40); yourCar.changeColor(yellow); yourCar.changeNo(888886); yourCar.print(); Car sheCar=new Car(); Car aBoyCar=sheCar;*/ Dog dog1; dog1=new Dog(); =tom; d

14、og1.age=2; dog1.weight=2; dog1.eat(0.5f); System.out.print(dog1體重+dog1.weight); Dog dog2=new Dog(); =jack; dog2.age=2; dog2.grow(); System.out.println(dog2年齡+dog2.age); ,第三周課,5.1.4類的構(gòu)造函數(shù),【任務(wù)5-3】為Student類定義構(gòu)造方法。,public class Student String name; int No; String dorm; String tel; Student(Strin

15、g aName,int aNo) name=aName; No=aNo; void assignDorm(String a) dorm=a; void provideTel(String b) tel=b; public static void main(String args) Student zhang=new Student(張三,001); zhang.assignDorm(A-101); videTel; ,【任務(wù)5-4】為Student類定義多個構(gòu)造方法。,源代碼在備注中,5.2 封裝性,封裝的定義包括下面幾個方面: (1)一個清晰的邊界

16、,所有對象的內(nèi)部軟件范圍限定在這個邊界之內(nèi)。 (2)一個接口,該接口描述當前對象和其他對象之間的交互作用。 (3)內(nèi)部實現(xiàn),對象內(nèi)部的實現(xiàn)是受保護的,這個實現(xiàn)給出了軟件對象功能的細節(jié),定義當前對象的類的外面不能訪問這些實現(xiàn)細節(jié)。,5.2.1類變量和類方法,對象的初始化,類中包含有成員變量和方法,當使用類聲明并創(chuàng)建對象時,使用new關(guān)鍵字,這時系統(tǒng)為對象創(chuàng)建自己的內(nèi)存區(qū)域并自動調(diào)用構(gòu)造方法初始化成員變量。,構(gòu)造方法,構(gòu)造方法是一個特殊的成員方法,它與類名相同。在創(chuàng)建對象空間后,系統(tǒng)自動根據(jù)參數(shù)調(diào)用相應的構(gòu)造函數(shù)。構(gòu)造函數(shù)的主要功能是為數(shù)據(jù)成員賦初值。,構(gòu)造方法的特點,構(gòu)造方法名與類名相同; 構(gòu)造

17、方法沒有返回類型,也不是void; 構(gòu)造方法的主要作用是對類對象的初始化。如果沒有定義構(gòu)造方法時,成員變量將被初始化為各種類型的默認值; 構(gòu)造方法只能與new關(guān)鍵字初始化對象時使用,其他時候不能使用; 一個類可以定義多個構(gòu)造方法,根據(jù)參數(shù)的不同決定執(zhí)行哪一個。,public class Student String name; int No; String dorm; String tel; Student(String aName,int aNo) name=aName; No=aNo; void assignDorm(String a) dorm=a; void provideTel(St

18、ring b) tel=b; public static void main(String args) Student zhang=new Student(張三,001); zhang.assignDorm(A-101); videTel; ,任務(wù)5-3定義構(gòu)造方法,構(gòu)造方法作用: 避免對象創(chuàng)建后沒有任何內(nèi)容,強制程序員在創(chuàng)建對象時就給某些變量一些初始值,任務(wù)5-4多個構(gòu)造方法,Student(String aName,int aNo) name=aName; No=aNo; Student(int aNo) No=aNo; Student(Stri

19、ng aName) name=aName; ,多個構(gòu)造方法,為程序員提供多種選擇,在創(chuàng)建對象時,可以任意使用其中一個,public class Student String name; int No; String dorm; String tel; Student(String aName,int aNo) name=aName; No=aNo; Student(int aNo) No=aNo; Student(String aName) name=aName; void assignDorm(String a) dorm=a; void provideTel(String b) tel=b

20、; ,任務(wù)5-4源代碼,public static void main(String args) Student zhang=new Student(張三); zhang.assignDorm(A-101); videTel; System.out.println(姓名:+); System.out.println(學號:+zhang.No); System.out.println(宿舍:+zhang.dorm); System.out.println(聯(lián)系電話:+zhang.tel); System.out.println(*)

21、; Student li=new Student(002); li.assignDorm(A-102); videTel; System.out.println(姓名:+); System.out.println(學號:+li.No); System.out.println(宿舍:+li.dorm); System.out.println(聯(lián)系電話:+li.tel); ,任務(wù)5-4源代碼,課后實訓一,一、類的定義及使用 (1)編寫一個位置類Position,包含兩個成員變量:橫坐標x,縱坐標y;一個方法:printInfo,打印出橫坐標和縱坐

22、標; (2) 編寫Position類的測試程序,創(chuàng)建一個點a(3,4),要求輸出點a的相關(guān)信息; (3) 將Position類的的兩個成員變量改為私有,并添加相應的set和get方法,用 set方法為點a賦值為(5,7),用get方法取出a的坐標并打印出來; (4) 為Position類添加一個不帶參數(shù)構(gòu)造方法,將橫坐標縱坐標初始化為原點,在測試程序中新增1個原點; (5)為Position類添加一個帶參數(shù)的構(gòu)造方法,傳兩個參數(shù)用于初始化坐標。在測試程序中新增1個點b(8,9); (6)為Position類添加一個靜態(tài)變量TotalNum,用于統(tǒng)計創(chuàng)建的Position對象數(shù),在測試程序中輸出

23、總的點數(shù)量。,五、類成員變量的定義,定義: 訪問權(quán)限符 static final 類型 變量名 權(quán)限修飾符有: public、protected和private static:在成員變量前說明該變量是靜態(tài)變量。 final:在成員變量前說明該變量是一個最終變量,修飾符 static,static可以修飾類中的屬性和方法。 靜態(tài)屬性在類定義時靜態(tài)地分配空間。它屬于類,不屬于某個特定的對象,因此可以通過類進行訪問。往往將屬于類的公共屬性說明成static。,任務(wù)5-5 static成員變量,public class Student String name; int No; String dorm;

24、 String tel; static int count=0; Student(String aName,int aNo) name=aName; No=aNo; count+; Student(int aNo) No=aNo; ,Student(String aName) name=aName; static void printCount() System.out.println(count); public static void main(String args) Student.printCount(); Student zhang=new Student(張三,001); Stu

25、dent li=new Student(李四,003); System.out.println(Student.count); ,Java實例 static方法,class Sta_method int width,height;/這兩個變量沒用到 public static double area(int width,int height) return width*height; public static void main(String args) int i,j; double f; i=Integer.parseInt(args0); j=Integer.parseInt(args

26、1); f=area(i,j); /直接引用此方法 System.out.println(Area=+i+*+j+=+f); ,靜態(tài)方法,由于靜態(tài)方法屬于整個類,因此它不能處理屬于某個對象的變量,只能處理屬于整個類的變量靜態(tài)變量。 靜態(tài)方法中只能調(diào)用靜態(tài)方法。,static的訪問規(guī)則,一個對象的方法可以訪問對象的數(shù)據(jù)成員,也可以訪問方法的局部變量 一個類方法只能訪問自己的局部變量或者類變量。,static的訪問規(guī)則,解決的辦法: 1. 將變量改成類變量 class StaticError static String mystring=“hello”; public static void ma

27、in(String args) System.out.println(mystring); ,static的訪問規(guī)則,2. 訪問局部變量 class NoStaticError public static void main(String args) String mystring=“hello”; System.out.println(mystring); ,【任務(wù)5-5】用static變量統(tǒng)計Student類對象個數(shù)。,/static變量和方法的使用 public class Student String name; int No; String dorm; String tel; sta

28、tic int count=0; Student(String aName,int aNo) name=aName; No=aNo; count+; Student(int aNo) No=aNo; Student(String aName) name=aName; static void printCount() System.out.println(已報到人數(shù):+count); public static void main(String args) Student.printCount(); Student zhang=new Student(張三,001); Student li=ne

29、w Student(李四,003); System.out.println(Student.count); ,已報到人數(shù):0 已報到人數(shù):2,5.2.2 使用private將變量封裝起來,【任務(wù)5-6】private修飾符讓Student類的數(shù)據(jù)更安全。,/設(shè)置private變量 public class Student String name; int No; String dorm; String tel; private static int count=0; Student(String aName,int aNo) name=aName; No=aNo; count+; static

30、 void printCount() System.out.println(總?cè)藬?shù):+ count); ,public class useStudent public static void main(String args) Student zhang=new Student(張三,001); Student li=new Student(李四,003); Student.printCount();/利用函數(shù)訪問count變量 /System.out.println(Student.count); /上句有語法錯誤,Student私有變量count不能被訪問 ,5.3擴展應用,5.3.1數(shù)組

31、與對象 【任務(wù)5-7】用數(shù)組處理多個學生對象。,public class Student String name; int no ; int score; void printInfo() System.out.println(name+t+no+t+score); ,public class useStudent public static void main(String args) Student netClass=new Student10; int score=90,67,78,90,65,45,67,89,76,67,; String name=susan,tom,jerry,ja

32、ck,rose, maggie,elisha,Dick ,Harry,John; for (int i=0;i=9;i+) netClassi=new Student();/為每個數(shù)組元素分配空間 netClassi.no=i+1; netC=namei; netClassi.score=scorei; System.out.println(name+t+no+t+score); for (int i=0;i=9;i+) netClassi.printInfo(); ,namenoscore susan190 tom267 jerry378 jack490 rose565

33、maggie645 elisha767 Dick 889 Harry976 John1067,5.3.2 對象作為方法的參數(shù)和返回值,【任務(wù)5-8】輸入和返回參數(shù)為學生類對象。,public class Student String name; int no ; double score1;/成績1 double score2;/成績2 int sustainers;/支持者 public Student(String name, int no, double score1, double score2, int sustainers) super(); = name; t

34、his.no = no; this.score1 = score1; this.score2 = score2; this.sustainers = sustainers; ,/比較兩個學生的信息,輸出綜合指數(shù)高的同學作為班長 public class useStudentPk static Student pk(Student a,Student b) double aEx=a.score1*0.2+a.score2*0.3+a.sustainers*0.5; double bEx=b.score1*0.2+b.score2*0.3+b.sustainers*0.5; if (aExbEx)

35、 return a; else return b; public static void main(String args) Student zhang=new Student(zhangsan,001,90,90,15); Student li=new Student(lisi,002,89,90,20); Student wang=new Student(wangwu,007,98,92,10); Student zhao=new Student(zhaoliu,012,89,70,25); Student tmp=pk(zhang,li); Student monitor=pk(tmp,

36、wang); monitor=pk(tmp,zhao); System.out.print(取勝者為:+); ,取勝者為:lisi,5.3.3 對象作為類的成員,public class Communication double mobliePhone;/移動電話 String eMail;/電子郵件 double qq;/qq號碼 String telephone;/宿舍電話 public Communication(double mobliePhone, String mail, double qq, String telephone) super(); this.

37、mobliePhone = mobliePhone; this.eMail = mail; this.qq = qq; this.telephone = telephone; ,public class Student String name; int no; Communication com1; public Student(String name, int no, Communication com1) super(); = name; this.no = no; 1 = com1; ,public class TestStudent public static vo

38、id main(String args) Communication com=new Communication(1311115566,123456; Student zhang=new Student(zhangsan,001,com); ,5.4習題,一、選擇題 1. 以下說法不正確的是()。 A) 類是同種對象的集合和抽象 B) 類是抽象的數(shù)據(jù)類型 C) 類是復合數(shù)據(jù)類型 D) 類是一個對象 2. 定義類的類頭時可以使用的關(guān)鍵字是()。 A) private B) protected C) final D) static 3. 下列選項中,用于在定義子類時聲明

39、父類名的關(guān)鍵字是()。 A)interface B) package C) extends D) class 4.下列類頭定義中,錯誤的是()。 A) public x extends y . B) public class x extends y . C) class x extends y implements y1 . D) class x .,5. 設(shè) A為已定義的類名,下列聲明A類的對象a的語句中,正確的是()。 A) float A a; B) public A a=A( ); C) A a=new int( ); D) static A a=new A( ); 6. 設(shè) A為已定

40、義的類名,下列聲明A類的對象a的語句中正確的是()。 A) public A a=new A( ); B) public A a=A( ); C) A a=new class( ); D) a A; 7. 設(shè) X 、Y 均為已定義的類名,下列聲明類X的對象x1的語句中正確的是()。 A) public X x1= new Y( ); B) X x1= X ( ); C) X x1=new X( ); D) int X x1; 8. 設(shè)i , j為類X中定義的int型變量名,下列X類的構(gòu)造函數(shù)中不正確的是()。 A) void X(int k ) i=k; B) X(int k ) i=k; C) X(int m, int n ) i=m; j=n; D) X( )i=0;j=0U1; U1希望后面加一些填空題和簡答題,以求和面得章節(jié)的統(tǒng)一。,二、填空題: 1、一個對象的三個生命周期是_、_、_。 2、 使用一個對象前,必須聲明并_它。 3、 創(chuàng)建類對象的運算符是_。創(chuàng)建的目的是_。 4、通過類MyClass中的不含參數(shù)的構(gòu)造函數(shù),生成該類的一個對象obj,可通過以下語句實現(xiàn): _。 5、通過_

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論