Java复习题Word格式.docx

上传人:b****6 文档编号:8562858 上传时间:2023-05-11 格式:DOCX 页数:28 大小:22.57KB
下载 相关 举报
Java复习题Word格式.docx_第1页
第1页 / 共28页
Java复习题Word格式.docx_第2页
第2页 / 共28页
Java复习题Word格式.docx_第3页
第3页 / 共28页
Java复习题Word格式.docx_第4页
第4页 / 共28页
Java复习题Word格式.docx_第5页
第5页 / 共28页
Java复习题Word格式.docx_第6页
第6页 / 共28页
Java复习题Word格式.docx_第7页
第7页 / 共28页
Java复习题Word格式.docx_第8页
第8页 / 共28页
Java复习题Word格式.docx_第9页
第9页 / 共28页
Java复习题Word格式.docx_第10页
第10页 / 共28页
Java复习题Word格式.docx_第11页
第11页 / 共28页
Java复习题Word格式.docx_第12页
第12页 / 共28页
Java复习题Word格式.docx_第13页
第13页 / 共28页
Java复习题Word格式.docx_第14页
第14页 / 共28页
Java复习题Word格式.docx_第15页
第15页 / 共28页
Java复习题Word格式.docx_第16页
第16页 / 共28页
Java复习题Word格式.docx_第17页
第17页 / 共28页
Java复习题Word格式.docx_第18页
第18页 / 共28页
Java复习题Word格式.docx_第19页
第19页 / 共28页
Java复习题Word格式.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Java复习题Word格式.docx

《Java复习题Word格式.docx》由会员分享,可在线阅读,更多相关《Java复习题Word格式.docx(28页珍藏版)》请在冰点文库上搜索。

Java复习题Word格式.docx

s1.age=18;

System.out.println(s1.getInfo());

}

}

classStudent{

staticStringclassID;

//班级编号

Stringname;

//学生姓名

intage;

//学生年龄

StringgetInfo(){

returnclassID+"

+name+"

+age;

3、publicclassTest{

s1.name="

Students2=newStudent();

s2.classID="

s2.name="

s2.age=20;

System.out.println(s1.getInfo());

System.out.println(s2.getInfo());

090932/Tom/18

4、publicclassTestStudent{

Students1=newStudent(“Tom”,18);

intage=15;

Student(Stringn,inta){//构造方法

name=n;

age=a;

returnname+"

Tom/18

5、Java语言中,允许一个类可以有多个构造方法

classBox{

doublelength;

doublewidth;

doubleheight;

Box(doublel,doublew,doubleh){

length=l;

 width=w;

 height=h;

}

Box(doublex){

length=x;

width=x;

height=x;

doublegetVol(){

returnlength*width*height;

6、publicclassTestBook{

Bookbook1=newBook("

Java"

25){

StringgetInfo(){return"

title="

+title+"

price="

+price;

StringgetTitle(){returntitle;

};

System.out.println(book1.getInfo());

Bookbook2=newBook("

25);

System.out.println(book2.getInfo());

classBook{

Stringtitle;

intprice;

Book(Stringt,intp){title=t;

price=p;

StringgetInfo(){returntitle+"

7、局部内部类

publicclassOuter{

privateintx=1;

publicvoidf(finalinty){

intz=2;

classInner{

publicInner(){//定义局部内部类

System.out.println(“x=”+x+”y=”+y);

//可以访问x与y,不能访问z

8、publicclassTest{

System.out.println(newStudent().id);

classPerson{

Stringname,sex;

classStudentextendsPerson{

Stringid;

说明:

若子类和父类都没有定义构造方法,则创建子类对象时,系统将调用父类的无参数构造方法。

9、publicclassTest{

(错的)

Student(){}

Student(Stringid){

this.id=id;

若子类有含参数构造方法,但没有无参数构造方法,则创建子类对象时,必须使用含参数构造方法。

10、publicclassTest{

System.out.println(newStudent(“0801”).id);

Person(){name="

张三"

sex="

男"

Student(Stringid){this.id=id;

若子类有构造方法,则创建子类对象时,系统将首先隐含调用父类的无参数构造方法,然后调用子类自己的构造方法。

11、publicclassTest{

System.out.println(newStudent(…).id);

Person(){}

Person(Stringn,Strings){

sex=s;

Student(Stringn,Strings,Stringid){

super(n,s);

this.id=id;

若子类有构造方法,且在其中的首句使用super显式调用父类的构造方法,则创建子类对象时,系统将不再隐含调用父类的无参数构造方法。

12、子类对象初始化顺序

通过new关键字为子类对象分配内存空间

父类静态成员变量显式初始化

子类静态成员变量显式初始化

父类实例成员变量显式初始化

父类构造方法

子类实例成员变量显式初始化

子类构造方法

得到子类对象

classAnimal{

publicAnimal(){System.out.println("

Animal"

);

classCatextendsAnimal{

publicCat(){System.out.println("

Cat"

classStore{

publicStore(){System.out.println("

Store"

publicclassPetstoreextendsStore{

privateCatcat=newCat();

publicPetstore(){System.out.println("

Petstore"

newPetstore();

Store

Animal

Cat

Petstore

13、父类的引用变量指向子类对象

(1)

publicclassAnimal{

voidrun(){

System.out.println("

Ananimalisrunning."

Animala=newDog();

a.run();

classDogextendsAnimal{

Adogisrunning."

形象理解:

对a来说,它把Dog对象当作了Animal对象(即子类对象可当作父类对象使用)

14、父类的引用变量指向子类对象

(2)

voidrun(){System.out.println("

animalrun."

voidact(){run();

a.act();

protectedvoidrun(){

dogrun."

15、classBook{

Stringtitle,author;

voidgetInfo(){

System.out.println(title+“,”+author);

Stringid,name;

System.out.println(id+“,”+name);

publicclassTest1{

publicstaticvoidmain(String[]args){

一个程序中出现了同名方法

16、classBook{

classEBookextendsBook{//电子书

Stringurl;

//网址

super.getInfo();

System.out.println(url);

publicclassTest2{

publicstaticvoidmain(String[]args){…}

继承引发的多态性——方法覆盖

17、classMyMath{

staticintsum(intx,inty){

returnx+y;

staticintsum(intx,inty,intz){

returnx+y+z;

staticintsum(floatx,floaty){(错的)

staticfloatsum(intx,inty){

publicclassTest3{

封装引发的多态性——方法重载

18、classBook{

Book(Stringt,Stringa){

title=t;

author=a;

classEBookextendsBook{

EBook(Stringt,Stringa,Stringu){

super(t,a);

url=u;

publicclassTest4{

publicstaticvoidmain(String[]args){

Bookbook=newEBook(“Java编程”,“张三”,“http:

”);

System.out.println(book.url);

对象引用引发的多态性,子类对象可当作父类对象使用

19、窘境:

构造方法中引用动态绑定方法1

classA{

intsum(intx,inty){returnx+y;

A(){System.out.println(this.sum(4,6));

classBextendsA{

publicB(){}

protectedintsum(intx,inty){

returnx*10+y*10;

publicclassTest{

newB();

}//不要在父类构造方法中调用被子类重写的方法

20、窘境:

构造方法中引用动态绑定方法2

publicclassTest03{

publicstaticvoidmain(String[]args){newB(5);

voidshow(){System.out.println("

A.show()"

publicA(){this.show();

privateintx;

publicB(intx){

System.out.println(this.x);

this.x=x;

{x=2;

B.show(),x="

+x);

输出:

B.show(),x=0

2

5

21、算术运算异常的捕获和处理

importjava.io.*;

publicclassUseException{

 publicstaticvoidmain(String[]args){

  try{

   inta,b;

   a=Integer.parseInt(args[0]);

   b=Integer.parseInt(args[1]);

   System.out.println(a+"

/"

+b+"

="

+(a/b));

  }catch(ArithmeticExceptione){

   System.out.println("

捕获了一个异常,除数不能为0!

"

  }

 }

22、异常处理例

classTestException{//该类的方法中会抛出异常

privateinti;

privateint[]array={1,2,3,4,5};

//定义一个含5个元素的数组

voidm1(){//该方法中会出现数组下标越界异常,且无处理

for(intj=0;

j<

10;

j++){

i=(int)(Math.random()*10);

下标为"

+i+"

的数组元素是"

+array[i]);

}//endofm1()

voidm2(){//该方法中会出现数组下标越界异常,但有异常处理

try{

catch(ArrayIndexOutOfBoundsExceptione){

在m2方法中出现数组下标越界"

}//endofm2()

voidm3()throwsUserException{//抛出自定义异常,但未处理

if(i>

=3)thrownewUserException("

下标偏大"

}//endofm3()

}//endofclassTestException

classUserExceptionextendsException{//自定义异常类

publicUserException(Stringmsg){

super(msg);

23、一维数组静态初始化

数组成员是引用类型的也可静态初始化

Point[]pa={newPoint(1,4),newPoint(3,9)};

classPoint{

intx,y;

Point(inta,intb){

x=a;

y=b;

}

24、一维数组动态初始化

数组定义与为数组分配空间和赋值分开进行

inta[]=newint[3];

 a[0]=1;

a[1]=5;

同样,引用类型的数组成员也可动态初始化

Point[]pa=newPoint[3];

pa[0]=newPoint(1,4);

 

pa[1]=newPoint(3,9);

classPoint{

 y=b;

}

课堂习题

1、对于类Book,下列选项中,正确的构造函数定义是(C)

A.book(titleString,authorString)

B.book(title,author)

C.Book(Stringtitle,Stringauthor)

D.Book(title,author)

2、以下选项中,(B)是对父类方法StringgetInfo()的覆盖

A.intgetInfo()

B.StringgetInfo()

C.StringgetInfo(Strings)

D.StringgetInfo(inti)

3、关于方法的重载,以下叙述错误的是:

(C)

A.方法的重载是通过在一个类中定义同名方法实现的

B.重载根据不同的形式参数列表来区分同名的不同方法

C.方法的重载是继承的一种表现

D.重载方法的方法名相同,是因为它们的最终功能和目标相同

4、下列选项中,(A)不是方法publicvoidprint()的重载方法

A.publicStringprint(Stringx)

B.publicvoidprint(charx)

C.publicvoidprint(intx)

D.publicvoidprint(Stringx)

5、publicclassTest{//定义主类Test

Dogdog=newDog();

dog.run();

Runnerhorse=newHorse();

Test.run(horse);

voidrun(Runnerr){r.run();

interfaceRunner{voidrun();

classDogimplements

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2