版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第一章 Java语言概述,华中科技大学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; ,华中科技大学IBM技术中心,HUST . spot =
2、 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 mouseEntered(Mouse
3、Event 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(“The value of i = ”
4、+ 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(Width of rect_one: + re
5、ct_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 Position of rect_two: +
6、 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 = y; ,Point origin_o
7、ne = new Point(23, 94);,华中科技大学IBM技术中心,HUST public int height = 0; public Point origin; public Rectangle() origin = new Point(0, 0); public Rectangle(Point p) 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
8、= h; . ,一个类可以包含多个构造器,这种情况成为构造器的重载 同一个类中的多个构造器通过参数的数目及类型的不同来区分,华中科技大学IBM技术中心,HUST static int share_data; class Demo ABCD a,b,c,d; /实例化 ,华中科技大学IBM技术中心,HUST System.out.println(Height of rect_one: + rect_one.height); int height=new Rectangle().height;,华中科技大学IBM技术中心,HUST 或者 objectReference.methodName();,
9、System.out.println(Area of rect_one: + rect_one.area(); rect_two.move(40, 72); int areaOfRectangle = new Rectangle(100, 50).area();,华中科技大学IBM技术中心,HUST / 超出对象作用域 ,引用变量引用了其它对象或引用了空对象 StringBuffer s = new StringBuffer(“test1”); s = new StringBuffer(“test2”); / 引用了新的对象 s = null; / 引用为空,华中科技大学IBM技术中心,HUS
10、T myRect.width = 40; myRect.height = 50; System.out.println(myRects area is + myRect.area(); ,华中科技大学IBM技术中心,HUST Rectangle rectangle = new Rectangle(point, 20, 20); point = null;,华中科技大学IBM技术中心,HUST Character a2 = new Character(a); Character b = new Character(b); int difference = pareTo(b); if (diffe
11、rence = 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) ? equal : not equal)+ to a2.); System.out.println(The character + a.toString() + is + (Character.isUpperCase(a.charValue() ? upper : lower)+ case.
12、); ,程序的输出: 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? (1)pareTo(b)=0 (2)a.equals(b) (3)a=b,华中科技大学IBM技术中心,HUST 此外,也可根据String类的构造函数创建String对象: String name = new String(“Petter”); 对于程序任何位置出现的双引号标记的字符串,
13、系统都会自动创建一个String对象。 可通过String对象的方法对字符串进行操作,华中科技大学IBM技术中心,HUST s=s+“defg; System.out.println(s); ,程序运行结果是abc还是abcdefg?,华中科技大学IBM技术中心,HUST str = str + “ 当修改操作频繁,或字符串的值很大时,会额外分配大量内存 因此,Java语言引入了一个StringBuffer类,用来表示内容可以扩充和修改字符串对象,华中科技大学IBM技术中心,HUST StringBuffer dest = new StringBuffer(s);,华中科技大学IBM技术中心,
14、HUST int len = palindrome.length();,华中科技大学IBM技术中心,HUST char aChar = anotherPalindrome.charAt(9);,华中科技大学IBM技术中心,HUST String roar = anotherPalindrome.substring(11, 15);,华中科技大学IBM技术中心,HUST private char pathSeparator, extensionSeparator; public Filename(String str, char sep, char ext) fullPath = str; pa
15、thSeparator = sep; extensionSeparator = ext; public String extension() int dot = fullPath.lastIndexOf(extensionSeparator); return fullPath.substring(dot + 1); public String filename() int dot = fullPath.lastIndexOf(extensionSeparator); int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.s
16、ubstring(sep + 1, dot); public String path() int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(0, sep); ,华中科技大学IBM技术中心,HUST System.out.println(Extension = + myHomePage.extension(); System.out.println(Filename = + myHomePage.filename(); System.out.println(Path = + myHomePage.pa
17、th(); ,程序输出: Extension = html Filename = index Path = /home/mem,华中科技大学IBM技术中心,HUST Float floatTwo = Float.valueOf(1.0); Double doubleOne = new Double(1.0); int difference = floatOpareTo(floatTwo); if (difference = 0) System.out.println(floatOne is equal to floatTwo.); else if (difference 0) System.o
18、ut.println(floatOne is greater than floatTwo.); System.out.println(floatOne is “ +(floatOne.equals(doubleOne) ? equal : not equal) + to doubleOne.); ,floatOne is equal to oneAgain. floatOne is not equal to doubleOne.,华中科技大学IBM技术中心,HUST anArray = new int10; for (int i = 0; i anArray.length; i+) anArr
19、ayi = i; System.out.print(anArrayi + ); System.out.println(); ,华中科技大学IBM技术中心,HUST for (int i = 0; i anArray.length; i+) System.out.println(anArrayi); ,华中科技大学IBM技术中心,HUST aMatrix1 = new int5; aMatrix2 = new int10;,华中科技大学IBM技术中心,HUST char copyTo = new char7; System.arraycopy(copyFrom, 2, copyTo, 0, 7)
20、; System.out.println(new String(copyTo); ,华中科技大学IBM技术中心,HUST for (int i = 0; i stringBuffers.length; i +) stringBuffersi.append(StringBuffer at index + i); ,欢迎提问,Question !,第六章 接口和包,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST ,public interface MyInterface int MAXSIZE = 1024; public abstract myMethod(String nam
21、e); ,华中科技大学IBM技术中心,HUST 声明接口也需要给出访问控制符; 接口具有继承性,通过关键字extends声明该新接口是某父接口的派生接口;一个接口可以有多个父接口,它们之间用逗号分隔,形成父接口列表。,华中科技大学IBM技术中心,HUST 系统默认:接口中的所有属性都是public, static和final (公共,静态和最终); 系统默认:接口中的所有方法都是public和 abstract (公共和抽象); 接口中方法的方法体可用Java语言书写,也可用其它语言书写(加native修饰)。,华中科技大学IBM技术中心,HUST 如果实现某个接口的类不是abstract的抽
22、象类,则在类的定义部分必须为所有抽象方法定义具体方法体,方法头部分应该与接口中的定义完全一致;,华中科技大学IBM技术中心,HUST 一个类在实现某个接口的抽象方法时,必须使用完全相同的方法声明。 接口的抽象方法访问修饰符为public,所以,类在实现方法时必须使用修饰符public,否则,系统将警告;因为缩小了接口中定义的方法的访问控制范围。,华中科技大学IBM技术中心,HUST final String oracleTicker = ORCL; final String ciscoTicker = CSCO; void valueChanged(String tickerSymbol, d
23、ouble newValue); void currentValue(String tickerSymbol, double newValue); ,华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST 该语句是将当前类置于包cardclassess中,需要在当前文件夹下创建一个名为cardclasses的子文件夹。 再例如:package cardsystem.cardclasses; 该语句将当前类置于cardsystem.cardclasses中,需要在当前文件夹下创建子文件cardsystem并在 cardsystem下再
24、创建的子文件cardclasses,当前类存放在这个文件夹里。,华中科技大学IBM技术中心,HUST Vector vc = new Vector();,华中科技大学IBM技术中心,HUST Vector vc = new Vector(); 消除名称的二义性 使用成员的限定名; 使用环境变量 classpath set classpath= javac classpath MyClass.java java classpath MyClass,华中科技大学IBM技术中心,HUST 这样,我们在使用java.util包中的任何类时,就可以直接使用简单类名。需要注意的是,import语句要么导入
25、一个类,要么导入一个完整包。不能使用通配符标记包的子集或多个包,下面三条语句均无法通过编译: import java.applet.A*; import java.*.*; import java.*.io;,华中科技大学IBM技术中心,HUST In Server.java add: package mygame.server;: In Utilities.java add: package mygame.shared;,华中科技大学IBM技术中心,HUST public static void main (String args) ExampleOfException eoe = new
26、ExampleOfException(); eoe.methodA(); System.out.println(Program finished.); void methodA() methodB(); void methodB() methodC(); void methodC() for (int i=0; i4; i+) System.out.println (linesi); ,The first line The second line The last line Exception in thread main java.lang.ArrayIndexOutOfBoundsExce
27、ption: 3 at ExampleOfException.methodC(ExampleOfException.java:16) at ExampleOfException.methodB(ExampleOfException.java:12) at ExampleOfException.methodA(ExampleOfException.java:9) at ExampleOfException.main(ExampleOfException.java:6),华中科技大学IBM技术中心,HUST ,华中科技大学IBM技术中心,HUST public static void main (
28、String args) ExampleOfException eoe = new ExampleOfException(); eoe.methodA(); System.out.println(Program finished.); . void methodC() for (int i=0; i4; i+) try System.out.println (linesi); catch (ArrayIndexOutOfBoundsException e) System.out.println(Re-setting Index Value); ,华中科技大学IBM技术中心,HUST final
29、ly 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 iArray=new int4; iArray10=3; ,华中科技大学IBM技术中心,HUST catch( ArrayIndexOutOfBoundsException e ) System.out.println(Catch +e.getMessage(); catch( Exce
30、ption 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 ); Proc( 2 ); ,程序运行结果: - In Situation0 - no Exception caught in Proc finally - In Situation1 - Catch java.lang.ArithmeticException: / by zero in
31、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 iArray=new int4; iArray10=3; ,华中科技大学IBM技术中心,HUST Proc(1); catch(ArrayIndexOutOfBoundsException e) System.out.println(Catch +e); finally System.out.
32、println(in Proc finally); ,程序运行结果: - In Situation0 - no Exception caught - In Situation1 - Catch java.lang.ArrayIndexOutOfBoundsException:10 in Proc finally,华中科技大学IBM技术中心,HUST MyException( int a ) detail = a; public String toString( ) return MyException +detail; ,华中科技大学IBM技术中心,HUST if( a10 ) throw n
33、ew MyException(a); System.out.println(normal exit); public static void main( String args ) try compute( 1 ); compute( 20 ); catch( MyException e ) System.out.println(Caught +e); ,程序运行结果: called compute(1)normal exitcalled compute(20)Caught MyException 20,华中科技大学IBM技术中心,HUST if (n1) throw new NumberFo
34、rmatException(); catch (NumberFormatException e) System.out.println(输入范围错误!); 代码2: int n = InputReader.inputInteger(请输入一个整数); if (n1) System.out.println(输入范围错误!);,代码1采用了异常处理方式;代码2则通过对用户输入的分析避免了异常的使用,提高了代码效率。,华中科技大学IBM技术中心,HUST String line = null; try input = new RandomAccessFile(named, “r”); while (
35、line = input.readLine() != null System.out.println(line); return; finally if (input != null) input.close(); ,第八章 线程,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random() * 5000 ); System.out.println( Name: + getName() + ; sleep: + sleepTi
36、me ); public void run() try System.out.println( getName() + going to sleep ); Thread.sleep( sleepTime ); catch ( InterruptedException ie ) System.err.println( ie.toString() ); System.out.println( getName() + done sleeping ); ,华中科技大学IBM技术中心,HUST thread1 = new PrintThread( thread1 ); thread2 = new Pri
37、ntThread( thread2 ); thread3 = new PrintThread( thread3 ); thread4 = new PrintThread( thread4 ); System.out.println( nStarting threads ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.out.println( Threads startedn ); ,华中科技大学IBM技术中心,HUST private int sleepTime; public Prin
38、tRunnable( String name ) = name; sleepTime = (int) ( Math.random() * 5000 ); System.out.println( Name: + name + ; sleep: + sleepTime ); public void run() try System.out.println( name + going to sleep ); Thread.sleep( sleepTime ); catch ( InterruptedException ie ) System.err.println( ie.toS
39、tring() ); System.out.println( name + done sleeping ); ,华中科技大学IBM技术中心,HUST thread1 = new PrintRunnable ( thread1 ); thread2 = new PrintRunnable ( thread2 ); thread3 = new PrintRunnable ( thread3 ); thread4 = new PrintRunnable ( thread4 ); System.out.println( nStarting threads ); new Thread(thread1).
40、start(); new Thread(thread2).start(); new Thread(thread3).start(); new Thread(thread4).start(); System.out.println( Threads startedn ); ,华中科技大学IBM技术中心,HUST import java.awt.Graphics; import java.util.Date; public class ClockApplet extends Applet implements Runnable private Thread clockThread = null;
41、public void start() if (clockThread =null) clockThread = new Thread(this); clockThread.start(); public void paint(Graphics g) Date date = new Date(); g.drawString(date.toString(), 5, 10); ,华中科技大学IBM技术中心,HUST try Thread.sleep(1000); catch (InterruptedException e) ,华中科技大学IBM技术中心,HUST 除非更高优先级的抢占CPU,或者,
42、华中科技大学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.out.println(getName() + is running.); ,华中科技大学IBM技术中心,HUST PriorityTest test2 = new PriorityTest(Lower priority thread); test2.setPriority(4); te
43、st1.start(); test2.start(); 从运行结果上看,低优先级的线程也有执行的机会。,华中科技大学IBM技术中心,HUST public int get() return this.data; public void put(int data) this.data = data; ,华中科技大学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 p
44、ut:” + i); try sleep(int)(Math.random()*100); catch (InterruptedException e) ,华中科技大学IBM技术中心,HUST public Consumer(CubbyHole res) this.res = res; public void run() for (int i=0; i10; i+) System.out.println(“Consumer get:” + res.get(); try sleep(int)(Math.random()*100); catch (InterruptedException e) ,
45、华中科技大学IBM技术中心,HUST Producer pro = new Producer(res); Consumer con = new Consumer(res); pro.start(); con.start(); ,华中科技大学IBM技术中心,HUST private boolean getable; public synchronized int get() while (getable = false) try wait(); catch (InterruptedException e) getable = false; notifyAll(); return this.dat
46、a; ,华中科技大学IBM技术中心,HUST catch (InterruptedException e) this.data = data; getable = true; notifyAll(); ,华中科技大学IBM技术中心,HUST synchronized b() a(); ,华中科技大学IBM技术中心,HUST Thread thread1 = new Thread(myGroup, “thread one”); Thread thread2 = new Thread(myGroup, “thread two”);,华中科技大学IBM技术中心,HUST class Copy pri
47、vate 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); len = in.read(buf); public void cat(String fsrc, String fdest) try InputStream in = new FileInputStream(fsrc); OutputStream out = new FileOut
48、putStream(fdest, true); copy(in, out); out.close(); in.close(); catch (IOException ex) System.err.println(ex); ,华中科技大学IBM技术中心,HUST c.cat(“in.dat”,”out.dat”); ,华中科技大学IBM技术中心,HUST publicclassByteArrayOutputStreamTest publicstaticvoidmain(Stringargs) try ByteArrayOutputStreambaos=newByteArrayOutputStre
49、am(); 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(
50、timeelapsed(milliseconds)=+(stop-start); catch(FileNotFoundExceptionfnfe) System.out.println(fnfe.getMessage(); catch(IOExceptionioe) System.out.println(ioe.getMessage(); ,华中科技大学IBM技术中心,HUST PipedOutputStream(PipedInputStream pis); 通过各自的connect()方法连接: 在类PipedInputStream中,connect(PipedOutputStream po
51、s); 在类PipedOutputStream中,connect(PipedInputStream pis);,华中科技大学IBM技术中心,HUST PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(pis); System.out.println(PipedInputStream); try pos.write(dataA); pos.write(dataB); System.out.println(byte)pis.read(); System.out.p
52、rintln(byte)pis.read(); finally pis.close(); pos.close(); ,华中科技大学IBM技术中心,HUST public class ReadLineTest public static void main(String args) try BufferedReader br = new BufferedReader(new FileReader(args0); String line = null; int i = 0; while(line = br.readLine() != null) i+; System.out.println(i +
53、 : + line); br.close(); catch(Exception e) e.printStackTrace(); ,华中科技大学IBM技术中心,HUST publicclassFileTest publicstaticvoidmain(Stringargs)throwsIOException RandomAccessFileraf=newRandomAccessFile(foo.txt,rw); try Writerout= newOutputStreamWriter(newFileOutputStream(raf.getFD(),UTF-8); out.write(HelloW
54、orld!); out.flush(); raf.seek(6); out.write(Java); out.flush(); finally raf.close(); ,第十章 图形用户界面,华中科技大学IBM技术中心,华中科技大学IBM技术中心,HUST import javax.swing.*; public 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); button = new JButton(Im JButton)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年人教版高三下册政治期中试卷(附答案及解析)
- 2026团员入团考试题型及答案
- 2026十九报告考试题目及答案
- 2025年3月翻译专业资格(水平)考试(CATTI)三级口译试题与答案
- 2026年人工智能行业创新报告及未来五至十年行业分析
- 医学26年:妊娠心血管疾病常见误区 心内科查房
- 2026年应急救援应急救援公众参与创新报告
- 医学26年:TACE治疗肝癌进展 查房课件
- 广东省广州市海珠区南武中学2021-2022学年七年级上学期期末历史试题(含答案)
- 2026年无人系统行业报告
- 鲁迅《孤独者》解读大纲
- 预防患者交叉感染措施
- DB45∕T 2362-2021 城镇排水管渠运行维护技术规程
- 呼吸机相关肺炎院感防控体系构建
- 大健康连锁店商业计划书
- 2024广西金融职业技术学院辅导员招聘笔试真题
- 井下煤矿爆破方案(3篇)
- 校园消防设施改造项目可行性研究报告
- JG/T 252-2015建筑用遮阳天篷帘
- CJ/T 511-2017铸铁检查井盖
- 幼儿园项目式课程教学培训
评论
0/150
提交评论