欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > PPT文档下载  

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

    • 资源ID:6003424       资源大小:1.35MB        全文页数:35页
    • 资源格式: PPT        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    电子商务技术基础第4章常用实例类.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 Catpublic String category=猫科动物;public String toString()return category;class Dogpublic String category=犬科;,public class DemoToStringpublic 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 DemoTryCatchpublic static void main(String args)int n=0,m=0;trym=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();,

    注意事项

    本文(电子商务技术基础第4章常用实例类.ppt)为本站会员(牧羊曲112)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开