java包中常用的类.doc

上传人:hs****ma 文档编号:559060696 上传时间:2023-12-14 格式:DOC 页数:7 大小:63.01KB
返回 下载 相关 举报
java包中常用的类.doc_第1页
第1页 / 共7页
java包中常用的类.doc_第2页
第2页 / 共7页
java包中常用的类.doc_第3页
第3页 / 共7页
java包中常用的类.doc_第4页
第4页 / 共7页
java包中常用的类.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《java包中常用的类.doc》由会员分享,可在线阅读,更多相关《java包中常用的类.doc(7页珍藏版)》请在金锄头文库上搜索。

1、1、 java.lang包及该包中的类:java.lang包是java最基本的包,这个包中的所有类都由系统自动引入,所以不用import语句就可以访问该包中的类。1.1、Object类:Object类是所有类的父类,包括自定义的类(无需用extends显式声明继承自Object)。Object类的方法:(1)、public boolean equals(Object obj)Object类的equals方法比较的是两个对象的地址,相当于两个对象做如下运算obj1=obj2,即比较两个对象的地址是否相等。若要按自己的意愿比较两个对象,需要对该方法进行覆盖,同时覆盖了该方法的类最好也覆盖Objec

2、t类的hashCode()方法,同时equals比较的两个对象返回为true时,hashCode()对这两个对象返回的hash值最好一样。(2)、public int hashCode()返回一个哈希值,不同对象有不同的哈希值。(3)、public String toString()返回一个反映当前对象信息的字符串。有一个Employee类如下:public class Employeeprivate int id;private String name;private int age;public Employee(int id, String name, int age)this.id =

3、 id;this.name = name;this.age = age;public Employee()public String getName()return this.name;public void setName(String name)this.name = name;public int getId()return this.id;public void setId(int id)this.id = id;public int getAge()return this.age;public void setAge(int age)this.age = age;public boo

4、lean equals(Object obj)if(this = obj)return true;if(this.getClass() = obj.getClass()/getClass方法获得对象原来的对象类型Employee emp = (Employee)obj;return this.getId() = emp.getId() & this.getAge() = emp.getAge() & this.getName().equals(emp.getName();return false;public String toString()return this.getId() + +th

5、is.getName() + + this.getAge(); public int hashCode()return this.getId()*5+this.getAge()*2+this.getName().length()+100;用test类调用如上的employee类:public class testpublic static void main(String args)Employee e1=new Employee(1,张三,21);Employee e2=new Employee(1,张三,21);System.out.println(e1.toString();/打印e1对

6、象的一些信息System.out.println(e1=e2);/打印结果为falseSystem.out.println(e1.equals(e2);/打印结果为true1.2、Class类:Class类非常特殊,当一个类X被编译后,都会有一个特殊的Class对象产生,它隐藏在X.class文件中,Class对象由编译系统自动生成。Class类的方法:(1)、public static Class forName(String className) throws ClassNotFoundException这个方法是静态方法,可以用Class直接调用,如:Class.forName(“Gum

7、”);方法的返回值是形参指示的类的Class对象。该方法的调用forName(“X”)会导致类X的初始化。(2)、public String getName()该方法返回Class对象代表的实体名(实体可以是类、接口、数组、基本数据类型等)。例如,(new Object().getClass().getName的值是java.lang.Object,其中getClass()获得当前对象的Class对象,同一个类的对象有相同的Class对象。1.3、Math类:Math类是一个最终类,类头定义是:public final class Math extends Object,该类的方法都是静态方法

8、,可以用类名直接调用。1.4、String与StringBuffer类:String类用于处理那些不会发生改变的字符串,而StringBuffer类用于处理那些可能发生变化的字符串。String可以通过String s1=”hello”或String s1=new String(“hello”)创建,而StringBuffer只能用构造函数创建,即:StringBuffer s1=new Stringbuffer(“hello”)。String s1=”hello”和String s1=new String(“hello”)的区别:public class testpublic static

9、void main(String args)String s1=hello;String s2=hello;/s1和s2指向相同的地址System.out.println(s1=s2);/打印的是trues1+=ss;/对s1作字符串的连接操作,s1将会重新生成System.out.println(s1+ +s2);/s1和s2不一样了String s3=new String(hello);String s4=new String(hello);/s3和s4指向不同的地址System.out.println(s3=s4);/打印的是falseString类的方法:(1)、public int

10、length()该方法返回字符串的长度。(2)、public char charAt(int index)返回index位置的字符,index从0到length()-1。(3)、public Boolean equals(Object obj)对Object类的方法的覆盖,可以用来比较两个String类中的字符串是否一样。(4)、public int indexOf(int ch)返回字符串中第一次出现形参所指示的字符的位置,若没该字符,则返回-1。形参是字符的Unicode值。(5)、public boolean equals(ObjectanObject)对Object类的equals方法

11、的重载,若当前String对象的字符串和形参anObject指向的String对象的字符串匹配时才返回为true。(6)、public static String format(String format, Object args)将形参format字符串格式化后返回格式化的字符串。例如:public class ExceptionTestpublic static void main(String args) throws ExceptionString s1=aaa %s bbb;String s2=s1.format(s1,ccc);/s2为”aaa ccc bbb”System.out

12、.println(s2);(7)、public String split(String regex)通过String regex正则表达式的匹配,将调用该方法的字符串拆分成各个字符串。例如:public class testpublic static void main(String args)String s1=aaa:bbb:ccc;String strarr=s1.split(:);/s1字符串按分号(:)拆分成aaa、bbb、ccc,分别保存于strarr字符串数组中for(int i=0;istrarr.length;i+)System.out.println(strarri);/打

13、印拆分的字符串StringBuffer类的方法:(1)、public StringBuffer append(String str)把形参字符串加到当前字符串之后,形成一个新的可变的字符串。例如:public class testpublic static void main(String args)StringBuffer sb=new StringBuffer(hello);sb.append(World);System.out.println(sb);/sb字符串变为helloWorld (2)、public StringBuffer insert(int offset, String

14、str)在当前字符串中插入形参字符串str,形成一个新的可变字符串,形参offset是偏移量,它指示在何处插入,0=offset=length()。(3)、public String toString()把当前可变字符串变成String类对象,事实上在println打印StringBuffer时,自动调用了该方法。1.5、System类:System类是一个final类,它的方法都是静态方法,没有人可以实例化一个System类。System类的属性:public static final InputStream in/标准输入,这个属性是InputStream的一个对象。public static final PrintStream out/标准输出public static final PrintStream err/标准错误输出System类的方法:(1)、public static Properties getProperties(argument);返回系统环境信息。(2)、public static String setProperty(String key, String value)设置系统变量的值,key为键名,value为键值。(3)、public static long currentTimeMillis()返回系统时间于1970年1月1日午夜间

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号