面向对象语言程序设计课程第五章课件

PPT
  • 阅读 52 次
  • 下载 0 次
  • 页数 66 页
  • 大小 343.035 KB
  • 2022-11-24 上传
  • 收藏
  • 违规举报
  • © 版权认领
下载文档30.00 元 加入VIP免费下载
此文档由【小橙橙】提供上传,收益归文档提供者,本网站只提供存储服务。若此文档侵犯了您的版权,欢迎进行违规举报版权认领
面向对象语言程序设计课程第五章课件
可在后台配置第一页与第二页中间广告代码
面向对象语言程序设计课程第五章课件
可在后台配置第二页与第三页中间广告代码
面向对象语言程序设计课程第五章课件
可在后台配置第三页与第四页中间广告代码
面向对象语言程序设计课程第五章课件
面向对象语言程序设计课程第五章课件
还剩10页未读,继续阅读
【这是免费文档,您可以免费阅读】
/ 66
  • 收藏
  • 违规举报
  • © 版权认领
下载文档30.00 元 加入VIP免费下载
文本内容

【文档说明】面向对象语言程序设计课程第五章课件.ppt,共(66)页,343.035 KB,由小橙橙上传

转载请保留链接:https://www.ichengzhen.cn/view-44887.html

以下为本文档部分文字说明:

面向对象JAVA语言程序设计信息科学与技术学院网络工程系周文峰Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.2第五

章常用系统类主要任务:介绍Java常用的系统类,包括字符串类、输入输出流类、JavaApplet类、数学函数类、日期类、随机数类以及向量类等。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyri

ght2004-2011AsposePtyLtd.3主要内容•字符串类String类StringBuffer类和StringBuilder类Evaluationonly.edwithAspose.Slide

sfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.45.1字符串类字符与字符串的区别•字符是指用单引号括起来单个字符,如’a’、’b’、’A’等。这里的字符不是指占1个字节

的ASCII字符,而是指占2个字节的Unicode字符。因为Unicode被设计用来处理现在世界上所有书面语言中的字符,所以一个汉字也是被当作一个字符来处理的。对于单个字符,除了有char这个原始类型以外,Java平台中的java.lang包

还专门提供了一个Character类来进行储存和操作。在计算机科学领域中,Unicode(统一码、万国码、单一码、标准万国码)是业界的一种标准。Evaluationonly.edwithAspose.Slidesfor.NET3.5Clie

ntProfile5.2Copyright2004-2011AsposePtyLtd.55.1字符串类•字符串是程序设计中经常用到的数据结构,很多编程语言将字符串定义为基本数据类型。但在Java语言中,字符串被定义为类,必须先生成类的实例对象然后才能使用。•常用有

两种类型的字符串:一种是创建以后不需要改变的,称为字符串常量,在Java中,String类用于存储和处理字符串常量;另外一种字符串是创建以后,需要对其进行改变的,称为字符串变量,在Java中,StringBuffer类用于存储和操作字符串变量。

Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.6字符串与字符串类•字符串是一个完

整的字符序列,可以包含字母、数字和其它符号。•在Java中,用双引号括起来的字符串是字符串常量,又称为无名字符串对象,由Java自动创建。•字符串常量可以赋给任何一个String对象引用,这样处理从表面上看起来和其它编程语言没有大的差别,照顾了程序员的习惯,但实际上存在着较大的差异。无论何

时,Java中的字符串都是以对象的面孔出现的,在运行时要为它分配内存空间,创建对象引用。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-

2011AsposePtyLtd.7Java将字符串定义为类有哪些好处呢?•首先,在任何系统平台上都能保证字符串本身以及对字符串的操作是一致的。对于网络环境,这一点是至关重要的。•其次,String和StringBuffer经过了精心设计,其功能是可以预见的。为此,二

者都被说明为最终类,不能派生子类,以防用户修改其功能。•最后,String和StringBuffer类在运行时要经历严格的边界条件检验,它们可以自动捕获异常,提高了程序的健壮性。Evaluationonly.edwithAspo

se.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.85.1字符串类•字符串对象•字符串长度及查找•字符串的连接及子串•字符串常量与比较•String对象的不变性•命令行参数Eva

luationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.95.1.1字符串类•Java语言提供了三个字符串类:St

ring类、StringBuffer类和StringBuilder类。String类是不变字符串,StringBuffer和StringBuilder是可变字符串,这3种字符串都是16位(2个字节)的Unicode字符序列,并且这3个类都被声明为final,因此

不能被继承。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.105.1.1字符串类•Java语言用String类表示字符串。一般使用St

ring类的构造方法创建一个字符串对象。String类有13个重载的构造方法,可以生成一个空字符串,也可以由字符或字节数组生成字符串。•String类常用的构造方法如下:•publicString()//空字符序列构造字符串对象•publicString(char[]value)//字

符数组构造字符串对象Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.115.1.1字符串类•public

String(char[]value,intoffset,intcount)//字符数组的子数组•publicString(byte[]bytes)//byte数组构造字符串对象•publicString(byte[]bytes,intoffset,intlength)•publicStri

ng(Stringoriginal)//构造一个original的副本•publicString(StringBufferbuffer)•publicString(StringBuilderbuffer)Evaluationonly.edwithAspose.Slidesfor.N

ET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.125.1.1字符串类例如:charchars1[]={'A','B','C'};charchars2[]={'中','国','π','ぁ'};Strings

1=newString(chars1);Strings2=newString(chars2,0,4);Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5

.2Copyright2004-2011AsposePtyLtd.135.1.1字符串类•在Java中,还有一种特殊的创建String对象的方法,这种方法是直接利用字符串常量创建字符串对象,例如:Strings=“ThisisaJavastring.”;Evalua

tiononly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.14•常量串存储在串池中。•编译时,每当碰到一个常量串,JVM就会在串池中搜索,如

果此串存在,则直接使用这个现成的串;如果没有,就在串池中创建一个新串。Stringx=“abcd”;Stringy=“abcd”;Stringz=“1234”;xyz串池对象:”abcd”对象:”1234”•x,y,z并不是真正的对象,只是指向对象存储位置的引用。(C语言中称为指针)栈内存搜索没

找到找到nullnullnull字符串的内存分配(字符串常量)Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004

-2011AsposePtyLtd.15•字符串变量与字符串常量不存储在同样的位置。字符串的内存分配(创建串对象)Stringx=newString(“abcd”);x对象:”abcd”对象:”abcd”其它常量对象串池null搜索

找到复制栈内存堆内存Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.165.1.2字符串长度及查找•创建Stri

ng类对象外,更常用的是调用String类的方法,该类定义了许多方法。publicintlength()•返回字符串的长度,即字符串包含的字符个数。注意对含有汉字和其他语言符号的字符串,计算长度时,一个符号作为一个字符计数。•例如获取字符串“MingRiSoft”长度的代码如下:Strin

gnameStr="MingRiSoft";inti=nameStr.length();//获得字符串的长度为10Evaluationonly.edwithAspose.Slidesfor.NET3.5Cli

entProfile5.2Copyright2004-2011AsposePtyLtd.175.1.2字符串长度及查找publicintindexOf(intch)•查找字符ch第一次出现的位置。如果查找不成功则返回-1。publicintindexOf(intch,intfromIndex)•查

找字符ch从fromIndex开始第一次出现的位置(在原字符串中的下标)。Stringstr="mingrikeji";inti=str.indexOf('i');System.out.println("字符i

第一次出现在索引:"+i);//索引值是1Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011A

sposePtyLtd.185.1.2字符串长度及查找publicintindexOf(Stringstr)•查找字符串str第一次出现的位置。如果查找不成功则返回-1。publicintindexOf(Stringstr,in

tfromIndex)•查找字符串str从fromIndex开始第一次出现的位置(在原字符串中的下标)。Stringstr="mingrikeji";i=str.IndexOf("ri");System.out.println(“字符串“ri”第一次出现在索引:"+i);//索引值是4Evalua

tiononly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.195.1.2字符串长度及查找publicintlastIndexOf(intch)•查找字符ch最后一次

出现的位置。publicintlastIndexOf(intch,intendIndex)•查找字符ch到endIndex为止最后一次出现的位置。Stringstr="mingrikeji";i=str.lastIndexOf('i');Syste

m.out.println("字符i最后一次出现在索引:"+i);//索引值是9i=str.lastIndexOf('i',4);System.out.println(“到第4个字符为止,字符i最后一次出现在索引:"+i);//索引值是1Evaluationonly.edwithAspose.

Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.205.1.2字符串长度及查找publicintlastIndexOf(Stringstr)•查找字符串str最后一次出现的位置。publicintlastIn

dexOf(Stringstr,intendIndex)•查找字符串str到endIndex为止最后一次出现的位置(在原字符串中的下标)。Stringstr="mingrikeji";i=str.lastIndexOf(„ri');System.out.println(“字符串ri最

后一次出现在索引:"+i);//索引值是4Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.215.1.3字符串的连接与子串pub

licStringconcat(Stringstr)•调用字符串与参数字符串连接起来,产生一个新的字符串。publicStringsubstring(intbeginIndex,intendIndex)•从字符串的下

标beginIndex开始到endIndex结束产生一个子字符串。(不包含endIndex处的字符)publicStringsubstring(intbeginIndex)•从字符串的下标beginIndex开始到结束产生一个子字符串。Eval

uationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.225.1.3字符串的连接与子串pub

liccharcharAt(intindex)•返回字符串中指定位置的字符,index表示位置,范围为0~s.length()-1。publicStringreplace(charoldChar,charnewChar)•将调用

字符串中的所有oldChar字符改变为newChar字符,返回一个新的字符串。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011As

posePtyLtd.235.1.3字符串的连接与子串publicStringtoUpperCase()publicStringtoLowerCase()•将字符串转换成大写或小写字母。publicbooleanst

artsWith(Stringprefix)publicbooleanendsWith(Stringsuffix)•返回字符串是否以某个字符串开始或结尾。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright

2004-2011AsposePtyLtd.245.1.3字符串的连接与子串publicvoidgetChars(intsrcBegin,intsrcEnd,char[]dst,intdstBegin)•将字符串中从起始位置(srcBegin)到结束位置

(srcEnd)之间的字符复制到字符数组dst中从起始位置为dstBegin。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011

AsposePtyLtd.255.1.3字符串的连接与子串下面的程序说明了String对象的使用。Strings=newString("ThisisaJavastring.");System.out.println(s.length()

);//22System.out.println(s.charAt(10));//JSystem.out.println(s.lastIndexOf('a'));//13System.out.println(s.lastIn

dexOf('a',10));//8System.out.println(s.indexOf("is"));//2Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfi

le5.2Copyright2004-2011AsposePtyLtd.265.1.3字符串的连接与子串Strings=newString("ThisisaJavastring.");System.out.println(s.

lastIndexOf("is"));//5System.out.println(s.indexOf("my"));//-1char[]subs=newchar[4];s.getChars(10,14,subs,0);System.out.println

(subs);//JavaEvaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.275.1.3字符串的连接与子串Str

ings=newString("ThisisaJavastring.");Strings1="It'sinteresting.";s1=s.concat(s1);System.out.println(s.t

oUpperCase());//THISISAJAVASTRING.System.out.println(s1.toLowerCase());//thisisajavastring.it'sinteresting.

System.out.println(s.endsWith("in"));//falseEvaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011Asp

osePtyLtd.285.1.4字符串解析•String类提供了一个split()方法,它用来实现将一个字符串分解成子字符串或令牌(token)。该方法使用正则表达式指定分隔符publicString[]split(Stringregex,intli

mit)publicString[]split(Stringregex)Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.29publicclas

sSplitDemo{publicstaticvoidmain(Stringargs[]){Stringss="11,22,33";System.out.println(ss);String[]str=ss.split(",");System.out.println(st

r[0]);System.out.println(str[1]);System.out.println(str[2]);}}Evaluationonly.edwithAspose.Slidesfor.NET

3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.305.1.5字符串常量与字符串比较•在Java程序中,凡是由双引号定界的字符序列都是字符串常量(literalstring),如”abc”。字符串常量存放在字符串常

量池(poolofliteralstring)中。字符串常量也是String类的对象,可以直接使用,例如:System.out.println("Hello".length());//结果输出5Evaluationonly.edwi

thAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.311.字符串相等的比较你可能想到用“==”号来比较,如下代码所示。Strings1=newString("Hello

");Strings2=newString("Hello");System.out.println(s1==s2);5.1.5字符串常量与字符串比较Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.

2Copyright2004-2011AsposePtyLtd.32再看下面一段代码:Strings1=“Hello”;Strings2=“Hello”;System.out.println(s1==s2);Evaluationonly.edwithAspose.Slidesfo

r.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.33Stringstr1="abc";Stringstr2="abc";System.out.println(str1==str2);/

/true字符串的比较str1str2串池对象:”abc”栈内存搜索找到nullnull没找到Str1与str2存储的是同一块内存地址Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004

-2011AsposePtyLtd.34•Stringstr1=newString("abc");•Stringstr2=newString("abc");•System.out.println(str1==st

r2);//false字符串的比较str2对象:”abc”对象:”abc”其它常量对象串池null搜索找到复制栈内存堆内存str1对象:”abc”nullstr1与str2存储的是堆内存中的不同地址Eval

uationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.35如果要比较两个对象的内容是否相等,

可以使用String类的equals()方法。对于上面两种情况,表达式:s1.equals(s2);的结果都为true,因为s1、s2的内容都相等。例如比较字符串“A”和字符串“a”是否相等:注意:equals()方法在比较两个字符串时区分字母大小写。

Stringstr="A";booleanb=str.equals(“a”);//比较结果b为falseEvaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyrig

ht2004-2011AsposePtyLtd.362.字符串大小的比较要比较字符串的大小,可以使用String类的compareTo()方法,该方法的格式为:publicintcompareTo(StringanotherString)E

valuationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.37字符串比较使用字符的Unicode码值进行比较。若当前字符串小于参数字符串时,方法返回值小于0,若当前字符串

大于参数字符串时,方法返回值大于0,若当前字符串等于参数字符串时,方法返回值等于0。例如,表达式:“abc”.compareTo(“abd”)//返回值为-1。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright

2004-2011AsposePtyLtd.38对于字符串相等和大小比较还可以忽略大小写,可以使用下面两个方法:publicbooleanequalsIgnoreCase(StringanotherStrin

g)publicintcompareToIgnoreCase(StringanotherString)注意,字符串不能使用>、>=、<、<=进行比较,可以使用!=比较。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2C

opyright2004-2011AsposePtyLtd.39比较字符串startsWith()和endsWith()方法这两方法分别用于判断字符串是否以指定的字符串开始或结束,它们的定义如下:参数prefix和suffix为欲比较的字符串对象,该方法的返回值为boolean

型,例:分别判断字符串“ABCDE”是否以字符串“a”开始以及以字符串“DE”结束:publicbooleanstartsWith(Stringprefix)publicbooleanendsWith(String

suffix)Stringstr="ABCDE";booleanbs=str.startsWith(“a”);//比较结果bs为falsebooleanbe=str.endsWith("DE");//比较结果be为trueEvaluationonl

y.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.40startsWith()还有一个重载方法,可以指定索引位置开始是否为指定的字符串,重载方法定义如下:方法的第二个参数toffset是比较

字符串的索引位置。例如下面的代码判断字符串“ABCDE”从索引位置2开始是否为字符串“CD”:上面代码的判断结果为true,即字符串“ABCDE”从索引位置2开始是字符串“CD”。publicbooleansta

rtsWith(Stringprefix,inttoffset)Stringstr="ABCDE";booleanb=str.startsWith("CD",2);Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientPr

ofile5.2Copyright2004-2011AsposePtyLtd.415.1.6String对象的不变性•在Java程序中,一旦创建一个String对象,就不能对其内容进行改变,因此说Java的String对象是不可变的字符串。•请看下面的例子

。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.42publicclassCh

angeString{publicstaticvoidmain(Stringargs[]){Strings=newString("Hello,world");s.replace('o','A');s=s.substring(0,6).concat("Java");s.toLo

werCase();System.out.println(s);}}Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyL

td.43publicclassChangeString{publicstaticvoidmain(Stringargs[]){Strings=newString("Hello,world");Sys

tem.out.println(s.replace('o','A'));s=s.substring(0,6).concat("Java");System.out.println(s);System.out.println(s.toLowerCase());}}Evaluationonly.

edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.445.1.7命令行参数main()方法的声明格式为:publicstaticvoidmain(Stringarg

s[]){}其中参数Stringargs[]为字符串数组,该参数是通过命令行传递给main()方法的。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLt

d.45例:从命令行输入三个整数,输出最大值和最小值。publicclassThreeInteger{publicstaticvoidmain(Stringargs[]){intmax,min,a,b,c;a=Integer.parseInt(args[0])

;b=Integer.parseInt(args[1]);c=Integer.parseInt(args[2]);max=Math.max(Math.max(a,b),c);min=Math.min(

Math.min(a,b),c);System.out.println("max="+max);System.out.println("min="+min);}}Evaluationonly.edwithAspose.S

lidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.46运行该程序需要在命令行给出参数,如下所示:D:\>javaThreeInteger“23”“-234”“100”程序运行结果为:max=1

00min=-234Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.47注意:命令行参数传递

的是字符串,若将其作为数值处理,需要进行转换。若字符串中含有非数值字符,则抛出NumberFormatException运行时异常。根据传递的参数个数确定数组args的长度,如果给出的参数少于引用的元素,则抛出ArrayIndexOutOfBou

ndsException运行时异常。如D:\>javaThreeInteger“23”“-234”Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyL

td.485.2StringBuilder和StringBuffer类•创建StringBuffer对象•StringBuffer对象的访问和修改•StringBuilder类Evaluationonly.edwithAspose.S

lidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.495.2.1创建StringBuffer对象•字符串缓冲区类StringBuffe

r类是字符串的另一种处理办法。•StringBuffer类常用的构造方法有下面三个:•publicStringBuffer()–创建一个没有字符的字符串缓冲区,初始容量为16个字符。此时length(

)方法的值为0,而capacity()方法的值为16。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011A

sposePtyLtd.50•publicStringBuffer(intcapacity)–创建一个没有字符的字符串缓冲区,capacity为指定的初始容量。•publicStringBuffer(String

str)–利用一个已存在的字符串对象str创建一个字符串缓冲区对象,另外再分配16个字符的缓冲区。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011Aspo

sePtyLtd.51•StringBuffer类除定义了length()、charAt()、getChars()、indexOf()等方法外,还提供了下列常用方法:•publicintcapacity()–返回当前的字符串缓冲区的容量。•publicvoidsetCharAt

(intindex,charch)–用ch修改指定位置的字符。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.52•p

ublicStringBufferdeleteCharAt(intindex)–删除指定位置的字符。•publicStringBufferappend(Stringstr)–向当前的字符串的末尾添加一个字符串。该方法有一系列的重载方法,参数可以是boolean、char、int、long、f

loat、double、char[]等任何数据类型。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.53•publicS

tringBufferinsert(intoffset,Stringstr)–从当前字符串的指定位置插入一个字符串。这个方法个也有多个重载的方法,参数可以是boolean、char、int、long、float

、double、char[]等类型。•publicStringBufferdelete(intstart,intend)–删除从start开始到end(不包括end)之间的字符。Evaluationonly

.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.54•publicStringBufferreplace(intstart,intend,Stri

ngstr)–用字符串str替换从start开始到end(不包括end)之间的字符。•publicStringBufferreverse()–将字符串的所有字符反转。•publicStringBuffersubstring(intstart)–返回

从start开始到字符串末尾的子字符串Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.55•publicStringBuffersubstring(intstart

,intend)–返回从start开始到end(不包括end)之间的子字符串。例:下面程序演示了StringBuffer对象及其方法的使用。StringBuffersb=newStringBuffer("Hello");Syst

em.out.println(sb);//HelloSystem.out.println(sb.length());//5System.out.println(sb.capacity());//21Evalua

tiononly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.56sb.append("Java");System.out.println(sb);//HelloJavaSys

tem.out.println(sb.insert(5,“,”));//Hello,JavaSystem.out.println(sb.replace(6,10,"World!"));//Hello,World!System.ou

t.println(sb.reverse());//!dlroW,olleHEvaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.575.2.3StringBuilder

类•StringBuilder类与StringBuffer类的区别是–StringBuffer类的实例是线程安全的,–StringBuilder类的实例不是线程安全的。–如果需要线程同步,推荐使用StringBuffer类。Evaluationonly.

edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.58习题1.下列关于构造方法描述错误的是。A.Java语言规定构造方法名与类名必须

相同;B.Java语言规定构造方法没有返回值,但不用void声明;C.Java语言规定构造方法不可以重载;D.Java语言规定构造方法只能通过new自动调用。2.已知语句inta[]={1,2,3};下列描述错误的是。A.定义了一个名为a的一维

数组B.数组a有3个元素C.数组a中每个元素都是整型D.数组元素a[3]的值为3Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011A

sposePtyLtd.59习题3.下列选项中,用于在定义子类时声明父类名的关键字是。A.interfaceB.packageC.extendsD.class4.设i、j、k为类x中定义的int型变量名,下列类x的构造函数中不正确的是。A.x(intm){...

}B.voidx(intm){...}C.x(intm,intn){...}D.x(inth,intm,intn){...}5.Java和c++都是面向对象的程序设计语言。()6.同一个类中定义多个参数列表不同的同名

方法,叫做方法的重载。()Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.60习题7.一个程序里可以有多个父类,也可以有多个主类。()8.

一个子类可以重新定义从父类那里继承来的同名方法,而且允许它们有不同的返回值。()9.Javaapplication中的主类需包含main方法,main方法的返回类型是。A.intB.floatC.doubleD.void10.在类的定义中可以有两个同名方法,这种现象称

为方法。A.封装B.继承C.覆盖D.重载Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.6111.下列程序的输出结果是。classF{publicF(){Sys

tem.out.print("F()iscalled!");}}classSextendsF{publicS(){System.out.print("S()iscalled!");}}publicclassEx

{publicstaticvoidmain(Stringargs[]){Ssa=newS();}}A)F()iscalled!B)S()iscalled!C)F()iscalled!S()iscalled!D)S()iscalled!F()iscalled

!Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.62习题12.现有类说明如下:classA{intx=10;intGetA(){ret

urnx;}}classBextendsA{intx=100;intGetB(){returnx;}}问题:1)类B是否能继承类A的属性x?2)若b是类B的对象,则b.GetB()的返回值是什么?3)若b是类B的对象,则b.GetA()的返回值是什么?4)类A和类B都定

义了x属性,这种现象称为什么?Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.63习题13.以下Java语句中,

Stringstr=“123456789”;str=str.subString(1,3);执行后str中的值为()。A.“23”B.“123”C.“12”D.“234”14.Java语言中,Stringstr=”123456789”,Syst

em.out.println(str.indexOf(“5”)),输出结果为()。a)6b)5c)4d)-115.在java中,已定义两个接口B和C,要定义一个实现这两个接口的类,以下语句正确的是()。a)interfaceA

extendsB,Cb)interfaceAimplementsB,Cc)classAimplementsB,Cd)classAimplementsB,implementsCEvaluationonly.edwit

hAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.6416.给定JAVA代码,如下:ClassParent{publicvoidcount(){System.out.pri

ntln(10%3);}}publicclassTestextendsParent{publicvoidcount(){System.out.println(10/3);}publicstaticvoidmain

(Stringargs[]){Parentp=newTest();p.count();}}运行编译后,输出结果是()。A.1B.1.0C.3D.3.3333333333333335Evaluationonly.edwithAspos

e.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposePtyLtd.65习题17.在java中,下列()类不能派生出子类。a)publicclassMyClass{}b)cla

ssMyClass{}c)abstractclassMyClass{}d)finalclassMyClass{}18.在java中,以下()命令能够将java源文件编译为类文件a)Javab)javawc)javacd)jar19.研究下面的Java代码,当x=2时,运行结果是()。swi

tch(x){case1:System.out.println(1);case2:case3:System.out.println(3);Case4:System.out.println(4);}a)没

有输出任何结果b)输出结果为3c)输出结果是3和4d)输出结果是l、3和4Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2

004-2011AsposePtyLtd.66习题20.试编写java代码实现一个计算器类Computer,其中包括:(1)用counterValue来保存计数器的当前值;(2)方法increment

()计数器加1;(3)方法decrement()计数器减1;(4)方法reset()计数器清零。Evaluationonly.edwithAspose.Slidesfor.NET3.5ClientProfile5.2Copyright2004-2011AsposeP

tyLtd.

小橙橙
小橙橙
文档分享,欢迎浏览!
  • 文档 25747
  • 被下载 7
  • 被收藏 0
相关资源
广告代码123
若发现您的权益受到侵害,请立即联系客服,我们会尽快为您处理。侵权客服QQ:395972555 (支持时间:9:00-21:00) 公众号
Powered by 太赞文库
×
确认删除?