java进阶static -修饰成员方法 - 工具类26
package StaticM; public class static01 { String name; int age; static String school;// public static01() { } public static01(String name, int age, String school) { this.name = name; this.age = age; this.school = school; } /** * 获取 * @return name */ public String getName() { return name; } /** * 设置 * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * @return age */ public int getAge() { return age; } /** * 设置 * @param age */ public void setAge(int age) { this.age = age; } /** * 获取 * @return school */ public String getSchool() { return school; } /** * 设置 * @param school */ public void setSchool(String school) { this.school = school; } public String toString() { return "static01{name = " + name + ", age = " + age + ", school = " + school + "}"; } }
package StaticM; public class StaticDemo { public static void main(String[] args) { static01 stu = new static01(); stu.name = "张三"; stu.age = 23; static01.school = "清华";//建议用类名调用 System.out.println(stu.name+stu.age+static01.school); static01 stu2 = new static01(); stu2.name = "张三"; stu2.age = 23; System.out.println(stu2.name+stu2.age+stu2.school); } }
工具类:
package com.tools; public class toolsArr { private toolsArr() { } ; public static int max(int arr[]) { int max = arr[0]; for (int i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } public static int min(int arr[]) { int min = arr[0]; for (int i = 0; i < arr.length; i++) { if (arr[i] < min) { min = arr[i]; } } return min; } public static void sharedArray(int arr[]) { System.out.print("["); for (int i = 0; i < arr.length - 1; i++) { System.out.print(arr[i] + ", "); } System.out.print(arr[arr.length - 1] + "]"); } }
package com.tools; public class tools { // 1.成员方法什么时候加入 // static常用于制作工具类 // 2.工具类:不是描述事物的,而是帮我们完成一些事情(打工) // 3.如果发现一个类中,所有的方法,全都是 static所修饰私有该类的构造方法 // 目的:为了不让其他类,再创建对象 public static void main(String[] args) { int arr[] = {123, 456, 789}; System.out.println(toolsArr.max(arr)); System.out.println(toolsArr.min(arr)); toolsArr.sharedArray(arr); } }
代码改变了我们,也改变了世界