static工具类(个人学习笔记黑马学习)

package com.itheima.d1_static_util;

import java.util.Random;

/**
 * 工具类
 */
public class ItheimaUtil {
    public static String createVerifyCode(int n){
        //开发一个验证码
        String code="";
        String data="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random r=new Random();
        for (int i = 0; i < n; i++) {
            int index = r.nextInt(data.length());
            code+=data.charAt(index);
        }

        return code;
    }
}
package com.itheima.d1_static_util;

import java.util.Random;

public class Login {
    public static void main(String[] args) {
        //开发一个验证码

        System.out.println(ItheimaUtil.createVerifyCode(5));
    }
}
package com.itheima.d1_static_util;

public class Check {
    public static void main(String[] args) {
        //开发一个验证码

        System.out.println(ItheimaUtil.createVerifyCode(6));
    }
}

注意:需要将工具类的构造器私有化,显得专业

package com.itheima.d1_static_util;

import java.util.Random;

/**
 * 工具类
 */
public class ItheimaUtil {

    /**
     * 注意:由于工具类无需创建对象,所有把其构造器私有化
     */
    private ItheimaUtil(){

    }

    public static String createVerifyCode(int n){
        //开发一个验证码
        String code="";
        String data="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random r=new Random();
        for (int i = 0; i < n; i++) {
            int index = r.nextInt(data.length());
            code+=data.charAt(index);
        }

        return code;
    }
}

 

练习

 需求:在实际开发中,经常会遇到一些数组使用的工具类。请按照如下要求编写一个数组的工具类:ArraysUtils

  • 我们知道数组对象直接输出的时候是输出对象的地址的,而项目中很多地方都需要返回数组的内容,请在ArraysUtils中提供一个工具类方法toString,用于返回整数数组的内容,返回的字符串格式如:[10,20,50,34.100] (只考虑整数数组,且只考虑一维数组)
  • 经常需要统计平均值,平均值为去掉最低分和最高分后的分值,请提供这样一个工具方法getAerage,用于返回平2均分。(只考虑浮点型数组,且只考虑一维数组
  • 定义一个测试类TestDemo,调用该工具类的工具方法,并返回结果
package com.itheima.d1_static_util;

/**
 * 完成数组工具类
 */
public class ArrayUtil {
    /**
     * 私有构造器
     */
    private ArrayUtil(){

    }

    /**
     * 工具方法:静态方法
     */
    public static String toString(int[] arr){
        //校验
        if (arr==null){
            return null;
        }

        //拼接并返回
        String result="[";
        for (int i = 0; i < arr.length; i++) {
            result +=(i==arr.length-1?arr[i]:arr[i]+",");

        }
        result+="]";
        return result;
    }
}
package com.itheima.d1_static_util;

public class TestDemo2 {
    public static void main(String[] args) {
        int[] arr=null;
        int[] arr1={};
        int[] arr2={12,34,56};

        System.out.println(ArrayUtil.toString(arr));
        System.out.println(ArrayUtil.toString(arr1));
        System.out.println(ArrayUtil.toString(arr2));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值