面向對象程序設計期末復習_第1頁
面向對象程序設計期末復習_第2頁
面向對象程序設計期末復習_第3頁
面向對象程序設計期末復習_第4頁
面向對象程序設計期末復習_第5頁
已閱讀5頁,還剩49頁未讀 繼續(xù)免費閱讀

付費下載

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

面向對象程序設計

期末考試范圍

面向對象部分為主

Chapter8,10,11,13,14

時間2小時,英文題目,熟記每章后面的英文術語(KeyTerms)

變量和方法

變量、方法,如:

〃變量的申明:數(shù)據類型變量名

inta;

〃方法的申明:返回類型方法名(參數(shù)列表){語句}

intadd(inta,intb){…}

voidsetParameter(inta,intb){…}

注意大小寫

main方法

main方法是一個特殊的方法,是程序運行的入口

main方法的形式是固定的。

publicstaticvoidmain(String[]args){//Stringargs[]

inta=10;

intb=12;

intsum=a+b;

}

Java程序構成

Java是一種完全面向對象的程序設計語言,程序中不允許單獨出現(xiàn)任

何方法

類是構成Java程序的基本單位,也就是說,前面出現(xiàn)的變量、方法

都必須申明在類的內部。這些變量和方法稱為類的成員變量(屬性,

Attribute)和成員方法(Method),統(tǒng)稱為類的成員(Member)。

一個Java程序可以包含多個類,這些類可以封裝在不同的包中

Java類

申明類的語法規(guī)則

修飾符class類名{

變量的申明〃成員變量(屬性)

方法的申明〃成員方法

}

申明成員變量和申明成員方法的次序無關

一個類可以同時申明多個成員變量和成員方法,也可以不申明任何變

量或者方法,這些都是合法的。

在方法的內部也可以申明變量,這樣的變量稱為局部變量。

Java類舉例

publicclassPerson{

〃屬性:name、age>gender

〃方法:setName

〃方法:getName

}

Java程序舉例

綜合以上知識,我們將前面的main方法申明為類Test的一個成員:

publicclassTest{

publicstaticvoidmain(String[]args){

inta=10;〃變量a是一個局部變量

intb=12;

intsum=a+b;

習慣:類名首字母大寫,變量/方法名首字母小寫

數(shù)據類型

簡單數(shù)據類型:如int

類也是一種數(shù)據類型,稱為引用數(shù)據類型(復雜數(shù)據類型,reference

type)o因此以下變量和方法的申明都是正確的:

inta=10;

Personp;

Testb;

PersongetPerson(){---}

我們有時候也將類稱為類型,如變量p的類型為Person類型,注意:

這時候程序中必須已經申明了Person類,否則將產生錯誤。

Java程序舉例

classPerson{}

publicclassTest{

publicstaticvoidmain(---){

inta=10;〃變量a是一個局部變量

intb=12;

intsum=a+b;

Personp;

)

)

該Java程序由Person和Test兩個類構成

自定義類和預定義類

Java已經預先定義了很多類,這些類稱為預定義類

如:String

與此相對應,由我們自己定義的類則稱為自定義類,如前面的Person

類和Test類

如前面所說,類是一種數(shù)據類型。因此,無論是預定義類還是自定義

類,在程序中我們都可以用它們來申明變量的數(shù)據類型、或者作為方

法的返回類型。

Java類舉例

publicclassPerson{

Stringname;

intage;

publicvoidsetName(Stringstr){…}

publicStringgetName(){returnname;}

}

Java源程序

源程序保存在后綴名為Java的文件中。

一個Java源文件可以同時包含多個類的申明,但是其中最多只能有

一個用public修飾的類,即公有類。

源文件的名稱必須和公有類的名稱相一致,包括大小寫

舉例:Hello.java

publicclassHello

(

publicstaticvoidmain(Stringargs[])

(

System.out.println(HelloWorld!);

)

}

對象的創(chuàng)建

對象是類的實例

假設程序中申明了Person類,那么我們就可以在程序的任何地方創(chuàng)

建任意的Person類的實例,即Person類型的對象

對象創(chuàng)建的語法形式:

new類名(…);〃括號中是否有參數(shù),要依據構造方法

比如:newPerson();

Person類

publicclassPerson{

Stringname;

intage;

publicvoidsetAge(inta){

age=a;

}

publicintgetAge(){

returna;

}

}

對象的存在形式

對象存在于內存(堆,heap)

0x3A080x3A5C

name

age

對象的引用

對象由相應類型的變量引用,如:

Personpl=newPerson();

Personp2=newPerson();

0x3A5C

對象的操作

語法形式:對象成員,如:

pl.setAge(18);

p2.setAge(29);

0X3A5C

必須是對象.成員,不能是類.成員,除非該成員是static修飾的

Person.setAge(20),Error

關于方法調用

如方法有返回類型,則必須使用return語句,返回的值必須與返回類

型匹配

void返回類型可以沒有return語句,也可以包含一個沒有值的return

語句

publicclassPerson{

intage;

publicvoidsetAge(inta){

age=a;

return;

}

}

構造方法(Constructor)

Java中的每個類都有構造方法,構造方法具有和類名相同的名稱(包

括大小寫),而且無任何返回值(包括void)。

構造方法一般對成員變量賦一些初值;如果一個類沒有聲明構造方法,

Java自動為該類生成一個默認的無參的構造方法,并且該默認構造方

法的內容為空。即,Java類可以通過兩種方式獲得構造方法

使用系統(tǒng)默認的無參構造方法

顯式定義一個或多個構造方法

一旦顯式定義了構造方法,系統(tǒng)不再提供默認構造方法

一個類至少有一個構造方法,可以有多個構造方法,根據參數(shù)的不同

決定執(zhí)行哪一個(重載)

Person類

publicclassPerson{

Stringname;

intage;

publicPerson(){}〃構造方法

publicvoidsetAge(inta){age=a;}

publicintgetAge(){returna;}

)

引用數(shù)據類型

8種基本數(shù)據類型

intlongshortbooleanchardoublefloatbyte

除此以外的數(shù)據類型稱為引用類型

引用類型數(shù)據以對象的形式存在

引用類型變量的值是某個對象的句柄(對象在內存中的地址),而不是

對象本身

聲明引用類型變量時,系統(tǒng)只為該變量分配引用空間,并未創(chuàng)建一個

具體的對象,引用類型可賦值為null

類中的變量

屬性變量(成員變量):在方法體外定義的變量,是類的成員,可以是

基本類型、引用類型甚至是類本身,只要對象存在其屬性變量就存在。

作用域:類。

局部變量:在方法體內定義的變量,也稱為stackvariable,作用域:

方法。方法執(zhí)行時存在,方法執(zhí)行完畢后消失

局部變量在使用前必須進行初始化,而屬性變量可以不需要顯式進行

初始化,因為在創(chuàng)建對象時會進行默認的初始化。

舉例:MyDate類

publicclassMyDate{

privateintday=12;

privateintmonth=6;

privateintyear=1900;

publicMydate(intd,intm,inty){

year=y;

month=m;

day=d;

}

publicvoiddisplay(){

System.out.println(year+"/"+month+"/"+day);

}

publicstaticvoidmain(String[]args){

MyDatem;

m=newMyDate(22,9,2001);

m.display();

}

)

對象的構造和初始化(1)

為引用類型變量分配引用空間MyDatem;

mnull

創(chuàng)建新對象-首先為新對象分配內存空間,并進行屬性(實例變量)的

默認初始化,new

day

mnullmonth

year

對象的構造和初始化(2)

Java引用類型成員變量默認初始化原則

成員變量類型取值

byte0

short0

int0

long0L

char*\u0000,

float0.0F

double0.00

booleanfalse

所有引用類型null

對象的構造和初始化(3)

進行屬性(成員變量)的顯式初始化,顯式初始化取值來自于類的定義

中屬性聲明部分

privateintday=12;

privateintmonth=6;

privateintyear=1900;

day

m|null|month

year

對象的構造和初始化(4)

執(zhí)行構造方法

publicMydate(inty,intm,intd){

year=y;

month=m;

day=d;

}

newMyDate(22,9,2001);

mnull

對象的構造和初始化(5)

為引用類型變量m賦值

m=newMyDate(22,9,2001);

m0x3a478b

封裝類

Java編程語言提供wrapper類來操作作為對象的原始數(shù)據元素。

每個Java原始數(shù)據類型在java.lang包中具有相應的wrapper類。

使用wrapper類的示例:

intpint=420;

Integerwlnt=newInteger(plnt);

//thisiscalledboxing

intp2=Value();

//thisiscalledunboxing

自動裝箱與開箱

Integer(]intArray={newInteger(2)rabE-llnteger[)intArray={2,3};

newInteger(4),newInteger(3));

(a)(b)

Integeri=3;

不僅限于Integer,所有wrapper類都可以

Object[]o={3,false};〃合法

轉換

Integerintegerobject=lnteger.valueOf("12");

intnumber=Integer.parselnt("12");

注:lnteger.parselnt("xyz")編譯時沒問題,但運行時會異常

訪問權限(Permission)

Public;Protected;缺省(default);private

ModifierAccessedAccessedAccessedAccessed

onmeirbei'sfromthefromthefromafromadifferent

inaclasssameclasssamepackagesubclasspackage

publicMMM

protectedMM—

defaultMM——

privateM———

數(shù)組

數(shù)組由一組相同類型的元素構成

元素的個數(shù)稱為數(shù)組的長度,通過數(shù)組下標訪問元素,下標0代表第一

個元素

數(shù)組是一個對象,數(shù)組類型屬于引用數(shù)據類型,可賦值為null.

數(shù)組的申明和創(chuàng)建

?數(shù)組的申明

inta[];或intQa;

Pointp[];或PointOp;

?數(shù)組對象必須通過new關鍵字創(chuàng)建

數(shù)組的初始化

intoa={1,2,3,4,5};

PointQp={StringQusers={

newPoint(1,1),“John”,

newPoint(2,2)“Mary”

};};

參數(shù)傳遞1

基本變量傳值,引用變量傳地址

classMyDate{

privateintday;

privateintmonth;

privateintyear;

publicMyDate(intd,intm,inty){

day=d;month=m;year=y;

)

publicvoidsetDay(intd){day=d;}

publicvoidsetMonth(intm){month=m;}

publicvoidsetYear(inty){year=y;}

publicintgetDay(){returnday;}

publicintgetMonth(){returnmonth;}

publicintgetYear(){returnyear;}

publicvoiddisplay(){

System.out.println(day+"-"+month+"-"+year);

)

)

參數(shù)傳遞2

基本變量傳值,引用變量傳地址

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

MyDatedl=newMyDate(7,7,1970);

MyDated2=newMyDate(l,1,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(MyDateb){b=newMyDate(22,2,2004);}

publicvoidchange3(MyDateb){b.setDay(22);}

}

參數(shù)傳遞演示1

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7/7/1970);

BirthDated2=newBirthDate(l,l,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

)

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2/2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

}

參數(shù)傳遞演示2

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(lzl,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

}

Jr.

棧內1存-.

___r)

d23547521

dl587934\

main〈..

date——x9——\

[ex|1109251■-1970J

十處內存狀態(tài)

參數(shù)傳遞演示3

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(l,1,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

}

參數(shù)傳遞演示4

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(l,1,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

)

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

}

參數(shù)傳遞演示5

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(l,1,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

}

參數(shù)傳遞演示6

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(l,1,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){bnew

BirthDate(22,2z2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

)

參數(shù)傳遞演示7

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(l,1,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=new

BirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

)

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(l,l,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

}

參數(shù)傳遞演示9

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(l,1,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,272004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

參數(shù)傳遞演示10

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(lzl,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

}

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

)

參數(shù)傳遞演示11

publicclassExample{

publicstaticvoidmain(Stringargs[]){

Exampleex=newExample();

intdate=9;

BirthDatedl=newBirthDate(7,7,1970);

BirthDated2=newBirthDate(Ll,2000);

ex.changel(date);

ex.change2(dl);

ex.change3(d2);

System.out.println("date="+date);

dl.display();

d2.display();

)

publicvoidchangel(inti){i=1234;}

publicvoidchange2(BirthDateb){b=newBirthDate(22,2,2004);}

publicvoidchange3(BirthDateb){b.setDay(22);}

}

靜態(tài)方法

以static關鍵字申明的方法就稱為靜態(tài)方法,也稱為類方法

對于靜態(tài)方法,可以由類直接調用,當然,也可以通過對象進行調用

靜態(tài)變量

一個由static關鍵字修飾的變量,就稱為靜態(tài)變量,也稱為類變量,(請對

照實例變量)

如果一個類變量的訪問級別為public,那么可以通過類直接訪問該靜

態(tài)變量

靜態(tài)變量是多個對象間共享的

方法的重載

在同一個類中可以定義多個同名方法--方法重載

publicclassPrintStream{

publicvoidprintln(inti){.......}

publicvoidprintln(floatf){.......}

publicvoidprintln(Strings){.......}

)

重載方法的參數(shù)列表必須不同

重載方法的返回值類型可以相同,也可以不同

構造方法重載

構造方法重載舉例:

publicclassProject{

publicProject(){.......}

publicProject(Stringcode){.......}

publicProject(Stringcode,Stringname){.......}

}

構造方法重載,參數(shù)列表必須不同

可以在構造方法的第一行使用this關鍵字調用其它(重載的)構造方法

關鍵字this

指代當前對象自身

publicclassProject{

privateStringproj_code="";

privateStringproj_name="";

publicvoidsetName(Stringname){

j_name=name;〃等價于proj_name=name

}

}

publicclassTest{

publicstaticvoidmain(Stringargs[]){

Projectproj_l=newProject();proj_l.setName("abc");

〃this是proj_l

Projectproj_2=newProject();proj_2.setName("xyz");

//this是proj_2

}

}

publicvoidsetName(Stringproj_name){

j_name=proj_name;〃解決了變量的命名沖突和

不確定性問題

}

調用重載的構造方法

publicclassProject{

privateStringproj_code=

privateStringproj_name=

publicProject(Stringcode){

}

publicProject(){

this("abc");〃this必須是方法中的第一條語句

)

}

this指代當前對象,可以returnthis

this可以調用重載的構造方法,但不能直接用this初始化對象,new

this。是非法的

類的繼承

類繼承語法規(guī)則

<modifier>class<name>[extends<superclass>]

(

<declarations>

)

Object類是所有Java類的最高層父類

Java只支持單繼承,不允許多重繼承

一個子類只能有一個父類

一個父類可以繼承出多個子類

publicclassPerson{

publicStringname;

publicintage;

publicDatebirthDate;

publicStringgetlnfo(){...}

}

為描述和處理學生信息,定義類Student

publicclassStudent{

publicStringname;

publicintage;

publicDatebirthDate;

publicStringschool;

publicStringgetlnfo(){...}

}

通過繼承,簡化Student類的定義

publicclassPerson{

publicStringname;

publicintage;

publicDatebirthDate;

publicStringgetlnfo(){...}

}

publicclassStudentextendsPerson{

publicStringschool;

}

方法的重寫/覆蓋

在子類中可以根據需要對從父類中繼承來的方法進行改造--方法的

重寫

重寫方法必須和被重寫方法具有相同的方法名稱、參數(shù)列表和返回值

類型

重寫方法不能使用比被重寫方法更嚴格的訪問權限

構造方法不能被重寫,只能通過super調用

方法重寫舉例

publicclassPerson{

protectedStringname;

protectedintage;

protectedDatebirthDate;

publicStringgetlnfo(){

return"Name:"+name+"\n"+"age:"+age;

)

}

publicclassStudentextendsPerson{

protectedStringschool;

publicStringgetlnfo(){

return"Name:"+name+"\nage:"+age+"\nschool:"+

school;

}

}

關鍵字super

在Java類中使用super來引用父類的成分

super可用于訪問父類中定義的屬性

super可用于調用父類中定義的成員方法

super可用于在子類構造方法中調用父類的構造方法

super的追溯不僅于直接父類

關鍵字super舉例

publicclassPerson{

protectedStringname;

protectedintage;

protectedDatebirthDate;

publicStringgetlnfo(){

return"Name:"+name+"\n"+"age:"+age;

}

}

publicclassStudentextendsPerson{

protectedStringschool;

publicStringgetlnfo(){

〃調用父類的方法

returnsuper.getlnfo()+"\nschool:"+school;

}

}

調用父類構造方法

在子類的構造方法中可使用super(argument」ist)語句調用父類的構造

方法,super語句必須是構造方法的第一條語句

如果子類的構造方法中沒有顯示地調用父類構造方法,也沒有使用

this關鍵字調用重載的其它構造方法,則系統(tǒng)默認調用父類無參數(shù)的

構造方法

如果子類構造方法中既未顯式調用父類構造方法,而父類中又沒有無

參的構造方法,則編譯出錯

在子類中,不能通過父類的構造方法名調用父類的構造方法,只能通

過super關鍵字

調用父類構造方法舉例

1publicclassPerson{

2

3privateStringname;

4privateintage;

5privateDatebirthDate;

6

7publicPerson(Stringname,intage,Dated){

8=name;

9this.age=age;

10this.birthDate=d;

11}

12publicPerson(Stringname,intage){

13this(name,age,null);

14}

15publicPerson(Stringname,Dated){

16this(namez30,d);

17}

18publicPerson(Stringname){

19thisfname,30);

20)

21//……

22}

1publicclassStudentextendsPerson{

2privateStringschool;

3

4publicStudent(Stringname,intage,Strings){

5super(name,age);

6school=s;

7}

8publicStudent(Stringname,Strings){

9super(name);

10school=s;

11}

12publicStudent(Strings){//編譯出錯:nosuper。

super();

13school=s;

14)

15}

不能通過this關鍵字去初始化父類的屬性,只能通過super利用父類

的構造方法去初始化父類的屬性,不能通過父類構造方法名調用父類

構造方法

1publicclassStudentextendsPerson{

2privateStringschool;

3

4publicStudent(Stringname,intage,Strings){

5=name;this.age=age//error

6school=s;

7)

8publicStudent(Stringname,Strings){

9Pernson(name);//error

10school=s;

11)

12publicStudent(Strings){//編譯出錯:nosuper。

super();

13school=s;

14}

15}

多態(tài)(1)

多態(tài)--在Java中,子類的對象可以替代父類的對象使用

一個對象只能有一種確定的數(shù)據類型

一個引用類型變量可能指向(引用)多種不同類型的對象,父類的引用

變量可以指向子類實例,反之不成立

Personp=newStudent();

Objecto=newPerson();

o=newStudent();

Object[]o={newPerson。,newStudent()};

Students=newPerson()//error

多態(tài)(2)

一個引用類型變量如果聲明為父類的類型,但實際引用的是子類對象,

那么該變量就不能再訪問子類中添加的屬性和方法

Studentm=newStudent();

m.school="pku";〃合法

Persone=newStudent();

e.school="pku";〃非法

publicclassPerson{publicintgetlnfo(){}}

Objecto=newPerson();

o.getlnfo()〃非法

動態(tài)綁定/虛方法調用

正常的方法調用

Persone=newPerson();e.getlnfo();

Studente=newStudent();e.getlnfo();

動態(tài)綁定(多態(tài)情況下)

Persone=newStudent();

e.getlnfo();〃此時調用的是Student類中定義的getInfo方法

Person[]a={newPerson。,newStudent()}

Person[0].getlnfo()〃調用Person類中的方法

Person[l].getlnfo()〃調用Student類中的方法

編譯時類型和運行時類型

多態(tài)性應用舉例(1)

同類收集(homogenouscollections)

MyDate[]m=newMyDate[2];

m[0]=newMyDate(22,12,1964);

m[l]=newMyDate(22,1,1964);

異類收集(heterogeneouscollections)

Person[]p=newPerson[3];

p[0]=newStudent();

p[l]=newPerson();

p[2]=newGraduate();

多態(tài)性應用舉例(2)

方法聲明的參數(shù)類型為父類類型,可以使用子類的對象作為實參調用

該方法

publicclassTest{

publicvoidmethod(Persor)e){

//……

e.getlnfo();

)

publicstaticvoidmain(Stirngargs[]){

Testt=newTest();

Studentm=newStudent();

t.method(m);

}

}

instanceof操作符

publicclassPersonextendsObject{,?,}

publicclassStudentextendsPerson{,,,}

publicclassGraduateextendsPerson{,,,}

Personp=newStudent();

pinstanceofStudent;//true

因此從(pinstanceofStudent)不能推斷p被聲明為Student類型

publicvoidmethodl(Persone){

if(einstanceofStudent){

//處理Student類型及其子類類型對象

}elseif(einstanceofGraduate){

〃處理Graduate類型及其子類類型對象

}else{

〃處理Person類型對象

}

)

==操作符與equals方法

publicbooleanequals(Objectobj){

return(this==obj);

}

Object中的equals。方法利用==實現(xiàn)

Persona=newPerson();Personb=a;b.equals(a)==true;

基本類型使用==時比較值;引用類型使用==時僅比較地址;

如果想要比較引用類型不同實例的值(內容),必須重寫equals方法

舉例

publicbooleanequals(Objecto){

if(oinstanceofCircle){

returnradius==((Circle)o).radius;

)

else

returnfalse;

}

Circlea=newCircle(a);

Circleb=newCircle(ulv);

a!=b和a.equals(b)==true同時成立

toString。方法

任何Java類都是Object的子類

Object定義了一個toString()方法,它返回一個實例(對象)所屬類的名

稱@該對象在內存的地址;

System.out.println(person);

等價于System.out.println(person.toStringO);

同樣:"Person:"+person;

等價于"Person:"+person.toString();

抽象類

在有些情況下,可以暫時不實現(xiàn)類的某個成員方法,這時可以用

abstract關鍵字修飾類、以及相應的方法。

以abstract修飾的類稱為抽象類,以abstract修飾的方法稱為抽象方

不能對抽象類直接進行實例化。在子類中可以實現(xiàn)父類的抽象方法,

只有子類實現(xiàn)(覆蓋)了父類所有的抽象方法,那么才能創(chuàng)建子類的對

象,否則子類仍然是一個抽象類,也就是說也需要用關鍵字abstract

修飾

抽象類也可以不包含任何抽象方法,但抽象方法必須在抽象類(或接

口)中

最終類

final修飾的類是最終類,不能被繼承,如String,Math

final修飾的方法不能被重寫/覆蓋

有關final變量的說明:

final變量是常量

可僅設置一次final變量,但是賦值可獨立于聲明而發(fā)生,這稱之為

空final變量。即,如果final變量不在其聲明中被初始化;其初始

化被延遲:

空final實例變量必須在構造方法中被賦值。

空final局部變量可在方法的主體內隨時被設置。

它僅可設置一次。

接口

聲明接口

[public]interface(接口名〉[extends(父接口>]{

[public][static][final]〈數(shù)據類型>(變量>=<值>;

[public][abstract](數(shù)據類型><方法名>([<參數(shù)>]);

}

聲明實現(xiàn)接口的類implements

Java類只支持單繼承,但是可以實現(xiàn)多個接口

接口的特點:

接口以及接口中成員的訪問權限都是public

接口中的成員方法都是公有的、抽象的

接口中所有的方法都必須被實現(xiàn)接口的類覆蓋

接口中的成員變量都是常量,聲明時必須賦值

接口不能被實例化

Java異常

錯誤(Error):JVM系統(tǒng)內部錯誤、資源耗盡等嚴重情況

違例(Exception):其它因編程錯誤或偶然的外在因素導致的一般性問

題,例如:對負數(shù)開平方根;訪問null;試圖讀取不存在的文件;網絡連

接中斷;

Java異常舉例

publicclassSamplel{

publicstaticvoidmain(String[]args){

Stringfriends[]={"lisa",''bily","kessy"};

for(inti=0;i<5;i++)

System.out.println(friends[i]);

System.out.println("\nthisistheend");

}

}

Java異常類層次

常見異常

RuntimeException

錯誤的類型轉換

數(shù)組下標越界

空指針訪問

lOExeption

從一個不存在的文件中讀取數(shù)據

越過文件結尾繼續(xù)讀取

連接一個不存在的URL

異常處理機制

Java程序的執(zhí)行過程中如出現(xiàn)異常,會自動生成一個異常類對象,該

異常對象將被提交給Java運行時系統(tǒng),這個過程稱為拋出(thro

溫馨提示

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

最新文檔

評論

0/150

提交評論