实用类

发布时间 2023-09-26 08:43:38作者: 韩世康

1、枚举

  1. 枚举类型本质上也是⼀种类,只不过这个类的对象是固定的⼏个,不能随意让⽤户创建。
  2. 【修饰符】 enum 枚举类名 { 常⽤的对象列表 }。
  3. package com.student;
    
    public enum Gender {
        男,女
    }
    package com.student;
    
    public class Student {
        private String name;
        private Gender gender;
    
        public Student() {
        }
    
        public Student(String name, Gender gender) {
            this.name = name;
            this.gender = gender;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Gender getGender() {
            return gender;
        }
    
        public void setGender(Gender gender) {
            this.gender = gender;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", gender='" + gender + '\'' +
                    '}';
        }
    }
    package com.student;
    
    public class StudentTest {
        public static void main(String[] args) {
            Student s1 = new Student("文萱", Gender.女);
            Student s2 = new Student("文轩", Gender.男);
            System.out.println(s1);
            System.out.println(s2);
        }
    }

     

2、包装类

  1. 数据类型: 基本数据类型和引⽤数据类型, 使⽤基本数据类型在于效率,想要使⽤只针对对象设计的⽅法,那么基本数据类型的数据就需要⽤包装类来包装。
  2. 基本数据 -> 包装类(java.lang包)
  3. 1. byte Byte
    2. short Short
    3. int Integer
    4. long Long
    5. float Float
    6. double Double
    7. char Character
    8. boolean Boolean
  4. 例子
    package com.packaginclass;

    import java.util.ArrayList;
    import java.util.List;

    public class Demo01 {
    public static void main(String[] args) {
    //定义 基本数据类型
    int i = 1;
    System.out.println(i);
    double j = 9.99;
    System.out.println(j);
    //包装类 有两个作用:
    //1、可以使用相应的方法
    //2、集合中无法存放基本数据类型,把基本数据类型转换为包装类放入集合中
    List<Integer> list = new ArrayList<>();
    //使用包装类的构造方法
    //1、以每个包装类对应的基本数据类型作为参数
    //基本数据类型----->包装类型
    int iNum = 9;
    Integer integer = new Integer(iNum);
    Integer integer1 = iNum;//自动封装
    Integer integer2 = 9;
    list.add(iNum);//自动封装
    list.add(integer);
    list.add(integer1);
    double dNum = 3.14;
    Double d = new Double(dNum);
    Double d1 = new Double(3.14);
    //2、以字符串作为参数,当Number类型包装类构造参数作为字符串,字符串必须是能装换高维Number数字的字符串,否则报错
    //且字符串不能为null,除了Character之外都可以用字符串作为参数
    Integer integer3 = new Integer("123");
    Double d2 = new Double("9.8");
    Float f = new Float("9.8f");
    System.out.println(f);
    //boolean类型的构造方法参数为字符串时,除了true之外的其他字符串,结果都为false;
    Boolean b1 = new Boolean("true");
    System.out.println(b1);
    Boolean b2 = new Boolean("ASd");
    System.out.println(b2);
    Boolean b3 = new Boolean("false");
    System.out.println(b3);
    }

    }

     

3、包装类的常⽤⽅法

package com.packaginclass;
//包装累的常用方法
public class Demo02 {
    public static void main(String[] args) {
        //1、xxxValue()  包装类->基本数据类型
        Integer i=new Integer(100);
        int iNum = i.intValue();
        System.out.println(iNum);
        Boolean b =new Boolean(false);
        boolean b2=b.booleanValue();
        System.out.println(b2);
        //2、toString()   基本数据类型->字符串类型
        int num =100;
        String sNum=Integer.toString(num);
        System.out.println(sNum);
        boolean b3= true;
        String s =Boolean.toString(b3);
        System.out.println(s);
        String sNum2= 100+"";
        System.out.println(sNum2);
        String sNum3= num+"";
        System.out.println(sNum2);
        String sNum4= b3+"";
        System.out.println(sNum2);
        String sNum5= true+"";
        System.out.println(sNum2);
        //3、parseXXX()   字符串->基本数据类型(Character除外)
        String age ="18";
        int ageNum = Integer.parseInt(age);
        System.out.println(ageNum);
        String score ="68.0";
        double dScore = Double.parseDouble(score);
        System.out.println(dScore);
        //4、valueOf(基本数据类型)   基本数据类型-->包装类
        int j =68;
        Integer integer =Integer.valueOf(j);
        //注意:当包装类为Number时,参数字符串必须为数字兼容的字符串,否则NumberFormatException
        String flags="true";
        String flags1="True";
        String flags2="java";
        Boolean bFlag=Boolean.valueOf(flags);
        System.out.println(bFlag);
    }
}

4、拆箱和装箱

package com.packaginclass;

import java.util.ArrayList;
import java.util.List;

public class Demo03 {
    public static void main(String[] args) {
        //包装类 --> 基本数据类型 自动转换  --->拆箱
        Integer i =new Integer(100);
        Integer i2=100;//自动装箱
        int iNum =i;//拆箱 自动拆箱
        int iNum2 =i.intValue();//手动拆箱
        //基本数据类型--> 包装类型 自动转换--> 装箱
        int j=68;
        Integer j1=Integer.valueOf(j);//手动装箱
        Integer j2=j;
        //集合在只能存储包装类型,不能存储基本类型
        List list =new ArrayList<>();
        int num=100;
        Integer numInterger =new Integer(num);
        list.add(numInterger);
        list.add(num);   //将 int 100->封装Interger 100;
    }
}

5、math类

package com.packaginclass;
//math类的常用方法
public class Demo04 {
    public static void main(String[] args) {
        System.out.println(Math.abs(-8.7));
        System.out.println(Math.max(100,90));
    }
}

6、Random

package com.packaginclass;

import java.util.Random;

//Random生成随机数
public class Demo05 {
    public static void main(String[] args) {
        //创建一个对象,随机数生成器
        //1.随机数生成器的种子不同,每次生成的随机数不同,否则相同
        Random random= new Random(1);
        Random random1=new Random();
        System.out.println(random.nextInt(10));
        System.out.println(random1.nextInt(100));
        System.out.println("======================");
        for (int i = 0; i < 10; i++) {
            Random random2 =new Random(System.currentTimeMillis());
            System.out.println(random2.nextInt(100));
        }
        System.out.println("======================");
        for (int i = 0; i < 10; i++) {
            Random random2 =new Random(System.nanoTime());
            System.out.println(random2.nextInt(100));
        }

    }
}