System系统工具类
-
System
提供了一些与系统相关的方法。 -
该类的成员都是静态的,通过类名就可以直接调用
System 类的常用方法
方法名 | 说明 |
---|---|
public static void exit(int status) | 终止当前运行的Java虚拟机。参数非零表示异常终止 |
public static long currentTimeMillis() | 返回当前时间与1970年1月1日0:0:0之间的时间差异(以毫秒为单位) |
public static void arraycopy(源数组,源起始索引,目的数组,目的起始索引,拷贝个数) | 从源数组的源索引拷贝到目的数组的目的索引 |
程序执行耗时
long start = System.currentTimeMillis();
/*
程序
*/
long end = System.currentTimeMillis();
Sout: "耗时:" + (end - start) + "毫秒";
拷贝引用类型数组
源数组和目的数组都是引用数据类型时,子类类型可以拷贝给父类类型
public class Demo {
Student s1 = new Student("张三", 25);
Student s2 = new Student("李四", 28);
Student s3 = new Student("王五", 32);
Student[] arr1 = {s1, s2, s3};
Person[] arr2 = new Person[3];
//把arr1中的对象的地址值拷贝给arr2
System.arraycopy(arr1, 0, arr2, 0, 3);
//遍历
for(int i = 0; i < arr2.length; i++) {
//需要强转为子类类型
Student s = (Student)arr2[i];
System.out.println(s.getName + s.getAge);
}
}
class Person {
private String name;
private int age;
public Person {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public String getAge() {
return age;
}
}
class Student extends Person {
public Student() {}
public Student(String name, int age) {
super(name, age);
}
}