电子商务技术基础第4章常用实例类

上传人:xia****ian 文档编号:245075458 上传时间:2024-10-07 格式:PPT 页数:35 大小:1.35MB
返回 下载 相关 举报
电子商务技术基础第4章常用实例类_第1页
第1页 / 共35页
电子商务技术基础第4章常用实例类_第2页
第2页 / 共35页
电子商务技术基础第4章常用实例类_第3页
第3页 / 共35页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,P,o,w,e,r,B,a,r,中国专业,PPT,设计交流论坛,第,4,章,常用实例类,白宏斌,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,类两个静态常量,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),try,test();,catch(Exception e),System.out.println(e.getMessage();,static void test() throws Exception,throw new IndexOutOfBoundsException(java);,4.4 异常类,使用自定义异常类,自定义继承,Exception,的异常类,并规定哪些方法产生这样异常,4.4 异常类,class MyException extends Exception,String message;,MyException(int n),message=n+,不是正数,;,public String getMessage(),return getClass()+:+message;, ,class Counter,public void PingFangGen(int n) throws MyException,if(n0),MyException ex=new MyException(n);,throw(ex); ,double number=Math.sqrt(n);,System.out.println(n+,的平方根,:+number);, ,public class SelfDefineException,public static void main(String args),Counter c=new Counter();,try,c.PingFangGen(28);,c.PingFangGen(-8);,catch(MyException e),System.out.println(e.getMessage();,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!