




已阅读5页,还剩72页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java语言程序设计 复习 周敏彤 2005-11-21 提 纲 nJava概述 nJava基本语法 n程序的流程控制 n数组 n类和对象 n类和对象(续) n类和对象(续) n异常 nJava 2 API规范 n线程 nJava I/O操作 nJava图形用户界面 n网络编程 基本知识 65 应用知识 35 复习方式 n听课件,理解PPT中出现的程序,练习上机作业 n根据复习大纲,有选择的复习 考试题型 n填空(20%),判断对错(20%),选择(10%) n简答题(40%) n改错(10%) 第1讲 Java概述 nJava的发展历程(oakjava) nJava技术的含义 q编程语言(Programming Language) q运行平台(Platform) nJava语言的特点* q简单(Simple) q面向对象(Object oriented) q解释性(Interpreted) q平台独立和可移植(Architecture neutral class HelloWorld public static void main(String args) System.out.println(“Hello World!”); q如何编写、编译和运行? q什么是“.java”文件? q什么是”.class”文件? q什么是Java应用程序? q有哪些固定格式? q哪些是关键词? q如何注释? qimport、System、class的含义? 第2讲 Java基本语法 n关键词* abstract double int strictfp * boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto * protected transient const * if public try continueimplements return void default import short volatile do instanceof static while 注: *当前未被使用 * * 用于Java2 第2讲 Java基本语法 n标识符* q常量、变量、数据类型、类和方法 public class HelloWorld1 public static void main(String args) String message = “Hello World!”; myPrint(message); private static void myPrint(String s) System.out.println(s); q字母(AZ、az)、特殊符号($、_)和数字(09) q第1个符号不能为数字 q不能为关键词、true、false、null q区分大小写 第2讲 Java基本语法 n数据类型* q基本类型(primitive) n数字 (number) q整型 (integers) 字节整数 (byte, 8 bits): -128 127, 0 短整数 (short, 16 bits): -32768 32767, 0 整数 (int, 32 bits): -2147483648 2147483647, 0 长整数 (long, 64 bits): , 0L q实型 (real numbers): 浮点型 (有效位数不同) 单精度(float, 32 bits): , 0.0F 双精度(double, 64 bits): , 0.0D n字符 (char, 16-bit Unicode字符): u0000 uffff n布尔 (boolean): true, false q构造/引用类型(reference): 数组(Array), 类(class), 接口 (interface) 第2讲 Java基本语法 n常量与变量* qint i = 178; qlong l = 8864L; (8864l) qdouble d1 = 37.266; qdouble d2 = 37.266D; (37.266d) qdouble d3 = 26.77e3; qfloat f = 87.363F; (87.363f) qchar c = d; qboolean b1 = true; q十进制、八进制、十六进制 n123、 023、0xFE qfinal变量 第2讲 Java基本语法 n变量定义域* if ( . ) int i = 17; . System.out.println(“The value of i = “ + i); 第2讲 Java基本语法 n运算符* q算术运算符 n加法运算符 + “op1 + op2” n减法运算符 - “op1 - op2” n乘法运算符 * “op1 * op2” n除法运算符 / “op1 / op2” n求模运算符% “op1 % op2” 计算余数 q关系运算符 n大于 “op1 op2” n大于等于 = “op1 = op2” n小于 “op1 op2” n无符号右移 “op1 op2” int a = 42; int aa = a 2; System.out.println(“aa=” + aa); q条件运算符 op1 ? op2 : op3 q运算符的优先级 第2讲 Java基本语法 n运算符* q自增运算符(+)、自减运算符(-) n“赋值”和“运算”的先后顺序 int i = 10; int n = i+%5; int i = 10; int n = +i%5; i = 11, n = 0i = 11, n = 1 第2讲 Java基本语法 n类型转换* q隐型类型转换: 自动类型转换(系统完成) n宽化转换(widening conversion) byte j=60; short k=4; int l=31; long m=4l; long result=0l; result +=j-8; result *=k+2; result /=m+1; result -=l; result %=m; q显型类型转换: 强制类型转换 n窄化转换 (narrowing conversion) double a = 1.5; float b = a; System.out.println(“b=“ + b); 编译: “possible loss of precision” 数据精度丢失数据丢失 第3讲 程序的流程控制 n多个read方法的使用 import java.io.IOException; class Read1 public static void main(String args) throws IOException char c; System.out.println(“Please input a char: “); c = (char)System.in.read(); System.out.println(“Received char=“ + c); import java.io.IOException; class Read2 public static void main(String args) throws IOException byte b = new byte10; System.out.println(“Received number=“ + System.in.read(b); 第3讲 程序的流程控制 n多个read方法的使用 import java.io.IOException; class Read3 public static void main(String args) throws IOException byte b = new byte10; System.out.println(“Input 3 characters: ”); System.out.println(“The number=” + System.in.read(b); System.out.println(“Input 1 character: ”); System.out.println(“The number=” + System.in.read(b, 5, 1); for (int i = 0; i c) if (cb) System.out.print(c); else System.out.print(a); 修改配对关系: if (ac) if (cb) System.out.print(c); else System.out.print(a); 默认Java虚拟机: if (ac) if (cb) System.out.print(c); else System.out.print(a); 第3讲 程序的流程控制 nswitch语句* switch (表达式) case 值1: 语句序列; break; case 值2: 语句序列; break; default: 默认语句; 第3讲 程序的流程控制 nswitch语句* public class Test public static void main(String args) int month = 2, year = 2000; int numDays = 0; switch (month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if (year % 4 = 0) else numDays = 28; break; System.out.println(“Number of Days = “ + numDays); 第3讲 程序的流程控制 n循环控制语句* qwhile循环 qdo-while循环 qfor循环 import java.io.IOException; class Test public static void main(String args) throws IOException int i, n, sum=1; System.out.println(“Please input(09):”); n = System.in.read(); n -= 48; for (i = 1; i 1; j-) for (k=1; k”); 第3讲 程序的流程控制 n跳转语句* q带标号的continue语句 class Test public static void main(String args) iLoop: for (int i=1; i =10) System.out.print(p +“ ”); else System.out.print(p + “ ”); System.out.println(); 第4讲 数组 n一维数组的声明* q方法1: 类型 数组名; q方法2: 类型 数组名; q注意 n类型是数组中元素的数据类型(基本和构造类型) n数组名是一个标识符 n数组声明后不能被访问,因未为数组元素分配内存 空间 qString args; int a; double amount; char c; n一维数组的创建* q用new来创建数组 q为数组元素分配内存空间,并对数组元素进行初 始化 qint i = new int3; 第4讲 数组 n一维数组* class Test public static void main(String args) int i = new int3; float f = new float3; boolean b = new boolean3; char c = new char3; for (int j = 0; j = 0; ) for (int j = 0; j arrayj+1) int temp = arrayj; arrayj = arrayj+1; arrayj+1 = temp; for (int i = 0; i 4 则? 第5讲 类和对象 n面向对象编程的特点 q用客观世界中描述事物的方法来描述程序中要解决的问题 q万事万物都是对象 q程序便是成堆的对象,彼此通过消息的传递,请求其他对 象进行工作 n五个基本概念* q对象 n状态: 指对象本身的信息 n行为: 实现对信息的访问 q消息 n对象之间的交互和通信是通过相互间发送消息来实现 q类 n类是对象的模板(template)/抽象 n一个对象是类的一个实例(instance) q继承-树型结构 q多态性-不同的子类中同样的方法有不同的表现形 式 第5讲 类和对象 n多态性* class Square extends Shape void draw() System.out.println(“Square.draw()“); void erase() System.out.println(“Square.erase()“); class Triangle extends Shape void draw() System.out.println(“Triangle.draw()“); void erase() System.out.println(“Triangle.erase()“); class Shape void draw() void erase() class Circle extends Shape void draw() System.out.println(“Circle.draw()“) ; void erase() System.out.println(“Circle.erase( )“); 第5讲 类和对象 类的定义格式* 类修饰符 class 类名extends 父类名 implements 接口名 类型成员变量1; 类型成员变量2; 类型成员方法1(参数1, 参数2, ) 方法体; 类型成员方法2(参数1, 参数2, ) 方法体; 第5讲 类和对象 n类的定义格式 类修饰符 class 类名 extends 父类名 implements 接口 名 n常见的类修饰符* qpublic: Declares that the class can be used by any class regardless of its package (无任何限制) q无修饰: a class can be used only by other classes in the same package (仅仅能被同一个包中的其他类引用) qabstract: Declares that the class cannot be instantiated (声明该类不能被实例化,抽象类) qfinal: Declares that the class cannot be subclassed (声明该类不能有子类) 第5讲 类和对象 n类的定义格式 类修饰符 class 类名 extends 父类名 implements 接口名 n继承与实现* qextends: 继承的关系 qimplements: 实现哪些接口(interface)的方法 ,实现多重继承 第5讲 类和对象 n类成员的访问修饰符* qpublic qprivate qprotected q无修饰 qstatic qfinal 第5讲 类和对象 n静态变量和静态方法* q类的变量/方法,独立于类的对象,可以直接根据类名调 用 class S static int A = 12, B = 34; static void print() ; class Test public static void main(String args) System.out.println(“A=“ + S.A + “ B=“ + S.B); S.print(); 第5讲 类和对象 n成员方法的访问* class Test public static void main(String args) double d_product; Area myArea; myArea = new Area(); d_product = myAduct(); System.out.println(“myArea的面积是: ” + d_product); class Area Area() ; double width, height; void set(double w, double h) width = w; height = h; double product() return width*height; 第5讲 类和对象 n方法参数的传值(pass by value)* q基本类型与复合类型参数传递的结果不同 第5讲 类和对象 class Example Example() ; int u, v; void compute(int x, int y) int i, j; for (i=1; i=x;i+) j = y+i; System.out.print(j+“ ”); void p( ) u=3; v=2; compute(u, v); System.out.println(); u+=v; v*=u; compute(u, v); public static void main(String args) Example A = new Example(); A.p(); 第5讲 类和对象 n方法重写* q子类重写(重新定义)父类的方法 class Father void display( ) ; class Son extends Father void display() ; Father f = new Father(); f.display(); Son s = new Son(); s.display(); 第6讲 类和对象(续) n对象的创建* q创建对象/实例化对象new qApple a = new Apple(); (创建对象) q对象的实例化通过构造方法(constructor)来实 现 q构造方法的名字与类名相同 q构造方法没有返回值 q构造方法可以有多个,构成方法的重载 (overload) 第6讲 类和对象(续) n对象的创建* public static void main(String args) Square s1 = new Square(); Square s2 = new Square(20, 50); Square s3 = new Square(s1); System.out.println(s1.width() +“ ” +s1.height(); System.out.println(s2.width() +“ ” +s2.height(); System.out.println(s3.width() +“ ” +s3.height(); class Square int a, h; Square() a = 10; h = 20; Square(int x, int y) a = x; h = y; Square(Square s) a = s.width(); h = s.height(); int width() return a; int height() return h; 第6讲 类和对象(续) n默认构造方法* 例 class Apple int color; Apple a = new Apple(); n对象实例的判断*(null) 例 Apple a; if (a = null) System.out.println(“Day dream”); 第6讲 类和对象(续) n对象的引用* q通过对象引用对象的成员变量和成员方法 class Square int a, h; Square () a = 10; h = 20; Square(int x, int y) a = x; h = y; Square(Square r) a = r.width(); h = r.height(); int width() return a; int height() return h; void set(int x, int y) a=x; h =y; 第6讲 类和对象(续) n对象的释放* q周期性地释放不再被引用的对象,自动完成 q自动垃圾回收 第7讲 类和对象(续) n继承* q父类与子类的关系 (extends) q子类可调用父类的方法和变量, 子类可增加父类 中没有的方法和变量 q子类可重新定义父类的静态/实例变量 q子类可重新定义父类的静态/实例方法 第7讲 类和对象(续) n抽象类* q一个未完成的类, 抽象类不能被实例化 q子类继承抽象类时,必须重写抽象方法,否则 仍为抽象类 q仅仅抽象类可以包含抽象方法(abstract methods) q抽象方法: 仅仅申明了方法,但未实现 n有访问修饰词 n返回值类型 n方法名 n参数列表 n无方法体 第7讲 类和对象(续) n抽象类* abstract class Point int x = 1, y = 1; void move(int dx, int dy) x += dx; y += dy; alert(); abstract void alert(); abstract class ColoredPoint extends Point int color; class SimplePoint extends Point void alert() Point p = new SimplePoint(); 第7讲 类和对象(续) n接口* q接口是对abstract类的进一步扩展 q接口中的方法都是未实现的(类似于抽象方法), 目的是在实现接口的类之间建立一种协议 q接口中的变量都是常量 public interface Months int JANUARY=1, FEBRUARY=2, MARCH=3, APRIL=4, MAY=5, JUNE=6, JULY=7, AUGUST=8, SEPTEMBER=9,OCTOBER=10, NOVEMBER=11,DECEMBER=12; 第7讲 类和对象(续) interface Figure double half=0.5,pi=3.14159; void parameter(); void area(); class Triangle implements Figure double b, h; Triangle (double u, double v) b = u; h = v; public void parameter() System.out.println(b + “ “ + h); public void area() System.out.println(half*h*b); class Circle implements Figure double x, y, r; Circle(double u, double v, double m) x=u; y=v; r=m; public void parameter() System.out.println(x+“ “+y+“ “+r); public void area() System.out.println(pi*r*r); Triangle t = new Triangle(2, 3); Circle c = new Circle(4, 5, 6); Figure f = t, c; for (int i =0; i f.length; i+) fi.parameter(); fi.area(); 第7讲 类和对象(续) n包* q使Java类更容易发现和使用 q防止命名冲突 q进行访问控制 n层次结构 package org.jalpha; public class HelloWorld . . . 第8讲 异常 nJava语言利用异常来使程序获得处理错误的 能力(error-handling) n异常处理机制 q异常处理器(exception handler) ntry ncatch nfinally q异常的抛出(throw) 一个try语句必须带有至少一个catch语句块 或一个finally语句块 第8讲 异常 import java.io.IOException; class Test static char getChar() throws IOException char c = (char)System.in.read(); return c; public static void main(String args) try char c = getChar(); System.out.println(c); catch (IOException e) System.out.println(e); 第9讲 Java 2 API规范 njava.lang.System类 q数组复制* class Test public static void main(String args) int a = 2, 4, 6, 8; int b; int c = 1, 3, 5, 7, 9; b = a; System.arraycopy(a, 1, c, 0, 3); System.out.print(“数组a: ”); for (int i = 0; i a.length; i+) System.out.print(ai + “ ”); System.out.println(); System.out.print(“数组b: ”); for (int i = 0; i b.length; i+) System.out.print(bi + “ ”); System.out.println(); System.out.print(“数组c: ”); for (int i = 0; i c.length; i+) System.out.print(ci + “ ”); System.out.println(); 第9讲 Java 2 API规范 njava.lang.System类 q程序退出* public static void main(String args) ; if ( ) System.exit(0); ; 第9讲 Java 2 API规范 n基本类型和字符串(String)之间的转换 int i = 123; String s1 = Integer.toString(i); String s2 = Integer.toString(i, 10); String s3 = Integer.toString(i, 2); String s4 = Integer.toString(i, 8); String s5 = Integer.toString(i, 16); String s6 = Integer.toBinaryString(i); String s7 = Integer.toHexString(i); String s8 = Integer.toOctalString(i); 123 123 1111011 173 7b 1111011 7b 173 第9讲 Java 2 API规范 njava.lang.String类字符串 q判断字符串相等* String s1 = “java语言“; String s2 = “JavA语言“; System.out.println(s1.equals(s2); System.out.println(s1.equalsIgnoreCase(s2); System.out.println(pareTo(s2); System.out.println(pareToIgnoreCase(s2); 第9讲 Java 2 API规范 njava.lang.String类字符串 q其他* String s1 = “java语言“; String s2 = “JavA语言“; System.out.println(s1.length(); System.out.println(s2.length(); System.out.println(s1.substring(0, 4); System.out.println(s1.substring(4); System.out.println(s2.substring(0, 4); System.out.println(s2.substring(4); System.out.println(s1.charAt(0); 第9讲 Java 2 API规范 njava.lang.String类字符串 q其他* String s = “java语言”; System.out.println(s.indexOf(a); System.out.println(s.indexOf(a, 2); System.out.println(s.indexOf(“a”); System.out.println(s.indexOf(“语言”); System.out.println(s.lastIndexOf(a); System.out.println(s.lastIndexOf(v, 1); System.out.println(s.lastIndexOf(“语言”); System.out.println(s.lastIndexOf(“v”, 2); 第10讲 线程 n线程(Thread)* q线程是进程中一个“单一的连续控制流程”/执行路径 q一个进程可拥有多个并行的线程 q一个进程中的所有线程共享相同的内存单元/内存地址空 间可以访问相同的变量和对象通信、数据交换、同步操 作 q用途 n从提高程序执行效率的考虑 q最大限度地利用CPU效率 q应用在多处理器系统(SMP-Symmetric MultiProcessing) n执行异步或后台处理等 qClient/Server设计中的服务器端, 如每个用户请求 建立一个线程,实现多个请求同时并发 q图形用户界面(GUI)的设计中提高事件响应的灵敏度 第10讲 线程 n线程创建的两种方式* q“Subclassing Thread and Overriding run”继承java.lang.Thread类, 重写run()方法 q“Implementing the Runnable Interface”实 现java.lang.Runnable接口 n线程的同步* nsynchronized nwait()/notifyAll()/notify() 第11讲 Java I/O操作 nI/O来源* q控制台(console, 如DOS窗口)打印/读入 q文件(file)读/写 q网络接口(TCP/UDP端口)读/写 n以流(stream)的方式对数据进行操作* n两种流的定义(读取信息的基本数据单位)* q字节流(byte stream): 一个字节(8-bit)一个字节 读/写 q字符流(character stream):一个字符一个字符 读/写(具有特定字符编码的数据) 第11讲 Java I/O操作 n读/写流的一般流程* q读(Reading) nopen a stream /打开读出流 nwhile more information /判断 n read information /读 nclose the stream /关闭流 q写(Writing) nopen a stream /打开写入流 nwhile more information /判断 n write information /写 nclose the stream /关闭流 第11讲 Java I/O操作 n文件读/写操作* qFileInputStream/FileOutputStream (字节流) qFileReader/FileWriter (字符流) import java.io.*; public class CopyBytes public static void main(String args) throws IOException FileInputStream in = new FileInputStream(“original.txt”); FileOutputStream out = new FileOutputStream(“result.txt”); int c; while (c = in.read() != -1) out.write(c); in.close(); out.close(); 第11讲 Java I/O操作 njava.io.File类* q文件和目录的路径名 q目录管理 import java.io.File; public class DirList public static void main(String args) File path = new File(“.“); String list = path.list(); for(int i = 0; i list.length; i+) System.out.println(listi); 第11讲 Java I/O操作 njava.io.File类* q文件属性操作 import java.io.*; import java.util.*; publi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人教(部编版)道德与法治八下6.1国家权力机关 教学设计
- 2025年河北省邯郸市经济技术开发区中考一模数学试题(原卷版+解析版)
- 【CMF】2025中国宏观经济专题报告5GDP增速必要性与可能性
- 儿童应急救护培训课件
- 旅游业客户服务升级
- 2015年高考英语试卷(新课标Ⅰ)(原卷版)
- 2025景观绿化工程设计委托合同书
- 2025建筑工程材料采购销售合同示范文本模板
- 心理健康活动月策划书
- 几种重要病毒的简介
- 河北单招时政试题及答案
- 合理应用抗生素的培训
- 2025年山东省聊城市冠县中考一模地理试题(原卷版+解析版)
- 冯姓姓氏历史起源研究报告
- 小学生安全教育校本教材
- 体检护士礼仪规范
- 3.1伟大的改革开放+课件高中政治统编版必修一中国特色社会主义
- 使用错误评估报告(可用性工程)模版
- 高中英语新课程标准解读课件
- 干部履历表(中共中央组织部2015年制)
- 中国特色社会主义理论体系的形成发展PPT2023版毛泽东思想和中国特色社会主义理论体系概论课件
评论
0/150
提交评论