自学内容网 自学内容网

Java语言程序设计——篇八(1)

在这里插入图片描述

     🌿🌿🌿跟随博主脚步,从这里开始→博主主页🌿🌿🌿

主要内容

  ①Object: 终极父类
  ②Math类
  ③基本类型包装类
  ④日期/时间API

Object: 终极父类

  • Java语言中有一个名为java.lang.Object的特殊类,所有的类都是直接或间接地继承该类而得到的。
  • 定义类时,若没有用extends指明继承哪个类,编译器会自动加上extends Object。
  • Object 类中定义的方法:

public String toString( ) //返回对象的字符串表示
public boolean equals( Object obj) //比较对象是否与obj相等
public class<?> getClass() //返回对象所属的类所对应的Class对象
public int hashCode() //返回对象的哈希码值
protected Object clone() //创建并返回对象的一个副本
protected void finalize() //当对该对象没有引用时由垃圾回收器调用

toString( )方法

  • 调用对象的toString()方法可以返回对象的字符串表示
  • 如果在Employee类中没有覆盖toString()方法,执行以下代码:
 Employee emp = new Employee("刘明",30,5000);
 System.out.println(emp.toString());

可能产生类似下面的输出:

  com.demo.Employee@1db9742 //类完全限定名+@+16进制数据

在Employee类中覆盖 toString()方法

public String toString(){
       return  "员工信息:" + name +"  "+ age + "  "+ salary;
   }
System.out.println(emp.toString());
System.out.println(emp); //自动调用toString()方法
    输出结果都为:
    员工信息:刘明  30  5000.0

equals( )方法

equals()方法主要用来比较两个对象是否相等,使用格式为:

 obj1.equals(obj2)
equals()方法在Object类中的定义:
    public boolean equals(Object obj){
        return (this == obj); //比较引用是否相等
    }

相当于两个对象使用“==”号进行比较。

getClass( )方法

  • 返回运行时的对象所属的类所对应的Class对象。
  • 每当一个类被加载时,JVM就会自动为其生成一个Class对象。由于Class类没有构造方法,需要通过Object类的getClass()方法取得对象对应的Class对象。
  • getClass().getName()是用来返回Class对象所代表的具体对象的名称。

hashCode( )方法

  • hashCode()方法返回对象的哈希码(hash code)值,即对象在内存中的十进制地址。
  • 在覆盖Object类的hashCode()方法时,要保证相同对象的哈希码必须相同。
    覆盖时常调用java.util.Objects类的hash()方法
  • 可以使用不同算法生成对象的哈希码,例如,String类使用下面算法生成它的哈希码:
     int hash = 0;
     for(int i =0; i < length(); i++)
          hash = 31 * hash +charAt(i);
      //相同字符串的哈希码相同

clone( )方法

  • 使用Object类的clone()方法可以克隆一个对象,即创建一个对象的副本。
  • 要使类的对象能够克隆,类必须实现Cloneable接口。
  • clone()方法声明抛出CloneNotSupportedException异常。
  • clone()方法的返回类型为Object。

finalize( )方法

  • 在对象被销毁之前,垃圾回收器允许对象调用finalize( )方法进行清理工作,称为对象终结。
  • finalize( )方法的定义格式为:
protected void finalize( )  throws Throwable
  • 每个对象的finalize( )方法仅被调用一次。
实战演练

Object类中toString()、equals(Object obj)、hashCode()、getClass()、clone()方法使用

package shujia_test1;

public class CsdN8_1 implements Cloneable {
// Object类中toString()、equals(Object obj)、hashCode()、getClass()、clone()方法使用
private int id; // 编号
private String brand; // 品牌
private String color; // 颜色

public CsdN8_1(int id, String brand, String color) {
this.id = id;
this.brand = brand;
this.color = color;
}

public String toString() {
return "汽车:id = " + id + " brand=" + brand + "color=" + color;
}

public boolean equals(Object obj) {
return this.id == ((CsdN8_1) obj).id;
}

protected void finalize() throws Throwable {
System.out.println("The object is destroyed");

}

public static void main(String[] args) throws CloneNotSupportedException {
CsdN8_1 c1 = new CsdN8_1(101, "宝马", "棕色");
CsdN8_1 c2 = (CsdN8_1) c1.clone();
System.out.println(c1 == c2);
System.out.println(c1.equals(c2));
System.out.println(c1.getClass().getName());
System.out.println(c1.hashCode());
System.out.println(c1);
c1 = null;
c2 = null;
System.gc(); // 执行垃圾回收
}
}

运行结果:
在这里插入图片描述

Math类

Math类的使用

java.lang.Math类中定义了一些方法实现数学上的基本函数功能:

  • 指数函数
  • 对数函数
  • 平方根函数
  • 三角函数
  • 两个常量PI和E
  • Math类中定义的所有的方法和两个常量都是static的,仅能通过类名访问。
    在这里插入图片描述
实战演练

sqrt()、pow()、rint()、round()方法以及常量PI的使用

//sqrt()、pow()、rint()、round()方法以及常量PI的使用
public class MathDemo {
public static void main(String[] args) {
System.out.println("sqrt(2) = " + Math.sqrt(2)); //求2的平方根
System.out.println("pow(2,5) = " + Math.pow(2, 5)); //求2的5次方
//double rint(double x)返回与x最接近的整数,若x到两个整数的距离相等则返回偶数
System.out.println("rint(2.5) = " + Math.rint(2.5));
System.out.println("rint(-3.5) = " + Math.rint(-3.5));
//long round(double x)返回(long)Math.floor(x+0.5)
System.out.println("round(3.5) = " + Math.round(3.5));
System.out.println("round(-3.5) = " + Math.round(-3.5));
double pi = Math.PI;
pi = Math.round(pi * 10000) / 10000.0; // 四舍五入到小数点后4位
System.out.println("PI = " + pi);
   }
}

random()方法的使用

Math类中的random()方法用来生成大于等于0.0小于1.0的double型随机数
(0.0<=Math.random()<1.0)

(int)(Math.random() * 10)                 //  [0,9]
  50 + (int)(Math.random() * 51)        //  [50, 100]
  a + (int)(Math.random() * (b+1))     //  [a, a +b]
实战演练

问题描述:
编写一个方法,随机返回一个小写字母。用该方法随机生成100个小写字母输出,每行20个。

 public static char getLetter()

思路:小写字母的ASCII码值在97(‘a’)到122(‘z’)之间,因此只需随机产生97到122之间的整数,然后把它们转换成字符即可。

//编写一个方法,随机返回一个小写字母。
//用该方法随机生成100个小写字母输出,每行20个。
public class RandomCharacter {
//随机返回一个小写字母'a'~'z'
    public static char getLetter(){
     //[97+0, 97+26)即[97, 122]即['a','z']
      return (char)(97 + Math.random() * (26)); 
    }
    
    public static void main (String[] args) {
      for(int i = 1 ;i <= 100 ; i ++){
         System.out.print(getLetter()+" ");
        if( i % 20 ==0)    // 每行输出20个字母后换行
          System.out.println();
      }
    }
}

博主用心写,读者点关注,互动传真情,知识不迷路


原文地址:https://blog.csdn.net/2201_75851346/article/details/140718717

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!