1.Object类常用方法
Example8-1:Object类常用方法(08_API\api_object)
//Student.java
package api_object;
import java.util.Arrays;
import java.util.Objects;
//要调用clone()方法,必须让被克隆的类实现Cloneable接口
public class Student implements Cloneable{
private int id;
private String name;
private String password;
private double[] score;
public Student() {
}
public Student(int id, String name, String password, double[] score) {
this.id = id;
this.name = name;
this.password = password;
this.score = score;
}
@Override //重写toString()方法,返回对象的属性值
public String toString() {
return "Student [id=" + id + ", name=" + name + ", password=" + password
+ ", score=" + Arrays.toString(score)
+ "]";
}
@Override //重写equals()方法,按对象的属性值进行比较
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
return id == other.id && Objects.equals(name, other.name) &&
Objects.equals(password, other.password)
&& Arrays.equals(score, other.score);
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double[] getScore() {
return score;
}
public void setScore(double[] score) {
this.score = score;
}
}
//studentTest.java
package api_object;
public class StuTest {
public static void main(String[] args) throws CloneNotSupportedException
{
Student s1 = new Student(1,"joyce","jj123", new double[]{95.0,99.5});
System.out.println(s1.toString());
System.out.println(s1);
Student s2 = new Student(1,"joyce","jj123", new double[]{95.0,99.5});
System.out.println(s1.equals(s2)); //比较对象属性值是否相等
System.out.println( s1 == s2 );
//比较地址是否相等
Student s3 = (Student) s1.clone();
System.out.println(s3.toString());
}
}
//深克隆
public class Student implements Cloneable{
private String id;
private String name;
private String password;
private double[] scores;
public Student() {
}
publicStudent(String id, String name, String password, double[] scores)
{
this.id = id;
this.name = name;
this.password = password;
this.scores = scores;
}
//...get 和set...方法自己加上
@Override
protected Object clone() throws CloneNotSupportedException {
//先克隆得到一个新对象
Student s4 = (Student) super.clone();
//再将新对象中的引用类型数据,再次克隆
s4.scores = s4.scores.clone();
return s4;
}
}
浅克隆
浅克隆是指在克隆对象时,对于基本数据类型的变量会重新复制一份,而对于引用类型的变量只是对引用进行克隆。就是将栈中的值复制一份给新的变量,因为浅复制只会将对象的各个属性进行依次复制,并不会进行递归复制,而 JavaScript 存储对象都是存地址的,所以浅复制会导致两个对象指向都是同一个地址,一个发生改变另外一个也发生改变
深克隆本质上是创造一个完全一样的对象,但是两个对象的引用地址完全不同,也就是不同与浅克隆,一个对象的改变,另外一个对象并不会发生改变。
Example8-2:Math 类常用方法(MathTest.java)
public class MathTest {
public static void main(String[] args) {
// 1、public static int/double abs(int/double a):取绝对值(拿到的结果一定是正数)
System.out.println(Math.abs(-12)); // 12
System.out.println(Math.abs(123)); // 123
System.out.println(Math.abs(-3.14)); // 3.14
// 2、public static double ceil(double a): 向上取整
System.out.println(Math.ceil(4.0000001)); // 5.0
System.out.println(Math.ceil(4.0)); // 4.0
// 3、public static double floor(double a): 向下取整
System.out.println(Math.floor(4.999999)); // 4.0
System.out.println(Math.floor(4.0)); // 4.0
// 4、public static long round(double a):四舍五入
System.out.println(Math.round(3.4999)); // 3
System.out.println(Math.round(3.50001)); // 4
// 5、public static int max/min(int a, int b):取较大值
System.out.println(Math.max(10, 20)); // 20
System.out.println(Math.min(10, 20)); // 10
// 6、 public static double pow(double a, double b):取次方
System.out.println(Math.pow(2, 3)); // 2的3次方 8.0
// 7、public static double random(): 取随机数 [0.0 , 1.0) (包前不包后)
System.out.println(Math.random());
}
}
Example8-3:System 类常用方法(SystemTest.java)
public class SystemTest {
public static void main(String[] args) {
// public static long currentTimeMillis():
// 获取当前系统的时间
// 返回的是long类型的时间毫秒值:指的是从1970-1-1 0:0:0开始走到此刻的总的毫秒值,1s = 1000ms
long time1 = System.currentTimeMillis();
System.out.println(time1);
for(int i =0;i<100;i++) {
System.out.println("输出了:"+i);
}
long time2=System.currentTimeMillis();
System.out.println((time2-time1)/1000.0+"s");
}
}
Example8-4:基本类型包装类
package system;
import java.util.ArrayList;
public class SystemTest {
public static void main(String[] args) {
//1.public Integer(int value)构造方法
Integer a = new Integer(10); //提示已过时,以后不用了
//2.public static Integer valueOf(int i)方法
Integer b = Integer.valueOf(10);
//3.自动装箱:自动将基本类型转换为引用类型
Integer c = 10;
//4.自动拆箱:自动将引用类型转换为基本类型
int d = c;
//5.泛型和集合不支持基本类型,只支持引用数据类型
ArrayList<Integer> list = new ArrayList<>();
//添加的元素是基本类型,实际上会自动装箱为Integer类型
list.add(100);
//获取元素时,会将Integer类型自动拆箱为int类型
int e = list.get(0);
System.out.println("========================");
//6.把基本类型数据转换为字符串
Integer f = 23;
String s1 = Integer.toString(f);
System.out.println(s1 + 1); //231
String s2 = f.toString();
System.out.println(s2 + 1); //231
String s3 = f + "";
System.out.println(s3 + 1); //231
String s4 = String.valueOf(f);
System.out.println(s4 + 1); //231
//7.字符串转换为数值型数据
String ageStr = "315";
int age1 = Integer.parseInt(ageStr);
int age2 = Integer.valueOf(ageStr); //首选
System.out.println(age1 + 1);
String scoreStr = "3.14";
double score1 = Double.parseDouble(scoreStr);
double score2 = Double.valueOf(scoreStr); //首选
System.out.println(age1 + 1);
}
}
Example8-5:BigDecimal 类常用方法(BigDeTest.java)
package system;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class SystemTest {
public static void main(String[] args) {
System.out.println(0.1 + 0.2);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);
//结果显示精度缺失
//BigDecimal类精确运算:不失精度,还可保留指定的小数位
double a = 0.1;
double b = 0.2;
// 1、把浮点型数据封装成BigDecimal对象,参与运算。
// public static BigDecimal valueOf(double val):得到BigDecimal对象
//可精确运算
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
// 2、public BigDecimal add(BigDecimal augend): 加法
BigDecimal c1 = a1.add(b1);
System.out.println(c1);
// 3、public BigDecimal subtract(BigDecimal augend): 减法
BigDecimal c2 = a1.subtract(b1);
System.out.println(c2);
// 4、public BigDecimal multiply(BigDecimal augend): 乘法
BigDecimal c3 = a1.multiply(b1);
System.out.println(c3);
// 5、public BigDecimal divide(BigDecimal b): 除法
BigDecimal c4 = a1.divide(b1);
System.out.println(c4);
// BigDecimal d1 = BigDecimal.valueOf(0.1);
// BigDecimal d2 = BigDecimal.valueOf(0.3);
// BigDecimal d3 = d1.divide(d2);
// System.out.println(d3);
// 6、public BigDecimal divide(另一个BigDecimal对象,精确几位,舍入模
//式) : 除法,可以设置精确几位。
BigDecimal d1 = BigDecimal.valueOf(0.1);
BigDecimal d2 = BigDecimal.valueOf(0.3);
BigDecimal d3 = d1.divide(d2, 2, RoundingMode.HALF_UP); // 0.33
System.out.println(d3);
// 7、public double doubleValue() : 把BigDecimal对象又转换成double类
// 型的数据。
//print(d3);
//print(c1);
double db1 = d3.doubleValue();
double db2 = c1.doubleValue();
print(db1);
print(db2);
}
private BigDeTest divide(BigDeTest d2, int i, RoundingMode halfUp) {
// TODO Auto-generated method stub
return null;
}
private double doubleValue() {
// TODO Auto-generated method stub
return 0;
}
private BigDeTest divide(BigDeTest b1) {
// TODO Auto-generated method stub
return null;
}
private BigDeTest multiply(BigDeTest b1) {
// TODO Auto-generated method stub
return null;
}
private BigDeTest subtract(BigDeTest b1) {
// TODO Auto-generated method stub
return null;
}
private BigDeTest add(BigDeTest b1) {
// TODO Auto-generated method stub
return null;
}
private static BigDeTest valueOf(double a) {
// TODO Auto-generated method stub
return null;
}
public static void print(double a){
System.out.println(a);
}
}
Example8-6:日期与时间类
//LocalDate 类
package api_date;
import java.time.LocalDate;
public class Test_LocalDate {
public static void main(String[] args) {
// 0、获取本地日期对象(不可变对象)
LocalDate ld = LocalDate.now(); // 年 月 日
System.out.println(ld);
// 1、获取日期对象中的信息
int year = ld.getYear(); // 年
int month = ld.getMonthValue(); // 月(1-12)
int day = ld.getDayOfMonth(); // 日
int dayOfYear = ld.getDayOfYear(); // 一年中的第几天
int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几
System.out.println(year);
System.out.println(day);
System.out.println(dayOfWeek);
// 2、直接修改某个信息: withYear、withMonth、withDayOfMonth、
withDayOfYear
LocalDate ld2 = ld.withYear(2099);
LocalDate ld3 = ld.withMonth(12);
System.out.println(ld2);
System.out.println(ld3);
System.out.println(ld);
// 3、把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeks
LocalDate ld4 = ld.plusYears(2);
LocalDate ld5 = ld.plusMonths(2);
// 4、把某个信息减多少:minusYears、minusMonths、minusDays、
minusWeeks
LocalDate ld6 = ld.minusYears(2);
LocalDate ld7 = ld.minusMonths(2);
// 5、获取指定日期的LocalDate对象: public static LocalDate of(int
year, int month, int dayOfMonth)
LocalDate ld8 = LocalDate.of(2099, 12, 12);
LocalDate ld9 = LocalDate.of(2099, 12, 12);
// 6、判断2个日期对象,是否相等,在前还是在后: equals isBefore
isAfter
}
}
System.out.println(ld8.equals(ld9));// true
System.out.println(ld8.isAfter(ld)); // true
System.out.println(ld8.isBefore(ld)); // false
//LocalTime 类
package api_date;
import java.time.LocalTime;
public class Test_LocalTime {
public static void main(String[] args) {
// 0、获取本地时间对象
LocalTime lt = LocalTime.now(); // 时 分 秒 纳秒 不可变的
System.out.println(lt);
// 1、获取时间中的信息
int hour = lt.getHour(); //时
int minute = lt.getMinute(); //分
int second = lt.getSecond(); //秒
int nano = lt.getNano(); //纳秒
// 2、修改时间:withHour、withMinute、withSecond、withNano
LocalTime lt3 = lt.withHour(10);
LocalTime lt4 = lt.withMinute(10);
LocalTime lt5 = lt.withSecond(10);
LocalTime lt6 = lt.withNano(10);
// 3、加多少:plusHours、plusMinutes、plusSeconds、plusNanos
LocalTime lt7 = lt.plusHours(10);
LocalTime lt8 = lt.plusMinutes(10);
LocalTime lt9 = lt.plusSeconds(10);
LocalTime lt10 = lt.plusNanos(10);
// 4、减多少:minusHours、minusMinutes、minusSeconds、minusNanos
LocalTime lt11 = lt.minusHours(10);
LocalTime lt12 = lt.minusMinutes(10);
LocalTime lt13 = lt.minusSeconds(10);
LocalTime lt14 = lt.minusNanos(10);
// 5、获取指定时间的LocalTime对象:
// public static LocalTime of(int hour, int minute, int second)
LocalTime lt15 = LocalTime.of(12, 12, 12);
LocalTime lt16 = LocalTime.of(12, 12, 12);
// 6、判断2个时间对象,是否相等,在前还是在后: equals isBefore
isAfter
}
}
System.out.println(lt15.equals(lt16)); // true
System.out.println(lt15.isAfter(lt)); // false
//LocalDateTime 类
package api_date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Test_LocalDateTime {
public static void main(String[] args) {
// 0、获取本地日期和时间对象。
LocalDateTime ldt = LocalDateTime.now(); // 年 月 日 时 分 秒 纳秒
System.out.println(ldt);
// 1、可以获取日期和时间的全部信息
int year = ldt.getYear(); // 年
int month = ldt.getMonthValue(); // 月
int day = ldt.getDayOfMonth(); // 日
int dayOfYear = ldt.getDayOfYear(); // 一年中的第几天
int dayOfWeek = ldt.getDayOfWeek().getValue(); // 获取是周几
int hour = ldt.getHour(); //时
int minute = ldt.getMinute(); //分
int second = ldt.getSecond(); //秒
int nano = ldt.getNano(); //纳秒
// 2、修改时间信息:
// withYear withMonth withDayOfMonth withDayOfYear withHour
// withMinute withSecond withNano
LocalDateTime ldt2 = ldt.withYear(2029);
LocalDateTime ldt3 = ldt.withMinute(59);
// 3、加多少:
// plusYears plusMonths plusDays plusWeeks plusHours plusMinutes
plusSeconds plusNanos
LocalDateTime ldt4 = ldt.plusYears(2);
LocalDateTime ldt5 = ldt.plusMinutes(3);
// 4、减多少:
// minusDays minusYears minusMonths minusWeeks minusHours
minusMinutes minusSeconds minusNanos
LocalDateTime ldt6 = ldt.minusYears(2);
LocalDateTime ldt7 = ldt.minusMinutes(3);
// 5、获取指定日期和时间的LocalDateTime对象:
// public static LocalDateTime of(int year, Month month, int
dayOfMonth, int hour,
// int minute, int second, int
nanoOfSecond)
LocalDateTime ldt8 = LocalDateTime.of(2029, 12, 12, 12, 12, 12,
1222);
LocalDateTime ldt9 = LocalDateTime.of(2029, 12, 12, 12, 12, 12,
1222);
// 6、 判断2个日期、时间对象,是否相等,在前还是在后: equals、
isBefore、isAfter
System.out.println(ldt9.equals(ldt8));
System.out.println(ldt9.isAfter(ldt));
System.out.println(ldt9.isBefore(ldt));
// 7、可以把LocalDateTime转换成LocalDate和LocalTime
// public LocalDate toLocalDate()
// public LocalTime toLocalTime()
// public static LocalDateTime of(LocalDate date, LocalTime time)
LocalDate ld = ldt.toLocalDate();
LocalTime lt = ldt.toLocalTime();
LocalDateTime ldt10 = LocalDateTime.of(ld, lt);
}
}