Java语言培训教案.ppt_第1页
Java语言培训教案.ppt_第2页
Java语言培训教案.ppt_第3页
Java语言培训教案.ppt_第4页
Java语言培训教案.ppt_第5页
已阅读5页,还剩301页未读 继续免费阅读

下载本文档

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

文档简介

1、第一章 Java语言概述,华中科技大学IBM技术中心 2008,华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST public int x, y; /constructor public Spot() x = -1; y = -1; size = 1; /methods for access to the size instance variable public void setSize(int newSize) if (newSize = 0) size = newSize; public int getSize() return size; ,华中科技大学IB

2、M技术中心,HUST . spot = new Spot();,华中科技大学IBM技术中心,HUST g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);,华中科技大学IBM技术中心,HUST spot.setSize(RADIUS); spot.x = event.getX(); spot.y = event.getY(); repaint(); public void mouseClicked(MouseEvent event) public void mouseReleased(MouseEvent event) public void

3、 mouseEntered(MouseEvent event) public void mouseExited(MouseEvent event) ,欢迎提问,Question !,第三章 Java语言基础,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST int i; double pi = 3.1415926; String name;,华中科技大学IBM技术中心,HUST boolean b = 1;,原始类型,华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST if ( i 0) int i = 20; System.out.println(“

4、The value of i = ” + i); System.out.println(“The value of i = ” + i);,华中科技大学IBM技术中心,HUST ,final int blankfinal; . . . blankfinal = 0;,华中科技大学IBM技术中心,HUST Rectangle rect_one = new Rectangle(origin_one, 100, 200); Rectangle rect_two = new Rectangle(50, 100); / 显示rect_one的宽、高以及面积 System.out.println(Widt

5、h of rect_one: + rect_one.width); System.out.println(Height of rect_one: + rect_one.height); System.out.println(Area of rect_one: + rect_one.area(); rect_two.origin = origin_one; /设置rect_two的位置 / 显示rect_two的位置 System.out.println(X Position of rect_two: + rect_two.origin.x); System.out.println(Y Posi

6、tion of rect_two: + rect_two.origin.y); / 移动rect_two并且显示它的新位置 rect_two.move(40, 72); System.out.println(X Position of rect_two: + rect_two.origin.x); System.out.println(Y Position of rect_two: + rect_two.origin.y); ,华中科技大学IBM技术中心,HUST public int y = 0; public Point(int x, int y) this.x = x; this.y =

7、 y; ,Point origin_one = new Point(23, 94);,华中科技大学IBM技术中心,HUST Point origin_two = new Point(23, 94);,Point origin_one = new Point(23, 94); Point origin_two =origin_two;,华中科技大学IBM技术中心,HUST public int height = 0; public Point origin; public Rectangle() origin = new Point(0, 0); public Rectangle(Point p

8、) origin = p; public Rectangle(int w, int h) this(new Point(0, 0), w, h); public Rectangle(Point p, int w, int h) origin = p; width = w; height = h; . ,一个类可以包含多个构造器,这种情况成为构造器的重载 同一个类中的多个构造器通过参数的数目及类型的不同来区分,华中科技大学IBM技术中心,HUST static int share_data; class Demo ABCD a,b,c,d; /实例化 ,华中科技大学IBM技术中心,HUST Sy

9、stem.out.println(Height of rect_one: + rect_one.height); int height=new Rectangle().height;,华中科技大学IBM技术中心,HUST 或者 objectReference.methodName();,System.out.println(Area of rect_one: + rect_one.area(); rect_two.move(40, 72); int areaOfRectangle = new Rectangle(100, 50).area();,华中科技大学IBM技术中心,HUST / 超出对

10、象作用域 ,引用变量引用了其它对象或引用了空对象 StringBuffer s = new StringBuffer(“test1”); s = new StringBuffer(“test2”); / 引用了新的对象 s = null; / 引用为空,华中科技大学IBM技术中心,HUST myRect.width = 40; myRect.height = 50; System.out.println(myRects area is + myRect.area(); ,华中科技大学IBM技术中心,HUST Rectangle rectangle = new Rectangle(point,

11、20, 20); point = null;,华中科技大学IBM技术中心,HUST Character a2 = new Character(a); Character b = new Character(b); int difference = pareTo(b); if (difference = 0) System.out.println(a is equal to b.); else if (difference 0) System.out.println(a is greater than b.); System.out.println(a is + (a.equals(a2) ?

12、equal : not equal)+ to a2.); System.out.println(The character + a.toString() + is + (Character.isUpperCase(a.charValue() ? upper : lower)+ case.); ,程序的输出: a is less than b. a is equal to a2. The character a is lowercase.,华中科技大学IBM技术中心,HUST Character b = new Character(a);,下列boolean表达式的值是true还是false?

13、(1)pareTo(b)=0 (2)a.equals(b) (3)a=b,华中科技大学IBM技术中心,HUST 此外,也可根据String类的构造函数创建String对象: String name = new String(“Petter”); 对于程序任何位置出现的双引号标记的字符串,系统都会自动创建一个String对象。 可通过String对象的方法对字符串进行操作,华中科技大学IBM技术中心,HUST s=s+“defg; System.out.println(s); ,程序运行结果是abc还是abcdefg?,华中科技大学IBM技术中心,HUST str = str + “ 当修改操作

14、频繁,或字符串的值很大时,会额外分配大量内存 因此,Java语言引入了一个StringBuffer类,用来表示内容可以扩充和修改字符串对象,华中科技大学IBM技术中心,HUST StringBuffer dest = new StringBuffer(s);,华中科技大学IBM技术中心,HUST int len = palindrome.length();,华中科技大学IBM技术中心,HUST char aChar = anotherPalindrome.charAt(9);,华中科技大学IBM技术中心,HUST String roar = anotherPalindrome.substrin

15、g(11, 15);,华中科技大学IBM技术中心,HUST private char pathSeparator, extensionSeparator; public Filename(String str, char sep, char ext) fullPath = str; pathSeparator = sep; extensionSeparator = ext; public String extension() int dot = fullPath.lastIndexOf(extensionSeparator); return fullPath.substring(dot + 1

16、); public String filename() int dot = fullPath.lastIndexOf(extensionSeparator); int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(sep + 1, dot); public String path() int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(0, sep); ,华中科技大学IBM技术中心,HUST System.ou

17、t.println(Extension = + myHomePage.extension(); System.out.println(Filename = + myHomePage.filename(); System.out.println(Path = + myHomePage.path(); ,程序输出: Extension = html Filename = index Path = /home/mem,华中科技大学IBM技术中心,HUST Float floatTwo = Float.valueOf(1.0); Double doubleOne = new Double(1.0);

18、int difference = floatOpareTo(floatTwo); if (difference = 0) System.out.println(floatOne is equal to floatTwo.); else if (difference 0) System.out.println(floatOne is greater than floatTwo.); System.out.println(floatOne is “ +(floatOne.equals(doubleOne) ? equal : not equal) + to doubleOne.); ,floatO

19、ne is equal to oneAgain. floatOne is not equal to doubleOne.,华中科技大学IBM技术中心,HUST anArray = new int10; for (int i = 0; i anArray.length; i+) anArrayi = i; System.out.print(anArrayi + ); System.out.println(); ,华中科技大学IBM技术中心,HUST for (int i = 0; i anArray.length; i+) System.out.println(anArrayi); ,华中科技大

20、学IBM技术中心,HUST aMatrix1 = new int5; aMatrix2 = new int10;,华中科技大学IBM技术中心,HUST char copyTo = new char7; System.arraycopy(copyFrom, 2, copyTo, 0, 7); System.out.println(new String(copyTo); ,华中科技大学IBM技术中心,HUST for (int i = 0; i stringBuffers.length; i +) stringBuffersi.append(StringBuffer at index + i);

21、,欢迎提问,Question !,第六章 接口和包,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST ,public interface MyInterface int MAXSIZE = 1024; public abstract myMethod(String name); ,华中科技大学IBM技术中心,HUST 声明接口也需要给出访问控制符; 接口具有继承性,通过关键字extends声明该新接口是某父接口的派生接口;一个接口可以有多个父接口,它们之间用逗号分隔,形成父接口列表。,华中科技大学IBM技术中心,HUST 系统默认:接口中的所有属性都是public, stati

22、c和final (公共,静态和最终); 系统默认:接口中的所有方法都是public和 abstract (公共和抽象); 接口中方法的方法体可用Java语言书写,也可用其它语言书写(加native修饰)。,华中科技大学IBM技术中心,HUST 如果实现某个接口的类不是abstract的抽象类,则在类的定义部分必须为所有抽象方法定义具体方法体,方法头部分应该与接口中的定义完全一致;,华中科技大学IBM技术中心,HUST 一个类在实现某个接口的抽象方法时,必须使用完全相同的方法声明。 接口的抽象方法访问修饰符为public,所以,类在实现方法时必须使用修饰符public,否则,系统将警告;因为缩小

23、了接口中定义的方法的访问控制范围。,华中科技大学IBM技术中心,HUST final String oracleTicker = ORCL; final String ciscoTicker = CSCO; void valueChanged(String tickerSymbol, double newValue); void currentValue(String tickerSymbol, double newValue); ,华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST 该语句是将当前类置于包cardclasses

24、s中,需要在当前文件夹下创建一个名为cardclasses的子文件夹。 再例如:package cardsystem.cardclasses; 该语句将当前类置于cardsystem.cardclasses中,需要在当前文件夹下创建子文件cardsystem并在 cardsystem下再创建的子文件cardclasses,当前类存放在这个文件夹里。,华中科技大学IBM技术中心,HUST Vector vc = new Vector();,华中科技大学IBM技术中心,HUST Vector vc = new Vector(); 消除名称的二义性 使用成员的限定名; 使用环境变量 classpat

25、h set classpath= javac classpath MyClass.java java classpath MyClass,华中科技大学IBM技术中心,HUST 这样,我们在使用java.util包中的任何类时,就可以直接使用简单类名。需要注意的是,import语句要么导入一个类,要么导入一个完整包。不能使用通配符标记包的子集或多个包,下面三条语句均无法通过编译: import java.applet.A*; import java.*.*; import java.*.io;,华中科技大学IBM技术中心,HUST In Server.java add: package myga

26、me.server;: In Utilities.java add: package mygame.shared;,华中科技大学IBM技术中心,HUST public static void main (String args) ExampleOfException eoe = new ExampleOfException(); eoe.methodA(); System.out.println(Program finished.); void methodA() methodB(); void methodB() methodC(); void methodC() for (int i=0;

27、 i4; i+) System.out.println (linesi); ,The first line The second line The last line Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 3 at ExampleOfException.methodC(ExampleOfException.java:16) at ExampleOfException.methodB(ExampleOfException.java:12) at ExampleOfException.methodA(E

28、xampleOfException.java:9) at ExampleOfException.main(ExampleOfException.java:6),华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST public static void main (String args) ExampleOfException eoe = new ExampleOfException(); eoe.methodA(); System.out.println(Program finished.); . void methodC() for (int i=0; i4; i+)

29、 try System.out.println (linesi); catch (ArrayIndexOutOfBoundsException e) System.out.println(Re-setting Index Value); ,华中科技大学IBM技术中心,HUST finally return 0; ,华中科技大学IBM技术中心,HUST try if( sel=0 ) System.out.println(no Exception caught); return; else if( sel=1 ) int i=0; int j=4/i; else if( sel=2 ) int

30、iArray=new int4; iArray10=3; ,华中科技大学IBM技术中心,HUST catch( ArrayIndexOutOfBoundsException e ) System.out.println(Catch +e.getMessage(); catch( Exception e ) System.out.println(Will not be executed); finally System.out.println(in Proc finally); public static void main( String args ) Proc( 0 ); Proc( 1 )

31、; Proc( 2 ); ,程序运行结果: - In Situation0 - no Exception caught in Proc finally - In Situation1 - Catch java.lang.ArithmeticException: / by zero in Proc finally - In Situation2 - Catch 10 in Proc finally,华中科技大学IBM技术中心,HUST if(sel=0) System.out.println(no Exception caught); return; else if(sel=1) int iAr

32、ray=new int4; iArray10=3; ,华中科技大学IBM技术中心,HUST Proc(1); catch(ArrayIndexOutOfBoundsException e) System.out.println(Catch +e); finally System.out.println(in Proc finally); ,程序运行结果: - In Situation0 - no Exception caught - In Situation1 - Catch java.lang.ArrayIndexOutOfBoundsException:10 in Proc finally

33、,华中科技大学IBM技术中心,HUST MyException( int a ) detail = a; public String toString( ) return MyException +detail; ,华中科技大学IBM技术中心,HUST if( a10 ) throw new MyException(a); System.out.println(normal exit); public static void main( String args ) try compute( 1 ); compute( 20 ); catch( MyException e ) System.ou

34、t.println(Caught +e); ,程序运行结果: called compute(1)normal exitcalled compute(20)Caught MyException 20,华中科技大学IBM技术中心,HUST if (n1) throw new NumberFormatException(); catch (NumberFormatException e) System.out.println(输入范围错误!); 代码2: int n = InputReader.inputInteger(请输入一个整数); if (n1) System.out.println(输入范

35、围错误!);,代码1采用了异常处理方式;代码2则通过对用户输入的分析避免了异常的使用,提高了代码效率。,华中科技大学IBM技术中心,HUST String line = null; try input = new RandomAccessFile(named, “r”); while (line = input.readLine() != null System.out.println(line); return; finally if (input != null) input.close(); ,第八章 线程,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST public

36、PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random() * 5000 ); System.out.println( Name: + getName() + ; sleep: + sleepTime ); public void run() try System.out.println( getName() + going to sleep ); Thread.sleep( sleepTime ); catch ( InterruptedException ie ) System.err.printl

37、n( ie.toString() ); System.out.println( getName() + done sleeping ); ,华中科技大学IBM技术中心,HUST thread1 = new PrintThread( thread1 ); thread2 = new PrintThread( thread2 ); thread3 = new PrintThread( thread3 ); thread4 = new PrintThread( thread4 ); System.out.println( nStarting threads ); thread1.start(); t

38、hread2.start(); thread3.start(); thread4.start(); System.out.println( Threads startedn ); ,华中科技大学IBM技术中心,HUST private int sleepTime; public PrintRunnable( String name ) = name; sleepTime = (int) ( Math.random() * 5000 ); System.out.println( Name: + name + ; sleep: + sleepTime ); public voi

39、d run() try System.out.println( name + going to sleep ); Thread.sleep( sleepTime ); catch ( InterruptedException ie ) System.err.println( ie.toString() ); System.out.println( name + done sleeping ); ,华中科技大学IBM技术中心,HUST thread1 = new PrintRunnable ( thread1 ); thread2 = new PrintRunnable ( thread2 );

40、 thread3 = new PrintRunnable ( thread3 ); thread4 = new PrintRunnable ( thread4 ); System.out.println( nStarting threads ); new Thread(thread1).start(); new Thread(thread2).start(); new Thread(thread3).start(); new Thread(thread4).start(); System.out.println( Threads startedn ); ,华中科技大学IBM技术中心,HUST

41、import java.awt.Graphics; import java.util.Date; public class ClockApplet extends Applet implements Runnable private Thread clockThread = null; public void start() if (clockThread =null) clockThread = new Thread(this); clockThread.start(); public void paint(Graphics g) Date date = new Date(); g.draw

42、String(date.toString(), 5, 10); ,华中科技大学IBM技术中心,HUST try Thread.sleep(1000); catch (InterruptedException e) ,华中科技大学IBM技术中心,HUST 除非更高优先级的抢占CPU,或者,华中科技大学IBM技术中心,HUST public void run() while(true) int i; for(int j=0; j100000; j+) for(int k=0; k1000; k+) i = j + k; if (getPriority()NORM_PRIORITY) System.

43、out.println(getName() + is running.); ,华中科技大学IBM技术中心,HUST PriorityTest test2 = new PriorityTest(Lower priority thread); test2.setPriority(4); test1.start(); test2.start(); 从运行结果上看,低优先级的线程也有执行的机会。,华中科技大学IBM技术中心,HUST public int get() return this.data; public void put(int data) this.data = data; ,华中科技大

44、学IBM技术中心,HUST public Producer(CubbyHole res) this.res = res; public void run() for (int i=0; i10; i+) res.put(i); System.out.println(“Producer put:” + i); try sleep(int)(Math.random()*100); catch (InterruptedException e) ,华中科技大学IBM技术中心,HUST public Consumer(CubbyHole res) this.res = res; public void

45、run() for (int i=0; i10; i+) System.out.println(“Consumer get:” + res.get(); try sleep(int)(Math.random()*100); catch (InterruptedException e) ,华中科技大学IBM技术中心,HUST Producer pro = new Producer(res); Consumer con = new Consumer(res); pro.start(); con.start(); ,华中科技大学IBM技术中心,HUST private boolean getable

46、; public synchronized int get() while (getable = false) try wait(); catch (InterruptedException e) getable = false; notifyAll(); return this.data; ,华中科技大学IBM技术中心,HUST catch (InterruptedException e) this.data = data; getable = true; notifyAll(); ,华中科技大学IBM技术中心,HUST synchronized b() a(); ,华中科技大学IBM技术中

47、心,HUST Thread thread1 = new Thread(myGroup, “thread one”); Thread thread2 = new Thread(myGroup, “thread two”);,华中科技大学IBM技术中心,HUST class Copy private void copy(InputStream in, OutputStream out) throws IOException byte buf = new byte4096; int len = in.read(buf); while (len != -1) out.write(buf, 0, len

48、); len = in.read(buf); public void cat(String fsrc, String fdest) try InputStream in = new FileInputStream(fsrc); OutputStream out = new FileOutputStream(fdest, true); copy(in, out); out.close(); in.close(); catch (IOException ex) System.err.println(ex); ,华中科技大学IBM技术中心,HUST c.cat(“in.dat”,”out.dat”)

49、; ,华中科技大学IBM技术中心,HUST publicclassByteArrayOutputStreamTest publicstaticvoidmain(Stringargs) try ByteArrayOutputStreambaos=newByteArrayOutputStream(); PrintStreamps=newPrintStream(baos); for(inti=0;i1000;i+) ps.println(i+ABCDEFGHIJKLMNOPQRSTUVWXYZ); longstart=System.currentTimeMillis(); FileOutputStr

50、eamfos= 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

51、.getMessage(); ,华中科技大学IBM技术中心,HUST PipedOutputStream(PipedInputStream pis); 通过各自的connect()方法连接: 在类PipedInputStream中,connect(PipedOutputStream pos); 在类PipedOutputStream中,connect(PipedInputStream pis);,华中科技大学IBM技术中心,HUST PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedO

52、utputStream(pis); System.out.println(PipedInputStream); try pos.write(dataA); pos.write(dataB); System.out.println(byte)pis.read(); System.out.println(byte)pis.read(); finally pis.close(); pos.close(); ,华中科技大学IBM技术中心,HUST public class ReadLineTest public static void main(String args) try BufferedRea

53、der br = new BufferedReader(new FileReader(args0); String line = null; int i = 0; while(line = br.readLine() != null) i+; System.out.println(i + : + line); br.close(); catch(Exception e) e.printStackTrace(); ,华中科技大学IBM技术中心,HUST publicclassFileTest publicstaticvoidmain(Stringargs)throwsIOException Ra

54、ndomAccessFileraf=newRandomAccessFile(foo.txt,rw); try Writerout= newOutputStreamWriter(newFileOutputStream(raf.getFD(),UTF-8); out.write(HelloWorld!); out.flush(); raf.seek(6); out.write(Java); out.flush(); finally raf.close(); ,第十章 图形用户界面,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST import javax.swing.*; publ

55、ic class GUITest extends JFrame private JLabel label; private JButton button; private JCheckBox checkbox; private JRadioButton rbutton; private JTextField textfield; private JComboBox cbox; private JList list;,华中科技大学IBM技术中心,HUST Container container = getContentPane(); container.setLayout(new FlowLayout(); label = new JLabel(Im JLabel); but

温馨提示

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

评论

0/150

提交评论