Java语言程序设计-复习.ppt

上传人:牧羊曲112 文档编号:6510205 上传时间:2023-11-07 格式:PPT 页数:78 大小:281KB
返回 下载 相关 举报
Java语言程序设计-复习.ppt_第1页
第1页 / 共78页
Java语言程序设计-复习.ppt_第2页
第2页 / 共78页
Java语言程序设计-复习.ppt_第3页
第3页 / 共78页
Java语言程序设计-复习.ppt_第4页
第4页 / 共78页
Java语言程序设计-复习.ppt_第5页
第5页 / 共78页
点击查看更多>>
资源描述

《Java语言程序设计-复习.ppt》由会员分享,可在线阅读,更多相关《Java语言程序设计-复习.ppt(78页珍藏版)》请在三一办公上搜索。

1、Java语言程序设计复习,提 纲,Java概述Java基本语法程序的流程控制数组字符串类和对象Java 标准类库Java图形用户界面(不要求)异常Java I/O操作网络编程(不要求)线程(不要求),复习方式,理解PPT中出现的程序,网上练习题,练习上机作业根据复习大纲,有选择的复习,考试题型,选择(20%)判断对错(10%),简答题(20%)阅读程序(30%)编写程序(20%),第1讲 Java概述,Java的发展历程(oakjava)Java技术的含义编程语言(Programming Language)运行平台(Platform)JAVA虚拟机(JVM)Java语言的特点*简单(Simpl

2、e)面向对象(Object oriented)解释性(Interpreted)平台独立和可移植(Architecture neutral&Portable)鲁棒和安全(Robust&Secure)多线程(Multithreaded)分布式(Distributed)高性能(High performance)动态(Dynamic),第1讲 Java概述,Java程序的种类*Java应用程序(Java Application)独立的Java程序程序入口方法:public static void main(String args)分图形界面与文本界面两种Java小应用程序(Java Applet)在W

3、eb浏览器中运行(内嵌Java虚拟机)特定标记 Java Servlet应用程序,第1讲 Java概述,简单的Java程序*import java.lang.System;/可缺省class HelloWorld public static void main(String args)System.out.println(“Hello World!”);步骤:如何编写(建立)、编译和运行?什么是“.java”文件?什么是”.class”文件?,第2讲 Java基本语法,关键字(保留字)*abstract double int strictfp*boolean else interface su

4、per 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基本

5、语法,标识符*-表示成分的名称常量、变量、数据类型、类和方法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);字母(AZ、az)、特殊符号($、_)和数字(09)第1个符号不能为数字不能为关键词、true、false、null区分大小写,第2讲 Java基本语法,数据类型*基本类型(primitive)数字(num

6、ber)整型(integers)字节整数(byte,8 bits):-128 127,0短整数(short,16 bits):-32768 32767,0整数(int,32 bits):-2147483648 2147483647,0长整数(long,64 bits):,0L实型(real numbers):单精度(float,32 bits):,0.0F双精度(double,64 bits):,0.0D字符(char,16-bit Unicode字符):u0000 uffff布尔(boolean):true,false构造/引用类型(reference):数组(Array),类(class)

7、,接口(interface),第2讲 Java基本语法,常量与变量*int i=178;long l=8864L;(8864l)double d1=37.266;double d2=37.266D;(37.266d)double d3=26.77e3;float f=87.363F;(87.363f)char c=d;boolean b1=true;十进制、八进制、十六进制123、023、0 xFEfinal常量,第2讲 Java基本语法,变量定义域*if(.)int i=17;.System.out.println(The value of i=+i);,第2讲 Java基本语法,运算符*算

8、术运算符 加法运算符+“op1+op2”减法运算符-“op1-op2”乘法运算符*“op1*op2”除法运算符/“op1/op2”求模运算符%“op1%op2”计算余数关系运算符大于“op1 op2”大于等于=“op1=op2”小于“op1 op2”小于等于=“op1=op2”等于=“op1=op2”不等于!=“op1!=op2”,第2讲 Java基本语法,运算符*逻辑运算符逻辑与 42&15、43&1、42|15、42 15,第2讲 Java基本语法,运算符*移位运算符左移“op1 op2”无符号右移“op1 op2”int a=42;int aa=a 2;System.out.printl

9、n(“aa=”+aa);条件运算符op1?op2:op3运算符的优先级,第2讲 Java基本语法,运算符*自增运算符(+)、自减运算符(-)“赋值”和“运算”的先后顺序,int i=10;int n=i+%5;,int i=10;int n=+i%5;,i=11,n=0,i=11,n=1,第2讲 Java基本语法,类型转换*隐型类型转换:自动类型转换(系统完成)宽化转换(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;res

10、ult-=l;result%=m;显型类型转换:强制类型转换窄化转换(narrowing conversion)double a=1.5;float b=a;System.out.println(“b=+b);编译:“possible loss of precision”数据精度丢失数据丢失,第3讲 程序的流程控制,条件选择语句*if和if-else语句if语句的嵌套?:例 int x=xc)if(cb)System.out.print(c);else System.out.print(a);,修改配对关系:if(ac)if(cb)System.out.print(c);else System

11、.out.print(a);,默认Java虚拟机:if(ac)if(cb)System.out.print(c);else System.out.print(a);,注意条件,第3讲 程序的流程控制,switch语句*switch(表达式)case 值1:语句序列;break;case 值2:语句序列;break;default:默认语句;,第3讲 程序的流程控制,switch语句*,public class Test public static void main(String args)int month=2,year=2000;int numDays=0;switch(month)cas

12、e 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),第3讲 程序的流程控制,循环控制语句*while循环do-while循环for循环import java.io.IOException;class Test public static void main(String args)throws IOException int i,n,sum=1;System.out.println(“

13、Please input(09):”);n=System.in.read();n-=48;for(i=1;i=n;i+)sum*=i;System.out.println(n+“!=”+sum);,第3讲 程序的流程控制,循环控制语句*循环的嵌套import java.io.IOException;class Test public static void main(String args)throws IOException int n,sum,total=0;System.out.println(“Please input(09):”);n=System.in.read();n-=48;f

14、or(int j=1;j=n;j+)sum=1;for(int i=1;i=j;i+)sum*=i;total+=sum;System.out.println(“各阶乘之和为:”+total);,第3讲 程序的流程控制,跳转语句*continue 结束本次循环break 跳出(中止)循环class Test public static void main(String args)for(int j=1;j 6;j+)if(j=3)break;System.out.print(“j=“+j);System.out.println(“stop”);,第3讲 程序的流程控制,常见的一些算法创建一个应

15、用程序 X、n!。创建一个求和应用程序 1/X。(1/X)!。创建一个求和应用程序 1-1/2+1/3-1/4-.+1/99-1/100分别使用嵌套if语句和switch语句完成判断成绩等级程序。求出100-300之间的所有素数。从键盘输入一批数,统计运算。创建一个应用程序求100-999所有水仙花数。即各位数的立方和与该数相等。如:153=1*1*1+5*5*5+3*3*3教材上的例题、习题,第4讲 数组,一维数组的声明*方法1:类型 数组名;方法2:类型 数组名;注意类型是数组中元素的数据类型(基本和构造类型)数组名是一个标识符数组声明后不能被访问,因为未对数组分配内存空间String a

16、rgs;int a;double amount;char c;一维数组的创建*用new来创建数组为数组元素分配内存空间,并对数组元素进行初始化int i=new int3;,一维数组的排序*(冒泡、选择),第4讲 数组,二维数组的声明*类型 数组名,例 int a;二维数组的创建*方法1:直接分配空间(new)int a=new int23;a00 a01 a02a10 a11 a12方法2:从最高维开始,为每一维分配空间int c=new int2;c0=new int4;c1=new int3;c00 c01 c02 c03c10 c11 c12,第4讲 数组,二维数组*class Tes

17、t public static void main(String args)int a=new int33;a00=1;a11=1;a22=1;System.out.println(“数组a:”);for(int i=0;i a.length;i+)for(int j=0;jai.length;j+)System.out.print(aij+“”);System.out.println();,第4讲 数组,二维数组的最高维*int a=1,2,3,3,4,5;a00=1 a01=2 a02=3a10=3 a11=4 a12=5 String cartoons=“Flint”,Fred“,Wim

18、“,Pebbles“,Dino,Rub“,Barn“,Bet,Bam,Jet“,Geo“,Jane“,Elroy“,Judy“,Rosie,Sco“,Sco“,Shag“,Velma“,Fred“,Dap;,第4讲 数组,数组的界限*起点和终点数组的长度:数组名.length起点:数组名0终点:数组名length-1int i=4,56,78,9,34;i.length 5i0 4ilength-1=i434ia 若a4 则?,第5讲 字符串,java.lang.String类字符串判断字符串相等*,String s1=java语言;String s2=JavA语言;System.out.pr

19、intln(s1.equals(s2);System.out.println(s1.equalsIgnoreCase(s2);System.out.println(pareTo(s2);System.out.println(pareToIgnoreCase(s2);,第5讲字符串,其他*,String s=java语言;System.out.println(s.length();System.out.println(s.substring(0,4);System.out.println(s.substring(4);System.out.println(s.charAt(0);,System.

20、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);,第7讲字符串,StringBuffe

21、r 类字符串变量常见方法(append、length)等,第6讲 类和对象,面向对象编程的特点 用客观世界中描述事物的方法来描述程序中要解决的问题万事万物都是对象程序便是成堆的对象,彼此通过消息的传递,请求其他对象进行工作五个基本概念*对象 状态:指对象本身的信息行为:实现对信息的访问消息对象之间的交互和通信是通过相互间发送消息来实现类类是对象的模板(template)/抽象一个对象是类的一个实例(instance)继承-树型结构多态性-不同的子类中同样的方法有不同的表现形式,同一子类中相同方法名的不同方法,第6讲 类和对象,类的定义格式*类修饰符 class 类名extends 父类名 im

22、plements 接口名 t 类型成员方法1(参数1,参数2,)方法体;类型成员方法2(参数1,参数2,)方法体;,第6讲 类和对象,类的定义格式类修饰符 class 类名 extends 父类名 implements 接口名 常见的类修饰符*public:(无任何限制)无修饰:(仅仅能被同一个包中的其他类引用)abstract:(声明该类不能被实例化,抽象类)final:(声明该类不能有子类)继承与实现*extends:继承的关系implements:实现哪些接口(interface)的方法,实现多重继承,第6讲 类和对象,类成员的访问修饰符*publicprivateprotected无修

23、饰staticfinal,第6讲 类和对象,4.2.1 类的访问控制,第6讲 类和对象,4.2.2 类成员的访问控制,第6讲 类和对象,静态变量和静态方法*类的成员(变量/方法),独立于类的对象,可以直接根据类名调用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();,第6讲 类和对象,成员方法的访问*-定义、调用、返回、形参、实参,class Test publ

24、ic static void main(String args)double d_product;Area myArea;myArea=new Area();d_product=myArea.product();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讲 类和对象(续),向方法传递参数传递简单变量传递对象(

25、教材109),第6讲 类和对象,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();,第6讲 类和对象,方法重写*子类重写(重新定义)父类的方法及上转型问题,

26、class Father void display();,class Son extends Father void display();,Father f=new Father();f.display();,Son s=new Son();s.display();,Father s=new Son();s.display();,第6讲 类和对象(续),对象的创建*创建对象/实例化对象new Apple a=new Apple();-理解含义对象的实例化通过构造方法(constructor)来实现构造方法的名字与类名相同构造方法没有返回值构造方法可以有多个,构成方法的重载(overload),

27、第5讲 类和对象(续),对象的创建*,第5讲 类和对象(续),默认构造方法*例 class Apple int color;Apple a=new Apple();对象实例的判断*(null)例 Apple a;if(a=null)System.out.println(“Day dream”);,第5讲 类和对象(续),对象的引用*通过对象引用对象的成员变量和成员方法,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 wid

28、th()return a;int height()return h;void set(int x,int y)a=x;h=y;,第5讲 类和对象(续),对象的释放*周期性地释放不再被引用的对象,自动完成自动垃圾回收,第5讲 类和对象(续),继承*父类与子类的关系(extends)子类可调用父类的方法和变量,子类可增加父类中没有的方法和变量子类可重新定义父类的静态/实例变量子类可重新定义父类的静态/实例方法,注意调用,第5讲 类和对象(续),抽象类*一个未完成的类,抽象类不能被实例化子类继承抽象类时,必须重写抽象方法,否则仍为抽象类仅仅抽象类可以包含抽象方法(abstract methods)抽

29、象方法:仅仅申明了方法,但未实现有访问修饰词返回值类型方法名参数列表无方法体,第5讲 类和对象(续),抽象类*,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();,第5讲 类和对象(续),接口*接口是对

30、abstract类的进一步扩展接口中的方法都是未实现的(类似于抽象方法),目的是在实现接口的类之间建立一种协议接口中的变量都是常量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;,第5讲 类和对象(续),interface Figure double half=0.5,pi=3.14159;void parameter();void area();,class T

31、riangle 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

32、(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();,第5讲 类和对象(续),This、super的使用public class XX int var;XX(int var)this(Welcome);XX(String s)this();System.out.println(s);XX()Sy

33、stem.out.println(Good-bye);public static void main(String args)XX t=new XX(1);,第6讲 类和对象,多态性*,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()S

34、ystem.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讲 类和对象(续),包*使Java类更容易发现和使用防止命名冲突进行访问控制 层次结构,package org.jalpha;public class HelloWorld.,第7讲 Java 标准类库,Object类

35、toString()Equals()java.lang.System类exit()-程序退出System.inSystem.outSystem.err,第7讲 Java 标准类库,包装类型简单类型与包装类型转换字符串转换为基本类型-Integer.parseInt()基本类型和字符串(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

36、s5=Integer.toString(i,16);String s6=Integer.toBinaryString(i);String s7=Integer.toHexString(i);String s8=Integer.toOctalString(i);,12312311110111737b11110117b173,第8讲 Java 图形用户界面(不要求),Java基本图形使用Graphics或Graphics2D坐标定义画线、画圆、矩形等Font类Color类,第8讲 Java 图形用户界面,组件(构件)分类*构成图形用户界面的元素容器(Container)组件:可包含其他组件顶层容器

37、:JApplet,JDialog,JFrame,JWindow一般用途容器:JPanel,JScrollPane特定用途容器:InternalFrame非容器组件:必须要包含在容器中JLabel,JButton,JCheckbox,JList,JComboBox,第8讲 Java 图形用户界面,布局管理*决定组件在界面中所处的位置和大小 六种布局管理器(Layout Manager)FlowLayout(java.awt.FlowLayout)所有组件从左往右排成一行 一行排满后转到下一行从左往右排GridLayout(java.awt.GridLayout)将空间划分为由行和列组成的网格单元

38、,每个单元放一个组件,网格单元大小相同(宽度和高度)指定行数和列数BorderLayout(java.awt.BorderLayout)BorderLayout是默认的布局管理器 上北、下南、左西、右东、中,第8讲 Java 图形用户界面,Java事件机制(事件委托模型)事件源事件监听器,第8讲 Java 图形用户界面,常见的java组件JApplet(运行过程)JFrameJPanelJLabelJButtonJTextFieldJTextAreaJComboBoxJList,第8讲 Java 图形用户界面,常见的组件使用按钮与标签文本框与文本域列表框与标签、按钮下拉列表框与按钮、标签会简单

39、编程(包括在JFrame中),第9讲 异常,Java语言利用异常来使程序获得处理错误的能力(error-handling)异常类层次,常见的几种异常异常处理机制异常处理器(exception handler)trycatchfinally异常的抛出(throw)异常的抛弃(throws),一个try语句必须带有至少一个catch语句块或一个finally语句块,第9讲 异常,import java.io.IOException;class Test static char getChar()throws IOException char c=(char)System.in.read();ret

40、urn c;public static void main(String args)try char c=getChar();System.out.println(c);catch(IOException e)System.out.println(e);,第10讲 Java I/O操作,I/O来源*控制台(console,如DOS窗口)打印/读入文件(file)读/写网络接口(TCP/UDP端口)读/写以流(stream)的方式对数据进行操作*两种流的定义(读取信息的基本数据单位)*字节流(byte stream):一个字节(8-bit)一个字节读/写字符流(character stream)

41、:一个字符一个字符读/写(具有特定字符编码的数据),第10讲 Java I/O操作,读/写流的一般流程*读(Reading)open a stream/打开读出流while more information/判断 read information/读close the stream/关闭流写(Writing)open a stream/打开写入流while more information/判断 write information/写close the stream/关闭流,第10讲 Java I/O操作,字节流InputStream、OutputStream过滤流读写方法字符流Reader、

42、Writer过滤流读写方法,第10讲 Java I/O操作,文件读/写操作*FileInputStream/FileOutputStream(字节流)FileReader/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(“resul

43、t.txt”);int c;while(c=in.read()!=-1)out.write(c);in.close();out.close();,第10讲 Java I/O操作,java.io.File类*文件和目录的路径名目录管理,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);,第10讲

44、 Java I/O操作,java.io.File类*文件属性操作,import java.io.*;import java.util.*;public class AttrDemo2 public static void main(String args)throws IOException File testfile=new File(testfile2);testfile.delete();testfile.createNewFile();long modtime=testfile.lastModified();System.out.println(last modification ti

45、me#1=+new Date(modtime);testfile.setLastModified(0);modtime=testfile.lastModified();System.out.println(last modification time#2=+new Date(modtime);,第10讲 Java I/O操作(不要求),java.io.RandomAccessFile类*读写操作在同一个类中完成,须在构造对象时指定参数通过移动文件指针(file pointer)在文件的指定位置进行读写操作,第11讲 网络编程(不要求),为了实现客户端和服务端的通信,Java语言使用了基于套接字

46、的网络通信方式。这种套接字的网络通信方式分为流套节字和数据报套节字两种。流套节字网络通信方式使用的协议是传输控制协议TCP(Transmission Control Protocol),它提供一种面向连接的高可靠性的传输,利用它通信时,首先需要建立连接才能进行通信。数据报套节字网络通信方式使用的协议是UDP(User Datagram Protocol),它是一种无连接、高效率,但不十分可靠的协议,无须建立连接就可以进行。,第11讲 网络编程,Java语言通过软件包实现三种网上通信模式:URL通信模式(在使用URL通信模式时,它的底层仍使用流套接字方式);Socket通信模式(也称为流套接字通

47、信模式);Datagram通信模式(也称为数据报套接字通信模式)。,第11讲 网络编程,.Socket类*表示TCP连接的客户方(Client),和谁连接public Socket(String host,int port)throws UnknownHostException,IOException具有双向流public InputStream getInputStream()throws IOExceptionpublic OutputStream getOutputStream()throws IOException,第11讲 网络编程(不要求),.ServerSocket类*TCP连接

48、的服务器方(Server),监听端口public ServerSocket(int port)throws IOException接收连接请求public Socket accept()throws IOExceptionListens for a connection to be made to this socket and accepts it.The method blocks(阻塞)until a connection is made排队的服务类型Socket 通信机制(基本原理),第12讲 线程(不要求),线程(Thread)*线程是进程中一个“单一的连续控制流程”/执行路径一个进

49、程可拥有多个并行的线程一个进程中的所有线程共享相同的内存单元/内存地址空间可以访问相同的变量和对象通信、数据交换、同步操作用途从提高程序执行效率的考虑最大限度地利用CPU效率应用在多处理器系统(SMP-Symmetric MultiProcessing)执行异步或后台处理等Client/Server设计中的服务器端,如每个用户请求建立一个线程,实现多个请求同时并发图形用户界面(GUI)的设计中提高事件响应的灵敏度,第12讲 线程,线程创建的两种方式*“Subclassing Thread and Overriding run”继承java.lang.Thread类,重写run()方法“Implementing the Runnable Interface”实现java.lang.Runnable接口线程的同步*synchronizedwait()/notifyAll()/notify(),

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号