文章目录
1.Object类
1.1Object类的介绍
Object类是所有类的默认父类,所有的类都具有该类的11个方法
1.2toString方法
将返回的对象以字符串形式表示默认的字符串形式:
包名.类名.@地址值
在实际开发中我们会经常重写toString方法以便打印出该对象的内容
代码实现
package com.itheima.Text03;
public class Person {
private String name;
private int age;
//无参构造方法
public Person() {
}
//全参构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println("吃东西");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.itheima.Text03;
public class Test01 {
public static void main(String[] args) {
Person p = new Person("张三", 20);
p.eat();
//没有重写toString方法
// System.out.println(p);//com.itheima.Text03.Person@1b6d3586
//重写toString方法后
System.out.println(p);
}
}
当我们重写toString方法后返回的不再是地址值,而是具体的属性值
1.3equals方法
判断两个对象内容是否相等
默认比较的是两个地址值
在实际开发中我们会重写equals方法
代码实现:
package com.itheima.Text03;
import java.util.Objects;
public class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Animal animal = (Animal) o;
return age == animal.age &&
Objects.equals(name, animal.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.itheima.Text03;
public class TextAnimal {
public static void main(String[] args) {
Animal a1 = new Animal("孙悟空", 10000);
Animal a2 = new Animal("孙悟空", 10000);
if (a1.equals(a2)) {
System.out.println("相等");
} else {
System.out.println("不相等");
}
}
}
equals与==的区别
基本类型
==比较的是基本类型的数值,
equals没有该方法
引用类型的比较
==比较地址值
equals比较的是地址值,重写后比较的是内容
1.4本地方法
有native本地方法 ,不是Java写的
public native int hashCode();
1.5Objects类
是工具类
在Objects方法中有比较的方法
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
2Data类
2.1Data的构造方法
public Data();创建一个Data对象表示当前系统时间
public Data(long millis);创建一个Data对象,代表距离基准时间毫秒值
代码案例:
public class Demo01 {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);//Fri Mar 27 18:14:53 CST 2020
Date d1 = new Date(0);
System.out.println(d1);//Thu Jan 01 08:00:00 CST 1970
}
}
2.2Data的常用方法
getTime获取当前对象距离基准时间的毫秒值
setTime修改Data对象距离基准时间的毫秒值
Date d = new Date();
Date d3 = new Date();
long time = d.getTime();
System.out.println(time);//1585304579738
d3.setTime(1000);
System.out.println(d3);//Thu Jan 01 08:00:01 CST 1970
3.DataFormat类
3.1DataFormat类的作用
让时间日期和具体的文本之间来回转换
格式化:Data对象转换为时间字符串
解析:时间字符串转换为Data对象
案例:
new Date() --> “2020年03月27日 8点0分0秒” 格式化
“2020年03月27日 8点0分0秒” --> Date对象 解析
public class Demo02 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
Date d = new Date();
//格式化
String format = s.format(d);
System.out.println(format);//2020年03月27日
String data = "2020年3月27日";
Date parse = s.parse(data);
System.out.println(parse);//Fri Mar 27 00:00:00 CST 2020
}
}
format格式化
parse解析
4.Calendar类
4.1Calendar的介绍和获取对象的方式
calendar表示某个时间点
一般使用calendar 的静态方法
一般格式:
Calendar c = Calendar。getInstance();
get获取日历对象的某个成员变量值,其中参数代表成员变量的编号
set修改日历对象某个成员的值
add增加日历对象某个成员的值
案例
import java.util.Calendar;
public class Demo03_Calendar {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);//有我们需要的数据,也有我们不需要的数据
int nowyear = c.get(Calendar.YEAR);
int nowmonth = c.get(Calendar.MONTH);
int nowday = c.get(Calendar.DAY_OF_MONTH);
System.out.println("时间:" + nowyear + nowmonth + nowday);
c.set(Calendar.YEAR, 3000);
c.set(Calendar.MONTH, 3);
c.set(Calendar.DAY_OF_MONTH, 3);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println("时间:" + year + month + day);
c.add(Calendar.YEAR, 1000);
c.add(Calendar.MONTH, 2);
c.add(Calendar.DAY_OF_MONTH, 20);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);
System.out.println("时间:" + y + m + d);
}
}
5.Math类
5.1Math的介绍与常见方法
Math包含数学运算的相关方法
其中内部的方法都是静态的
对小数
abs求绝对值
ceil向上取整
floor向下取整
round四舍五入
pow求次幂
public class Demo04_Math {
public static void main(String[] args) {
double d = 3.14;
double abs = Math.abs(d);//绝对值为3.14
System.out.println(abs);
double ceil = Math.ceil(d);//向上取整4
System.out.println(ceil);
double floor = Math.floor(d);//向下取整3
System.out.println(floor);
long round = Math.round(d);//四舍五入3
System.out.println(round);
double pow = Math.pow(d, 3);
System.out.println(pow);
}
}
6.System
System类的介绍
System类的构造被私有化不能new对象
常用方法:
System.exit();退出jvm虚拟机
currentTimeMillis();获取当前毫秒值
public class Demo05_System {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("woaijava");
System.exit(0);
}
}
}
计算毫秒值
public class Demo06_System {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String s = " ";
for (int i = 0; i < 50000; i++) {
s += " ";
}
long end = System.currentTimeMillis();
System.out.println("所用时间为:"+(end-start));//所用时间为:2895
}
}