IBM公司Java培训PPT.ppt_第1页
IBM公司Java培训PPT.ppt_第2页
IBM公司Java培训PPT.ppt_第3页
IBM公司Java培训PPT.ppt_第4页
IBM公司Java培训PPT.ppt_第5页
已阅读5页,还剩302页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一章Java语言概述,华中科技大学IBM技术中心2008,华中科技大学IBM技术中心,HUST,华中科技大学IBM技术中心,HUSTpublicintx,y;/constructorpublicSpot()x=-1;y=-1;size=1;/methodsforaccesstothesizeinstancevariablepublicvoidsetSize(intnewSize)if(newSize=0)size=newSize;publicintgetSize()returnsize;,华中科技大学IBM技术中心,HUST.spot=newSpot();,华中科技大学IBM技术中心,HUSTg2d.fillRect(0,0,getWidth()-1,getHeight()-1);,华中科技大学IBM技术中心,HUSTspot.setSize(RADIUS);spot.x=event.getX();spot.y=event.getY();repaint();publicvoidmouseClicked(MouseEventevent)publicvoidmouseReleased(MouseEventevent)publicvoidmouseEntered(MouseEventevent)publicvoidmouseExited(MouseEventevent),欢迎提问,Question!,第三章Java语言基础,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUSTinti;doublepi=3.1415926;Stringname;,华中科技大学IBM技术中心,HUSTbooleanb=1;,原始类型,华中科技大学IBM技术中心,HUST,华中科技大学IBM技术中心,HUSTif(i0)inti=20;System.out.println(“Thevalueofi=”+i);System.out.println(“Thevalueofi=”+i);,华中科技大学IBM技术中心,HUST,finalintblankfinal;.blankfinal=0;,华中科技大学IBM技术中心,HUSTRectanglerect_one=newRectangle(origin_one,100,200);Rectanglerect_two=newRectangle(50,100);/显示rect_one的宽、高以及面积System.out.println(Widthofrect_one:+rect_one.width);System.out.println(Heightofrect_one:+rect_one.height);System.out.println(Areaofrect_one:+rect_one.area();rect_two.origin=origin_one;/设置rect_two的位置/显示rect_two的位置System.out.println(XPositionofrect_two:+rect_two.origin.x);System.out.println(YPositionofrect_two:+rect_two.origin.y);/移动rect_two并且显示它的新位置rect_two.move(40,72);System.out.println(XPositionofrect_two:+rect_two.origin.x);System.out.println(YPositionofrect_two:+rect_two.origin.y);,华中科技大学IBM技术中心,HUSTpublicinty=0;publicPoint(intx,inty)this.x=x;this.y=y;,Pointorigin_one=newPoint(23,94);,华中科技大学IBM技术中心,HUSTPointorigin_two=newPoint(23,94);,Pointorigin_one=newPoint(23,94);Pointorigin_two=origin_two;,华中科技大学IBM技术中心,HUSTpublicintheight=0;publicPointorigin;publicRectangle()origin=newPoint(0,0);publicRectangle(Pointp)origin=p;publicRectangle(intw,inth)this(newPoint(0,0),w,h);publicRectangle(Pointp,intw,inth)origin=p;width=w;height=h;.,一个类可以包含多个构造器,这种情况成为构造器的重载同一个类中的多个构造器通过参数的数目及类型的不同来区分,华中科技大学IBM技术中心,HUSTstaticintshare_data;classDemoABCDa,b,c,d;/实例化,华中科技大学IBM技术中心,HUSTSystem.out.println(Heightofrect_one:+rect_one.height);intheight=newRectangle().height;,华中科技大学IBM技术中心,HUST或者objectReference.methodName();,System.out.println(Areaofrect_one:+rect_one.area();rect_two.move(40,72);intareaOfRectangle=newRectangle(100,50).area();,华中科技大学IBM技术中心,HUST/超出对象作用域,引用变量引用了其它对象或引用了空对象StringBuffers=newStringBuffer(“test1”);s=newStringBuffer(“test2”);/引用了新的对象s=null;/引用为空,华中科技大学IBM技术中心,HUSTmyRect.width=40;myRect.height=50;System.out.println(myRectsareais+myRect.area();,华中科技大学IBM技术中心,HUSTRectanglerectangle=newRectangle(point,20,20);point=null;,华中科技大学IBM技术中心,HUSTCharactera2=newCharacter(a);Characterb=newCharacter(b);intdifference=pareTo(b);if(difference=0)System.out.println(aisequaltob.);elseif(difference0)System.out.println(aisgreaterthanb.);System.out.println(ais+(a.equals(a2)?equal:notequal)+toa2.);System.out.println(Thecharacter+a.toString()+is+(Character.isUpperCase(a.charValue()?upper:lower)+case.);,程序的输出:aislessthanb.aisequaltoa2.Thecharacteraislowercase.,华中科技大学IBM技术中心,HUSTCharacterb=newCharacter(a);,下列boolean表达式的值是true还是false?(1)pareTo(b)=0(2)a.equals(b)(3)a=b,华中科技大学IBM技术中心,HUST此外,也可根据String类的构造函数创建String对象:Stringname=newString(“Petter”);对于程序任何位置出现的双引号标记的字符串,系统都会自动创建一个String对象。可通过String对象的方法对字符串进行操作,华中科技大学IBM技术中心,HUSTs=s+“defg;System.out.println(s);,程序运行结果是abc还是abcdefg?,华中科技大学IBM技术中心,HUSTstr=str+“当修改操作频繁,或字符串的值很大时,会额外分配大量内存因此,Java语言引入了一个StringBuffer类,用来表示内容可以扩充和修改字符串对象,华中科技大学IBM技术中心,HUSTStringBufferdest=newStringBuffer(s);,华中科技大学IBM技术中心,HUSTintlen=palindrome.length();,华中科技大学IBM技术中心,HUSTcharaChar=anotherPalindrome.charAt(9);,华中科技大学IBM技术中心,HUSTStringroar=anotherPalindrome.substring(11,15);,华中科技大学IBM技术中心,HUSTprivatecharpathSeparator,extensionSeparator;publicFilename(Stringstr,charsep,charext)fullPath=str;pathSeparator=sep;extensionSeparator=ext;publicStringextension()intdot=fullPath.lastIndexOf(extensionSeparator);returnfullPath.substring(dot+1);publicStringfilename()intdot=fullPath.lastIndexOf(extensionSeparator);intsep=fullPath.lastIndexOf(pathSeparator);returnfullPath.substring(sep+1,dot);publicStringpath()intsep=fullPath.lastIndexOf(pathSeparator);returnfullPath.substring(0,sep);,华中科技大学IBM技术中心,HUSTSystem.out.println(Extension=+myHomePage.extension();System.out.println(Filename=+myHomePage.filename();System.out.println(Path=+myHomePage.path();,程序输出:Extension=htmlFilename=indexPath=/home/mem,华中科技大学IBM技术中心,HUSTFloatfloatTwo=Float.valueOf(1.0);DoubledoubleOne=newDouble(1.0);intdifference=floatOpareTo(floatTwo);if(difference=0)System.out.println(floatOneisequaltofloatTwo.);elseif(difference0)System.out.println(floatOneisgreaterthanfloatTwo.);System.out.println(floatOneis“+(floatOne.equals(doubleOne)?equal:notequal)+todoubleOne.);,floatOneisequaltooneAgain.floatOneisnotequaltodoubleOne.,华中科技大学IBM技术中心,HUSTanArray=newint10;for(inti=0;ianArray.length;i+)anArrayi=i;System.out.print(anArrayi+);System.out.println();,华中科技大学IBM技术中心,HUSTfor(inti=0;ianArray.length;i+)System.out.println(anArrayi);,华中科技大学IBM技术中心,HUSTaMatrix1=newint5;aMatrix2=newint10;,华中科技大学IBM技术中心,HUSTcharcopyTo=newchar7;System.arraycopy(copyFrom,2,copyTo,0,7);System.out.println(newString(copyTo);,华中科技大学IBM技术中心,HUSTfor(inti=0;istringBuffers.length;i+)stringBuffersi.append(StringBufferatindex+i);,欢迎提问,Question!,第六章接口和包,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST,publicinterfaceMyInterfaceintMAXSIZE=1024;publicabstractmyMethod(Stringname);,华中科技大学IBM技术中心,HUST声明接口也需要给出访问控制符;接口具有继承性,通过关键字extends声明该新接口是某父接口的派生接口;一个接口可以有多个父接口,它们之间用逗号分隔,形成父接口列表。,华中科技大学IBM技术中心,HUST系统默认:接口中的所有属性都是public,static和final(公共,静态和最终);系统默认:接口中的所有方法都是public和abstract(公共和抽象);接口中方法的方法体可用Java语言书写,也可用其它语言书写(加native修饰)。,华中科技大学IBM技术中心,HUST如果实现某个接口的类不是abstract的抽象类,则在类的定义部分必须为所有抽象方法定义具体方法体,方法头部分应该与接口中的定义完全一致;,华中科技大学IBM技术中心,HUST一个类在实现某个接口的抽象方法时,必须使用完全相同的方法声明。接口的抽象方法访问修饰符为public,所以,类在实现方法时必须使用修饰符public,否则,系统将警告;因为缩小了接口中定义的方法的访问控制范围。,华中科技大学IBM技术中心,HUSTfinalStringoracleTicker=ORCL;finalStringciscoTicker=CSCO;voidvalueChanged(StringtickerSymbol,doublenewValue);voidcurrentValue(StringtickerSymbol,doublenewValue);,华中科技大学IBM技术中心,HUST,华中科技大学IBM技术中心,HUST,华中科技大学IBM技术中心,HUST该语句是将当前类置于包cardclassess中,需要在当前文件夹下创建一个名为cardclasses的子文件夹。再例如:packagecardsystem.cardclasses;该语句将当前类置于cardsystem.cardclasses中,需要在当前文件夹下创建子文件cardsystem并在cardsystem下再创建的子文件cardclasses,当前类存放在这个文件夹里。,华中科技大学IBM技术中心,HUSTVectorvc=newVector();,华中科技大学IBM技术中心,HUSTVectorvc=newVector();消除名称的二义性使用成员的限定名;使用环境变量classpathsetclasspath=javacclasspathMyClass.javajavaclasspathMyClass,华中科技大学IBM技术中心,HUST这样,我们在使用java.util包中的任何类时,就可以直接使用简单类名。需要注意的是,import语句要么导入一个类,要么导入一个完整包。不能使用通配符标记包的子集或多个包,下面三条语句均无法通过编译:importjava.applet.A*;importjava.*.*;importjava.*.io;,华中科技大学IBM技术中心,HUSTInServer.javaadd:packagemygame.server;:InUtilities.javaadd:packagemygame.shared;,华中科技大学IBM技术中心,HUSTpublicstaticvoidmain(Stringargs)ExampleOfExceptioneoe=newExampleOfException();eoe.methodA();System.out.println(Programfinished.);voidmethodA()methodB();voidmethodB()methodC();voidmethodC()for(inti=0;i4;i+)System.out.println(linesi);,ThefirstlineThesecondlineThelastlineExceptioninthreadmainjava.lang.ArrayIndexOutOfBoundsException:3atExampleOfException.methodC(ExampleOfException.java:16)atExampleOfException.methodB(ExampleOfException.java:12)atExampleOfException.methodA(ExampleOfException.java:9)atExampleOfException.main(ExampleOfException.java:6),华中科技大学IBM技术中心,HUST,华中科技大学IBM技术中心,HUSTpublicstaticvoidmain(Stringargs)ExampleOfExceptioneoe=newExampleOfException();eoe.methodA();System.out.println(Programfinished.);.voidmethodC()for(inti=0;i10)thrownewMyException(a);System.out.println(normalexit);publicstaticvoidmain(Stringargs)trycompute(1);compute(20);catch(MyExceptione)System.out.println(Caught+e);,程序运行结果:calledcompute(1)normalexitcalledcompute(20)CaughtMyException20,华中科技大学IBM技术中心,HUSTif(n1)thrownewNumberFormatException();catch(NumberFormatExceptione)System.out.println(输入范围错误!);代码2:intn=InputReader.inputInteger(请输入一个整数);if(n1)System.out.println(输入范围错误!);,代码1采用了异常处理方式;代码2则通过对用户输入的分析避免了异常的使用,提高了代码效率。,华中科技大学IBM技术中心,HUSTStringline=null;tryinput=newRandomAccessFile(named,“r”);while(line=input.readLine()!=nullSystem.out.println(line);return;finallyif(input!=null)input.close();,第八章线程,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUSTpublicPrintThread(Stringname)super(name);sleepTime=(int)(Math.random()*5000);System.out.println(Name:+getName()+;sleep:+sleepTime);publicvoidrun()trySystem.out.println(getName()+goingtosleep);Thread.sleep(sleepTime);catch(InterruptedExceptionie)System.err.println(ie.toString();System.out.println(getName()+donesleeping);,华中科技大学IBM技术中心,HUSTthread1=newPrintThread(thread1);thread2=newPrintThread(thread2);thread3=newPrintThread(thread3);thread4=newPrintThread(thread4);System.out.println(nStartingthreads);thread1.start();thread2.start();thread3.start();thread4.start();System.out.println(Threadsstartedn);,华中科技大学IBM技术中心,HUSTprivateintsleepTime;publicPrintRunnable(Stringname)=name;sleepTime=(int)(Math.random()*5000);System.out.println(Name:+name+;sleep:+sleepTime);publicvoidrun()trySystem.out.println(name+goingtosleep);Thread.sleep(sleepTime);catch(InterruptedExceptionie)System.err.println(ie.toString();System.out.println(name+donesleeping);,华中科技大学IBM技术中心,HUSTthread1=newPrintRunnable(thread1);thread2=newPrintRunnable(thread2);thread3=newPrintRunnable(thread3);thread4=newPrintRunnable(thread4);System.out.println(nStartingthreads);newThread(thread1).start();newThread(thread2).start();newThread(thread3).start();newThread(thread4).start();System.out.println(Threadsstartedn);,华中科技大学IBM技术中心,HUSTimportjava.awt.Graphics;importjava.util.Date;publicclassClockAppletextendsAppletimplementsRunnableprivateThreadclockThread=null;publicvoidstart()if(clockThread=null)clockThread=newThread(this);clockThread.start();publicvoidpaint(Graphicsg)Datedate=newDate();g.drawString(date.toString(),5,10);,华中科技大学IBM技术中心,HUSTtryThread.sleep(1000);catch(InterruptedExceptione),华中科技大学IBM技术中心,HUST除非更高优先级的抢占CPU,或者,华中科技大学IBM技术中心,HUSTpublicvoidrun()while(true)inti;for(intj=0;j100000;j+)for(intk=0;k1000;k+)i=j+k;if(getPriority()NORM_PRIORITY)System.out.println(getName()+isrunning.);,华中科技大学IBM技术中心,HUSTPriorityTesttest2=newPriorityTest(Lowerprioritythread);test2.setPriority(4);test1.start();test2.start();从运行结果上看,低优先级的线程也有执行的机会。,华中科技大学IBM技术中心,HUSTpublicintget()returnthis.data;publicvoidput(intdata)this.data=data;,华中科技大学IBM技术中心,HUSTpublicProducer(CubbyHoleres)this.res=res;publicvoidrun()for(inti=0;i10;i+)res.put(i);System.out.println(“Producerput:”+i);trysleep(int)(Math.random()*100);catch(InterruptedExceptione),华中科技大学IBM技术中心,HUSTpublicConsumer(CubbyHoleres)this.res=res;publicvoidrun()for(inti=0;i10;i+)System.out.println(“Consumerget:”+res.get();trysleep(int)(Math.random()*100);catch(InterruptedExceptione),华中科技大学IBM技术中心,HUSTProducerpro=newProducer(res);Consumercon=newConsumer(res);pro.start();con.start();,华中科技大学IBM技术中心,HUSTprivatebooleangetable;publicsynchronizedintget()while(getable=false)trywait();catch(InterruptedExceptione)getable=false;notifyAll();returnthis.data;,华中科技大学IBM技术中心,HUSTcatch(InterruptedExceptione)this.data=data;getable=true;notifyAll();,华中科技大学IBM技术中心,HUSTsynchronizedb()a();,华中科技大学IBM技术中心,HUSTThreadthread1=newThread(myGroup,“threadone”);Threadthread2=newThread(myGroup,“threadtwo”);,华中科技大学IBM技术中心,HUSTclassCopyprivatevoidcopy(InputStreamin,OutputStreamout)throwsIOExceptionbytebuf=newbyte4096;intlen=in.read(buf);while(len!=-1)out.write(buf,0,len);len=in.read(buf);publicvoidcat(Stringfsrc,Stringfdest)tryInputStreamin=newFileInputStream(fsrc);OutputStreamout=newFileOutputStream(fdest,true);copy(in,out);out.close();in.close();catch(IOExceptionex)System.err.println(ex);,华中科技大学IBM技术中心,HUSTc.cat(“in.dat”,”out.dat”);,华中科技大学IBM技术中心,HUSTpublicclassByteArrayOutputStreamTestpublicstaticvoidmain(Stringargs)tryByteArrayOutputStreambaos=newByteArrayOutputStream();PrintStreamps=newPrintStream(baos);for(inti=0;i1000;i+)ps.println(i+ABCDEFGHIJKLMNOPQRSTUVWXYZ);longstart=System.currentTimeMillis();FileOutputStreamfos=newFileOutputStream(ByteArrayOutputStreamTest);baos.writeTo(fos);fos.close();longstop=System.currentTimeMillis();System.out.println(timeelapsed(milliseconds)=+(stop-start);catch(FileNotFoundExceptionfnfe)System.out.println(fnfe.getMessage();catch(IOExceptionioe)System.out.println(ioe.getMessage();,华中科技大学IBM技术中心,HUSTPipedOutputStream(PipedInputStreampis);通过各自的connect()方法连接:在类PipedInputStream中,connect(PipedOutputStreampos);在类PipedOutputStream中,connect(PipedInputStreampis);,华中科技大学IBM技术中心,HUSTPipedInputStreampis=newPipedInputStream();PipedOutputStreampos=newPipedOutputStream(pis);System.out.println(PipedInputStream);trypos.write(dataA);pos.write(dataB);System.out.println(byte)pis.read();System.out.println(byte)pis.read();finallypis.close();pos.close();,华中科技大学IBM技术中心,HUSTpublicclassReadLineTestpublicstaticvoidmain(Stringargs)tryBufferedReaderbr=newBufferedReader(newFileReader(args0);Stringline=null;inti=0;while(line=br.readLine()!=null)i+;System.out.println(i+:+line);br.close();catch(Exceptione)e.printStackTrace();,华中科技大学IBM技术中心,HUSTpublicclassFileTestpublicstaticvoidmain(Stringargs)throwsIOExceptionRandomAccessFileraf=newRandomAccessFile(foo.txt,rw);tryWriterout=newOutputStreamWriter(newFileOutputStream(raf.getFD(),UTF-8);out.write(HelloWorld!);out.flush();raf.seek(6);out.write(Java);out.flush();finallyraf.close();,第十章图形用户界面,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUSTimportjavax.swing.*;publicclassGUITestextendsJFrameprivateJLabellabel;privateJButtonbutton;privateJCheckBoxcheckbox;privateJRadioButtonrbutton;privateJTextFieldtextfield;privateJComboBoxcbox;privateJListlist;,华中科技大学IBM技术中心,HUSTContainercontainer=getContentPane();container.setLayout(newFlowLayout();label=newJLabel(ImJLabel);button=newJButton(ImJButton);rbutton=newJRadioButton(ImJRadioButton);textfield=newJTextField(10);textfield.setText(ImJTextField);checkbox=newJCheckBox(ImJCheckBox);cbox=ne

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论