本节将介绍剩下的几个java常用类。

3.Object类

  • Object类存储在java.lang包中,是所有java类(Object类除外)的终极父类。当然,数组也继承了Object类,但是,接口和枚举类是不继承Object类的。
  • java的任何类都继承了Object类的成员方法,并且可以重写不被final修饰的方法。
  • Object类包含的一些成员方法:

    那些java中的常用类(二)

  1. Object()方法:一般情况下,java通过new关键字来创建对象,而这是通过类中的构造方法来实现的。因此,java中规定:在类的定义过程中,对于未定义构造方法的类,默认会有一个无参的构造方法,作为所有java类的父类,Object自然也要体现这一特性。所以,在Object的源码中没有给出构造方法的具体定义,但实际上这个构造方法是存在的。
  2. clone()方法:这个方法可以产生一个相同的类,并且返回给调用者。这是一个native方法,由C语言实现,使用时需要注意:当代码执行的时候,将会检查调用对象的类(或者父类)是否实现了java.lang.Cloneable接口(Object类不实现Cloneable)。如果没有实现这个接口,clone()将会抛出一个检查异常()——java.lang.CloneNotSupportedException,如果实现了这个接口,clone()会创建一个新的对象,并将原来对象的内容复制到新对象,最后返回这个新对象的引用。
  3. finalize()方法:finalize()也是一个native方法,并且这个方法可以被子类对象所覆盖,然后作为一个终结者,当GC被调用的时候完成最后的清理工作(例如释放系统资源之类)。默认的finalize()方法什么也不做,当被调用时直接返回。
  4. Object还提供了wait、notify、notifyAll几个方法,用于控制线程的暂停和运行,这将在以后的文章中进行详细介绍。
  5. clone()方法的使用示例:
     1 /*
     2  * 定义一个Person类,并实现Cloneable接口
     3  */
     4 public class Person implements Cloneable {
     5 
     6     @Override
     7     protected Object clone() throws CloneNotSupportedException {
     8         // TODO Auto-generated method stub
     9         return super.clone();
    10     }
    11 
    12     public String name;
    13 }
     1 public class CloneTest {
     2     public static void main(String[] args) throws CloneNotSupportedException {
     3         //创建一个Person对象,并初始化
     4         Person p1 = new Person("hello");
     5         System.out.println(p1.name);//hello
     6         
     7         /*
     8          * 调用clone方法,对p1对象进行克隆
     9          *     注意:当代码执行的时候,将会检查调用对象的类(或者父类)是否实现了java.lang.Cloneable接口(Object类不实现Cloneable)。
    10          *         如果没有实现这个接口,clone()将会抛出一个检查异常()——java.lang.CloneNotSupportedException,
    11          *         如果实现了这个接口,clone()会创建一个新的对象,并将原来对象的内容复制到新对象,最后返回这个新对象的引用。
    12          */
    13         //所以,此处会先检查Person类是否实现了Cloneable,即重写clone()方法。
    14         Person p2 = (Person) p1.clone();
    15         System.out.println(p2.name);//hello
    16         
    17     }
    18 
    19 }

4.Math类

 Math类提供了基本数学函数的计算方法,并且Math类中所有的成员变量(主要指PI和E)和成员方法都是静态的,可以通过类名直接调用。这里只是简单列举一些:

 1 public class MathDemo {
 2 
 3     public static void main(String[] args) {
 4         /** 
 5          * abs求绝对值 
 6          */  
 7         System.out.println(Math.abs(-10.4));    //10.4  
 8         System.out.println(Math.abs(10.1));     //10.1  
 9           
10         /** 
11          * ceil天花板的意思,就是返回大的值,注意一些特殊值 
12          */  
13         System.out.println(Math.ceil(-10.1));   //-10.0  
14         System.out.println(Math.ceil(10.7));    //11.0  
15         System.out.println(Math.ceil(-0.7));    //-0.0  
16         System.out.println(Math.ceil(0.0));     //0.0  
17         System.out.println(Math.ceil(-0.0));    //-0.0  
18           
19         /** 
20          * floor地板的意思,就是返回小的值 
21          */  
22         System.out.println(Math.floor(-10.1));  //-11.0  
23         System.out.println(Math.floor(10.7));   //10.0  
24         System.out.println(Math.floor(-0.7));   //-1.0  
25         System.out.println(Math.floor(0.0));    //0.0  
26         System.out.println(Math.floor(-0.0));   //-0.0  
27           
28         /** 
29          * max 两个中返回大的值,min和它相反,就不举例了 
30          */  
31         System.out.println(Math.max(-10.1, -10));   //-10.0  
32         System.out.println(Math.max(10.7, 10));     //10.7  
33         System.out.println(Math.max(0.0, -0.0));    //0.0  
34           
35         /** 
36          * random 取得一个大于或者等于0.0小于不等于1.0的随机数 
37          */  
38         System.out.println(Math.random());  //0.08417657924317234  
39         System.out.println(Math.random());  //0.43527904004403717  
40           
41         /** 
42          * rint 四舍五入,返回double值 
43          * 注意.5的时候会取偶数 
44          */  
45         System.out.println(Math.rint(10.1));    //10.0  
46         System.out.println(Math.rint(10.7));    //11.0  
47         System.out.println(Math.rint(11.5));    //12.0  
48         System.out.println(Math.rint(10.5));    //10.0  
49         System.out.println(Math.rint(10.51));   //11.0  
50         System.out.println(Math.rint(-10.5));   //-10.0  
51         System.out.println(Math.rint(-11.5));   //-12.0  
52         System.out.println(Math.rint(-10.51));  //-11.0  
53         System.out.println(Math.rint(-10.6));   //-11.0  
54         System.out.println(Math.rint(-10.2));   //-10.0  
55           
56         /** 
57          * round 四舍五入,float时返回int值,double时返回long值 
58          */  
59         System.out.println(Math.round(10.1));   //10  
60         System.out.println(Math.round(10.7));   //11  
61         System.out.println(Math.round(10.5));   //11  
62         System.out.println(Math.round(10.51));  //11  
63         System.out.println(Math.round(-10.5));  //-10  
64         System.out.println(Math.round(-10.51)); //-11  
65         System.out.println(Math.round(-10.6));  //-11  
66         System.out.println(Math.round(-10.2));  //-10  
67         
68         /*
69          * 指数函数的计算方法,返回double值
70          *     注意Math.log()是计算以e为底数的自然对数
71          */
72         System.out.println(Math.pow(Math.E, 2));//e^2
73         System.out.println(Math.sqrt(25));//5.0
74         System.out.println(Math.exp(2));//e^2
75         System.out.println(Math.log(Math.pow(Math.E, 2)));//2.0
76         System.out.println(Math.log10(100));//2.0
77         
78     }
View Code

相关文章: