Presentation is loading. Please wait.

Presentation is loading. Please wait.

抽象类 File类 String类 StringBuffer类

Similar presentations


Presentation on theme: "抽象类 File类 String类 StringBuffer类"— Presentation transcript:

1 抽象类 File类 String类 StringBuffer类
第12讲 包和接口(二) 抽象类 File类 String类 StringBuffer类 System类 Math类 Random类 1/

2 教学目标 理解抽象类的概念; 了解File类、String类、StringBuffer类的用途
掌握System类的常用方法 会使用Math类的常用方法 会使用Random类的常用方法

3 抽象类 抽象类的定义与一般类一样都有数据和方法,定义格式与一般类也非常类似只是在定义类的class前增加一个关键字abstract就表示定义一个抽象类,也就说用abstract说明的类称为抽象类。 抽象类不能用来实例化一个对象,它只能被继承。

4 抽象类示例 public abstract class Person{ //定义一个抽象类Person String name;
int age; public void birth(){……} public abstract void go(); } 注意: (1)Person类是抽象类不能用new创建它的实例,可以被继承,抽象方法go()只有方法头标志而没有实现,它的实现由子类操作。 (2)注意抽象类与接口的关系.

5 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()  //返回指定文件最后被修改的时间

6 File类提供的方法(续) 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()   //返回目录中的所有文件对象

7 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);

8 File类示例(续) if(fileName.isDirectory()){
System.out.println(fileName.getName()+"是一个目录"); System.out.println("****************************"); File list[]= fileName.listFiles(); for(int i=0;i<list.length;i++){ System.out.println(list[i].getName()); } } else{ System.out.println(fileName.getName()+"是一个文件"); System.out.println("******************************"); System.out.println(fileName.getAbsolutePath()); } }

9 String类 例如: String str=new String(“Yangzi River”); String类的构造方法
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”);

10 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”); //值为?

11 String类的常用方法(2) (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指定从字符串中返回第几个字符,这个方法返回一个字符型变量。

12 String类的常用方法(3) (7)public String concat( String str ):该方法将把字符串常量str连接在当前字符串常量的后面,生成一个新的字符串常量,并返回生成的字符串。 (8)public char[] toCharArray():该方法将当前字符串常量转换为字符数组,并返回该字符数组。 (9)public String toString():该方法重载了超类Object中的方法toString(),一个对象调用该方法可以获得该对象的字符串表示。 (10)toUpperCase()和toLowerCase()方法可以把一个字符串所有字符分别转换为大写和小写字符.

13 String类的常用方法(4) (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转换为相应的数据类型。

14 与Object类中的equals()是否相同?“==”呢?
String类的常用方法示例 下面的例子说明了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()); } 与Object类中的equals()是否相同?“==”呢? 判断两个引用是否是同一个对象 判断两个引用的内容是否相同 运行结果: false true 12

15 StringBuffer类 StringBuffer strb=new StringBuffer(“hello!”);
  public StringBuffer()   public StringBuffer( int length )   public StringBuffer( String str ) 说明:   使用第一个构造方法创建一个空的StringBuffer类的对象,该对象的初始容量为16个字节。使用第二个构造方法创建一个长度为 参数length 的StringBuffer类的对象。注意:如果参数length小于0,将产生NegativeArraySizeException异常。第三个构造方法用一个已存在的字符串常量来创建StringBuffer类的对象,其初始容量为参数字符串str的长度再加上16个字节。

16 StringBuffer类的常用方法 (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。 strb.append(“world!”); strb.append(5.4);

17 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); }

18 System类 System是一个功能强大的特殊的系统类,它提供了标准的输入与输出,以及运行时的系统信息。
System类的构造方法的访问权限为private,所以这个类不能被实例化,即不存在System类的对象。 System类中的所有方法和属性都是static的,即所有方法和属性都可以以System为前缀直接调用。同时System类是final类,所以不能被继承。

19 系统属性 系统属性名 含义 os.name 操作系统名称 java.classpath Java类库的路径 java.home
user.dir 用户当前的工作目录 user.name 用户名 file.separator 文件分隔符 line.separator 行结束符 path.separator 路径分隔符 user.home 用户的根目录

20 System类举例 使用System类中的方法获取当前的系统属性类表列表。 public class Ex7_9{
 public static void main(String args[]) {   String p=System.getProperty("os.name","windows xp");   System.out.println("Operating System :"+p);  } } 其中方法getProperty()是一个类方法,获取当前操作系统的名称,有两种格式,分别为: public static String getProperty(String key); public static String getProperty(String key,String default);

21 Math类 java.lang包中的Math类提供了许多用来进行科学计算的方法,这些方法都是(static)类的方法,所以在使用时不需要创建Math类的对象,而直接用类名作为前缀,就可以调用这些方法。 Math类中还有两个静态常量E和PI,它们的值分别是: 和 。

22 Math类的常用方法及其含义 方法名 说明 public static long abs(double a) 返回a的绝对值
public static double max(double a,double b) 返回a与b中的较大的值 public static double min(double a,double b) 返回a与b中的较小的值 public static double random() 生成一个0—1之间的随机数(不含1) public static double pow(double a,double b) 返回a的b次方 public static double sqrt(double a) 返回a的平方根 public static double log(double a) 返回a的对数 public static double sin(double a) 返回正弦值 public static double cos(double a) 返回余弦值 public static double asin(double a) 返回反正弦值 public static int round(float a) 返回离a最近的整数,即四舍五入 public static int floor(double a) 返回比a小的最大整数,即取整

23 Math类举例 例 下面的例子演示几个常用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( )"+Math.round( )); 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()); }

24 Random类 Java实用工具类库中的类java.util.Random提供了产生各种类型随机数的方法。
它可以产生int、long、float、double类型的随机数。这也是它与java.lang.Math中的方法random()最大的不同之处,后者只能产生double型的随机数. Random类的构造方法 public Random() public Random(long seed) Java产生随机数需要有一个种子数seed。第一个构造方法没有参数,它使用系统时间作为种子数seed。

25 Random类的常用方法 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之间的双精度浮点随机数,且浮点数按高斯分布。

26 Random类举例 例7.11应用Random类提供的方法生成各种类型的随机数。 import java.util.Random;
public class Ex7_11{ public static void main(String args[]) { Random r1=new Random(); Random r2=new Random(2008); //创建了类Random的两个对象。 System.out.println("The 1st set of random numbers:"); System.out.println(" Integer:"+r1.nextInt()); System.out.println(" Integer:"+r1.nextInt(100)); // [0,100) System.out.println(" Long:"+r1.nextLong()); System.out.println(" Float:"+r1.nextFloat()); System.out.println(" Double:"+r1.nextDouble()); System.out.println(" Gaussian:"+r1.nextGaussian()); //各种类型 System.out.print("The 2nd set of random numbers:"); for(int i=1;i<10;i++){ System.out.println(r2.nextInt()+" "); if(i==5) System.out.println(); } } }

27 作业 作业 P157-1~5,10,11


Download ppt "抽象类 File类 String类 StringBuffer类"

Similar presentations


Ads by Google