《包和接口二》PPT课件.ppt_第1页
《包和接口二》PPT课件.ppt_第2页
《包和接口二》PPT课件.ppt_第3页
《包和接口二》PPT课件.ppt_第4页
《包和接口二》PPT课件.ppt_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

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

文档简介

1/,第12讲 包和接口(二),抽象类 File类 String类 StringBuffer类,System类 Math类 Random类,教学目标,理解抽象类的概念; 了解File类、String类、StringBuffer类的用途 掌握File类、String类、StringBuffer类的常用方法 掌握System类的常用方法 会使用Math类的常用方法 会使用Random类的常用方法,抽象类,抽象类的定义与一般类一样都有数据和方法,定义格式与一般类也非常类似只是在定义类的class前增加一个关键字abstract就表示定义一个抽象类,也就说用abstract说明的类称为抽象类。 抽象类不能用来实例化一个对象,它只能被继承。,抽象类示例,public abstract class Person /定义一个抽象类Person String name; int age; public void birth() public abstract void go(); 注意: ()Person类是抽象类不能用new创建它的实例,可以被继承,抽象方法go()只有方法头标志而没有实现,它的实现由子类操作。 ()注意抽象类与接口的关系,File类提供的方法,经常使用File类的方法获取文件本身的一些信息。 (1)文件操作 public String getName()/返回文件对象名,不包含路径名 public String getPath() /返回相对路径名,包含文件名 public String getAbsolutePath() /返回绝对路径名,包含文件名 public String getParent() /返回父文件对象的路径名 public File getParentFile() /返回父文件对象 public long length() /返回指定文件的字节长度 public boolean exists() /判断指定文件是否存在 public long lastModified() /返回指定文件最后被修改的时间,public boolean renameTo(File dest) /文件重命名 public boolean delete() /删除空目录 public boolean canRead() /判断文件是否可读的 public boolean canWrite() /判断文件是否可被写入 (2)目录操作 public boolean mkdir() /创建指定目录,正常true public String list() /返回目录中的所有文件名 public File listFiles() /返回目录中的所有文件对象,File类提供的方法(续),import java.io.*; public class Ex7_6 public static void main(String args) throws IOException String filePath; InputStreamReader is=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(is); System.out.print(“请输入信息:“); filePath=br.readLine(); File fileName=new File(filePath);,判断所输入的信息是代表一个目录,还是一个文件。如果是目录,则输出该目录下的所有文件;如果是一个文件的话,则输出此文件的绝对路径。,File类示例,if(fileName.isDirectory() System.out.println(fileName.getName()+“是一个目录“); System.out.println(“*“); File list= fileName.listFiles(); for(int i=0;ilist.length;i+) System.out.println(listi.getName(); else System.out.println(fileName.getName()+“是一个文件“); System.out.println(“*“); System.out.println(fileName.getAbsolutePath(); ,File类示例(续),String类,String类的构造方法 String类共有7个构造方法: public String(); public String(String s); public String(char c); public String(char c,int startIndex,int count); public String(byte b,int startIndex,int count); public String(byte b) public String(StringBuffer buffer); 例如: String str=new String(“Yangzi River”);,String类的常用方法(1),(1)public int length():求出一个字符串的长度,即求出字符串对象中字符的个数 如:str.length(); /值为? (2)public boolean equals(String s):字符串对象调用String类中的equals方法,比较当前字符串对象的内容是否与参数指定的字符串s的内容相同。 如:str.equals(“yangzi river”); /值为? (3)public boolean startsWith(String s)和public boolean endsWith(String s):startsWith方法判断当前字符串常量是不是以字符串常量s开头结尾的,若是,则返回true,否则返回false。 如:str.startsWith(“Yan”); /值为?,(4)public int compareTo(String s):该方法按字典序比较字符串常量的大小,参数s为第二个字符串常量。若两个字符串常量相同,则返回0。若当前字符串常量大,则返回值大于0。若另一个字符串常量大,则返回值小于0。 (5)public int indexOf(String s)和public int lastIndexOf(String s):方法返回s在当前字符串常量中从左到右第一次出现的位置,若当前字符串常量中不包含字符串常量s,则返回-1。 (6)public char charAt( int index ):该方法用来获取字符串常量中的一个字符。参数index指定从字符串中返回第几个字符,这个方法返回一个字符型变量。,String类的常用方法(2),String类的常用方法(3),(7)public String concat( String str ):该方法将把字符串常量str连接在当前字符串常量的后面,生成一个新的字符串常量,并返回生成的字符串。 (8)public char toCharArray():该方法将当前字符串常量转换为字符数组,并返回该字符数组。 (9)public String toString():该方法重载了超类Object中的方法toString(),一个对象调用该方法可以获得该对象的字符串表示。 (10)toUpperCase()和toLowerCase()方法可以把一个字符串所有字符分别转换为大写和小写字符,(11)字符串与基本数据的相互转换 public static String valueOf(boolean b) public static String valueOf(数据类型 变量名) valueOf方法可将boolean、char、int、long、float和double 6种类型的变量转换为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); 以上6个方法可将数字型的字符串s转换为相应的数据类型。,String类的常用方法(4),下面的例子说明了String类中常用方法的使用。 public class Ex7_7 public static void main(String args) String s1,s2; s1=new String(“Java Program“); s2=new String(“Java Program“); System.out.println(s1=s2); System.out.println(s1.equals(s2); System.out.println(s1.length(); ,String类的常用方法示例,判断两个引用是否是同一个对象,判断两个引用的内容是否相同,运行结果: false true 12,与Object类中的equals()是否相同?“=”呢?,StringBuffer类,StringBuffer的构造方法 public StringBuffer() public StringBuffer( int length ) public StringBuffer( String str ) 说明: 使用第一个构造方法创建一个空的StringBuffer类的对象,该对象的初始容量为16个字节。使用第二个构造方法创建一个长度为 参数length 的StringBuffer类的对象。注意:如果参数length小于0,将产生NegativeArraySizeException异常。第三个构造方法用一个已存在的字符串常量来创建StringBuffer类的对象,其初始容量为参数字符串str的长度再加上16个字节。,StringBuffer strb=new StringBuffer(“hello!”);,(1)append (int n) 方法 将其他Java类型的数据转化为字符串后,在追加到StringBuffer对象中。 (2)public char charAt(int n) 返回参数n指定位置上的字符。注意,字符串中的第一个字符的位置为0,第二个字符的位置为1,以此类推,且n是一个小于字符串长度的非负数。 (3)public void setCharAt(int n,char ch) 将StringBuffer对象的第n个位置上的字符替换为字符ch。,StringBuffer类的常用方法,strb.append(“world!”); strb.append(5.4);,StringBuffer类示例,下面的例子说明了StringBuffer类中常用方法的使用。 public class Ex7_8 public static void main(String args) StringBuffer s=new StringBuffer(“Java are“); s.setCharAt(5,i); s.setCharAt(6,s); s.delete(7,8); s.append(“ programming“); System.out.println(s); ,System类,System是一个功能强大的特殊的系统类,它提供了标准的输入与输出,以及运行时的系统信息。 System类的构造方法的访问权限为private,所以这个类不能被实例化,即不存在System类的对象。 System类中的所有方法和属性都是static的,即所有方法和属性都可以以System为前缀直接调用。同时System类是final类,所以不能被继承。,系统属性,使用System类中的方法获取当前的系统属性类表列表。,public class Ex7_9 public static void main(String args) String p=System.getProperty(““,“windows xp“); System.out.println(“Operating System :“+p); ,System类举例,其中方法getProperty()是一个类方法,获取当前操作系统的名称,有两种格式,分别为: public static String getProperty(String key); public static String getProperty(String key,String default);,Math类,java.lang包中的Math类提供了许多用来进行科学计算的方法,这些方法都是(static)类的方法,所以在使用时不需要创建Math类的对象,而直接用类名作为前缀,就可以调用这些方法。 Math类中还有两个静态常量E和PI,它们的值分别是:2.718282828284590452354和3.14159265358979323846。,Math类的常用方法及其含义,public class Ex7_10 public static void main(String args) System.out.println(“Math.pow(2,3)“+Math.pow(2,3); System.out.println(“Math.round(2006.10)“+Math.round(2006.10); System.out.println(“Math.sqrt(16)“+Math.sqrt(16); System.out.println(“Math.floor(2006.9)“+Math.floor(2006.9); System.out.println(“Math.random()“+Math.random(); ,例 下面的例子演示几个常用Math方法的调用方法。,Math类举例,Random类,Java实用工具类库中的类java.util.Random提供了产生各种类型随机数的方法。 它可以产生int、long、float、double类型的随机数。这也是它与java.lang.Math中的方法random()最大的不同之处,后者只能产生double型的随机数. Random类的构造方法 public Random() public Random(long seed) Java产生随机数需要有一个种子数seed。第一个构造方法没有参数,它使用系统时间作为种子数seed。,public int nextInt() 产生一个32位整型随机数 public long nextLong() 产生一个64位整型随机数。 public float nextFloat() 产生一个Float型随机数。 public double nextDouble() 产生一个Double型随机数。 public synchronized void setSeed(long seed) 设定种子数seed。 public double nextGaussian(); 该方法可以获得0.0到1.0之间的双精度浮点随机数,且浮点数

温馨提示

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

评论

0/150

提交评论