自学内容网 自学内容网

Java -2

常用API

System

  • 可以获取当前时间,以此计算运行代码的时间
  • 也可以控制代码的结束

 //获取当前时间点-毫秒 1970 1-1 8:00
long num = System.currentTimeMillis();
System.out.println(num);

//系统退出运行
System.exit(0);

Runtime

获取操作系统的线程大小
能从操作系统最多获取多少内存大小
已经获取的获取的内存大小-M 
剩下的内存大小
在固定时间关机与取消
//获取操作系统的线程大小
int nums = Runtime.getRuntime().availableProcessors();
System.out.println(nums);

//能从操作系统最多获取多少内存大小
long l = Runtime.getRuntime().maxMemory();
System.out.println(l / 1024 / 1024);

//已经获取的获取的内存大小
long L = Runtime.getRuntime().totalMemory();
System.out.println(L / 1024 / 1024);

//剩下的内存大小
long LL = Runtime.getRuntime().freeMemory();
System.out.println(LL / 1024 / 1024);

//在固定时间关机与取消
Runtime.getRuntime().exec("shutdown -s -t 3600");
Runtime.getRuntime().exec("shutdown -a");


System.exit(0);

equals

import java.util.Objects;

public class Equals {
    String name;
    int age;

    public Equals(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Equals() {
    }

    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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Equals equals = (Equals) o;
        return age == equals.age && Objects.equals(name, equals.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

public class BiEqual {
    public static void main(String[] args) {

        Equals a1 = new Equals("ax", 11);
        Equals a2 = new Equals("ax", 11);
        boolean w = a1.equals(a2);
        System.out.println(w);
        //这里的比较默认的是对象的地址,如果想要比较具体的参数值需重写
        String s = "ax";
        StringBuilder sb = new StringBuilder("ax");
        boolean equals = s.equals(sb.toString());
        boolean equals1 = s.equals(sb);
        boolean equals2 = sb.equals(s);
        System.out.println(equals);
        System.out.println(equals1);
        System.out.println(equals2);
        //String中的String中的equals方法是先判断是否为字符串,如果不是字符串那再继续比较其他属性
        //StringBuilder中没有重写特定的equals方法只能从obj中继承
        //obj中equals比较的是对象的地址



    }

}

Objects下的equqls可以有非空判断

clone

浅克隆与深克隆(在于引用数据类型数组的更改变化)

对象拷贝:clone方法是protected只可以在子类中被调用,在这里需要重写

并且java类中要继承Cloneable接口

该接口没有抽象方法,是一个标记性接口

Cloneable实现当前类可以被克隆,没有实现当前类的对象不可以被克隆

浅克隆,浅拷贝:

javabean类

public class CloneStudent implements Cloneable {
    String name;
    int age;
    int[] arr;

    public CloneStudent(String name, int age, int[] arr) {
        this.name = name;
        this.age = age;
        this.arr = arr;
    }

    public CloneStudent() {
    }

    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;
    }

    public int[] getArr() {
        return arr;
    }

    public void setArr(int[] arr) {
        this.arr = arr;
    }

    public void getCloneArr() {
        for (int j : arr) {
            System.out.print(j + " ");
        }
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

实现类

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {


        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        CloneStudent cs = new CloneStudent("ax", 19, arr);

        CloneStudent css = (CloneStudent) cs.clone();

        cs.getArr()[0] = 100;

        System.out.println(cs.getAge() + " " + cs.getName());
        cs.getCloneArr();
        System.out.println();
        System.out.println(css.getAge() + " " + css.getName());
        css.getCloneArr();
    }
}

深克隆,深拷贝:

javabean(这里的clone方法需要重写需求)

public class CloneStudent implements Cloneable {
    String name;
    int age;
    int[] arr;

    public CloneStudent(String name, int age, int[] arr) {
        this.name = name;
        this.age = age;
        this.arr = arr;
    }

    public CloneStudent() {
    }

    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;
    }

    public int[] getArr() {
        return arr;
    }

    public void setArr(int[] arr) {
        this.arr = arr;
    }

    public void getCloneArr() {
        for (int j : arr) {
            System.out.print(j + " ");
        }
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        int[] a = this.arr;
        int[] a1 = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            a1[i] = a[i];
        }
        CloneStudent clone = (CloneStudent) super.clone();
        clone.setArr(a1);

        return clone;
    }
}

测试类

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {


        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        CloneStudent cs = new CloneStudent("ax", 19, arr);

        CloneStudent css = (CloneStudent) cs.clone();

        cs.getArr()[0] = 100;

        System.out.println(cs.getAge() + " " + cs.getName());
        cs.getCloneArr();
        System.out.println();
        System.out.println(css.getAge() + " " + css.getName());
        css.getCloneArr();
    }
}

执行结果


原文地址:https://blog.csdn.net/2302_79847831/article/details/142435863

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