34-Java:定义工具类

定义工具类

  1. 例1
    在这里插入图片描述
package a02staticdemo2;

public class ArrayUtil {
    
    
    //私有化构造方法
    //目的是不让外界创建它的对象

    private ArrayUtil(){
    
    }

    //需要定义为静态的,方便调用
    public static  String printArr(int[] arr){
    
    
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
    
    
            if (i == arr.length -1){
    
    
                sb.append(arr[i]);
            }else {
    
    
                sb.append(arr[i]).append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static double getValue(double[] arr){
    
    
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
    
    
            sum = sum + arr[i];
        }
        return sum/arr.length;
    }
}

package a02staticdemo2;

public class testdemo {
    
    
    public static void main(String[] args) {
    
    
        //测试工具类中的方法是否正确
        int[] arr1 = {
    
    1,2,3,4,5};
        //静态方法:类名直接调用
        String str = ArrayUtil.printArr(arr1);
        System.out.println(str);

        double[] arr2 = {
    
    1.2,2.3,3.4,4.5,5.6};
        double ave = ArrayUtil.getValue(arr2);
        System.out.println(ave);
    }
}

在这里插入图片描述


  1. 例2
  • 在这里插入图片描述
package a03staticdemo3;

public class Student {
    
    
    private String name;
    private int age;
    private String gender;

    public Student() {
    
    
    }

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

    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 String getGender() {
    
    
        return gender;
    }

    public void setGender(String gender) {
    
    
        this.gender = gender;
    }
}

package a03staticdemo3;

import java.util.ArrayList;

public class StudentUtil {
    
    
    private StudentUtil() {
    
    
    }
    //静态方法
    public static int getMaxAgeStudent(ArrayList<Student> list){
    
    
        //定义一个参照物
        int max = list.get(0).getAge();
        //循环遍历集合
        for (int i = 1; i < list.size(); i++) {
    
    
            int temp = list.get(i).getAge();
            if (temp > max){
    
    
                max = temp;
            }
        }

        return max;
    }
}

package a03staticdemo3;

import java.util.ArrayList;

public class testdemo {
    
    
    public static void main(String[] args) {
    
    
        //创建集合
        ArrayList<Student> list = new ArrayList<>();
        //创建学生对象
        Student stu1 = new Student("zhangsan",23,"男");
        Student stu2 = new Student("lisi",24,"女");
        Student stu3 = new Student("wangwu",25,"男");
        //把对象添加到集合中
        list.add(stu1);
        list.add(stu3);
        list.add(stu3);

        int maxAgeStudent = StudentUtil.getMaxAgeStudent(list);
        System.out.println(maxAgeStudent);
    }
}

猜你喜欢

转载自blog.csdn.net/u014217137/article/details/129516614