电子商务技术基础第4章常用实例类.ppt_第1页
电子商务技术基础第4章常用实例类.ppt_第2页
电子商务技术基础第4章常用实例类.ppt_第3页
电子商务技术基础第4章常用实例类.ppt_第4页
电子商务技术基础第4章常用实例类.ppt_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

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

文档简介

第4章,常用实例类,白宏斌89419727,4.1 String类,字符串常量和变量都是一个对象,由java.lang包中的String类来创建。 字符串常量 “你好!” “I am happy!” 字符串变量 String name;,4.1 String类,创建字符串变量 使用字符串常量创建字符串对象 String tom=“we are students“; 使用String类的构造方法创建字符串对象 String s=new String(“we are students“); 用一个已创建的字符串创建另一个字符串对象 String tom=String(s);,4.1 String类,创建字符串变量(续) 用一个字符数组a 创建一个字符串对象 格式:String (char a) char a =b,o,y; String s=new String(a); 提取字符数组a 中的一部分字符创建一个字符串对象 格式:String(char a,int startIndex,int count) char a=s,t,b,u,s,n; String s=new String(a,2,3);,4.1 String类,String类常用方法 1. public int length() 用途:获取字符串长度,String s=“How are you”; int n=s.length();,4.1 String类,String类常用方法 2. public boolean equals(String s) 用途:比较当前字符串对象与参数指定的字符串s的字符串内容是否相同,String tom=new String(“how are you”); String jerry=new String(“how are you”); Boolean b1=tom.equals(jerry); Boolean b2=tom=jerry;,equals()方法和“=”区别: equals判断两个对象的字符串内容是否一致。 “=”判断两个对象的引用是否一致。,4.1 String类,String类常用方法 3. public boolean contains(String s) 用途:判断当前字符串对象是否含有参数指定的字符串s,String tom=new String(“how are you”); Boolean b=tom.contains(“are”);,4.1 String类,String类常用方法 4. public boolean startsWith(String s) public boolean endsWith(String s) 用途:判断当前字符串对象的前缀或后缀是否是参数指定的字符串s,String tom=new String(“how are you”); Boolean b1=tom.startsWith(“ho”); Boolean b2=tom.endsWith(“u”);,4.1 String类,String类常用方法 5. public int indexOf (String s) 用途:从当前字符串的头开始检索字符串s,并返回首次出现s的位置。如果没有检索返回-1 。 public int indexOf(String s ,int startpoint ) 用途:返回指定字符在此字符串中第一次出现处的位置,String tom=“I am a good cat”; tom.indexOf(“a”); tom.indexOf(“a”,7);,4.1 String类,String类常用方法 6. public String substring(int startpoint) 用途:获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到最后所得到的字符串。 public String substring(int start ,int end) 用途:从当前字符串的start处截取到end处所得到的字符串,但不包括end处所对应的字符。,String tom=“I love tom”; String s=tom.substring(2,5);,4.1 String类,String类常用方法 7. public String trim() 用途:字符串对象去掉前后空格。,4.1 String类,字符串转换成基本数据类型 相应类的对应方法: public static int parseInt(String s) public static byte parseByte(String s) public static short parseShort(String s) public static long parseLong(String s) public static float parseFloat(String s) public static double parseDouble(String s),如: String s=“1234”; int x=Integer.parseInt(s);,4.1 String类,基本数据类型转换成字符串 String 类的对应的方法: public String valueOf(byte n) public String valueOf(int n) public String valueOf(long n) public String valueOf(float n) public String valueOf(double n),如: String str=String.valueOf(123.45678);,4.1 String类,对象的字符串表示 所有的类是Object类的子类或间接子类; Object类有一个public方法toString(),用于获得该对象的字符串表示。 若一个类重写toString()方法,则按照重写的方式执行,若没有重写toString()方法,则得到的是对象的字符串表示: 类名对象哈希码,4.1 String类,import java.util.Date; class Cat public String category=“猫科动物“; public String toString() return category; class Dog public String category=“犬科“; ,public class DemoToString public static void main(String argus) Date date=new Date(); Cat garfield=new Cat(); Dog odie=new Dog(); System.out.println(date.toString(); System.out.println(garfield.toString(); System.out.println(odie.toString(); ,4.2 Date类,定义在java.util包中 用于操作时间变量,4.2 Date类,构造函数 public Date() 获取本地当前时间。 Date now=new Date(); public Date(long time) time表示相对1970年1月1日0点(GMT)的毫秒数 Date now=new Date(System.currentTimeMillis();,System类的public static long currentTimeMillis()方法返回系统时间与1970年1月1日0点(GMT)之间的时间差(以毫秒为单位测量),4.2 Date类,格式化时间 Date默认时间格式不一定符合应用需求 Tue Jan 17 16:57:39 CST 2012 使用DateFormat的子类SimpleDateFormat来实现日期的格式化 1. public SimpleDateFormat(String pattern) 2. SimpleDateFormat.format(Date date),pattern 日期模式,美国中部标准时间,4.2 Date类,日期模式(pattern) y或yy表示用2位数字输出年份;yyyy表示用4位数字输出年份。 M或MM表示用2为数字或文本输出月份,如果想用汉字输出月份,pattern中应连续包含至少3个M,如:MMM。 d或dd表示用2位数字输出日。 H或HH表示用两位数字输出小时。 m或mm表示用两位数字输出分。 s或ss表示用两位数字输出秒。 E或EE表示用字符串输出星期。,pattern中的英文字符要用” ”转义字符括起。 如:pattern=“Time:yyyy-MM-dd”,4.2 Date类,Date nowTime=new Date(); System.out.println(“现在的时间:“+nowTime); SimpleDateFormat matter_eng=new SimpleDateFormat(“BeijingTime yyyy-MM-dd“); System.out.println(“现在的时间:“+matter_eng.format(nowTime); SimpleDateFormat matter_chn=new SimpleDateFormat(“北京时间 yyyy-MM-dd HH:mm:ss(a)(EE)“); System.out.println(“现在的时间:“+matter_chn.format(nowTime);,4.3 Math类,定义在java.lang包中 Math类两个静态常量 E =2.7182828284590452354 PI=3.14159265358979323846,4.3 Math类,Math类的常用方法 public static long abs(double a) public static double max(double a,double b) public static double min(double a,double b) public static double random() public static double pow(double a,double b) public static double sqrt(double a) public static double log(double a) public static double sin(double a) public static double asin(double a),产生一个0到1之间的随机数(不包括0和1),4.3 Math类,4.4 异常类,异常,指程序运行时可能出现一些若不进行不处理就会造成系统终止运行的错误,如除数为0、数组下标越界、文件找不到等。 异常处理,指为了加强程序的健壮性,对可能出现的异常作出相应处理的操作。 原理:当程序运行出现异常时,Java运行环境就用异常类Exception或其子类创建一个异常对象,并等待处理。,4.4 异常类,try-catch语句 将可能出现的异常操作放在try-catch语句的try部分。将异常处理语句放到catch语句。 当try部分中的某个语句发生异常后,try部分将立刻结束执行,而转向执行相应的catch部分,然后再执行catch语句以后的部分。 如: try 包含可能发生异常的语句 catch(ExceptionSubClass e) 异常处理语句 ,try-catch语句可以由几个catch语句组成,分别处理相应的异常。,4.4 异常类,public class DemoTryCatch public static void main(String args ) int n=0,m=0; try m=Integer.parseInt(“8888“); n=Integer.parseInt(“12s3a“); System.out.println(“我没有机会输出“); catch(Exception e) System.out.println(“发生异常“); n=123; System.out.println(“m=“+m+“,n=“+n); ,4.4 异常类,try-catch-finally语句 finally语句指定无论是否异常处理,都要执行所指定的语句,为程序提供统一出口,用于清除资源,如关闭数据连接,关闭文件等。 如: try 包含可能发生异常的语句 catch(ExceptionSubClass e) 处理语句 finally 清理资源 ,4.4 异常类,RuntimeException,IOException,EOFException,FileNotFoundException,ArithmeticException,NullPointerException,IndexOutOfBoundsException,VirtualMachineError,AWTError,OutOfMemoryError,StackOverflowError,Error,Exception,Throwable,Object,4.4 异常类,常见异常 IOException:输入输出异常 ArithmeticException:数学异常 如: int a=12 / 0; ArrayIndexOutOfBoundsException:下标越界异常 如: int array=new int4; array7=1; NullPointerException:空指针异常 如: Date d= null; System.out.println(d.toString(); ClassCastException:类型转换异常 如: Animal animal=new Dog(); Cat cat=(Animal)animal;,4.4 异常类,访问异常信息常用方法 getMessage() 返回String类型的异常信息 printStackTrace() 打印跟踪方法调用栈获取异常信息 toString() 返回类名+getMessage()内容,4.4 异常类,throws 声明方法可能抛出异常 出现在方法头 不在当前方法处理该异常,而在调用该方法的代码中处理 对于可能产生异常(RuntimeException及其子类除外,可由Java虚拟机自动捕获)的方法,如果方法内部不通过try结构处理异常,则必须通过throws抛出可能的异常 throw 抛出具体异常 出现在方法体 若在本方法中处理异常,需要使用try结构;若在上级代码中处理异常,则需要在方法头配合使用throws声明要抛出的异常,4.4 异常类,public class DemoThrows public static void main(String args) try new DemoThrows().test(); catch(Exception e) e.printStackTrace(); void test() throws StringIndexOutOfBoundsException String str=“java“; for (int i=0;i=str.length();i+) System.out.println(str.substring(i,i+1); ,4.4 异常类,class DemoThrow public static void main(String args) tr

温馨提示

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

评论

0/150

提交评论