//测试类,用来执行代码
package homework;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] a={4,5,3,6,8,1};//自己初始化数组
int[] b=new int[10];//自行输入数组数字
Scanner scanner=new Scanner(System.in);
int i=0;
//输入数组b中内容,直至输入0退出
while(true){
System.out.println("请输入一个数字,输入0退出");
b[i]=scanner.nextInt();
if(b[i]==0)
{
break;
}
i++;
}
//调用静态方法执行相应功能
ArrUtill.arrPrint(a);
int sum1=ArrUtill.getSum(a);
int sum2=ArrUtill.getSum(b);
System.out.println("求和为:"+sum1);
System.out.println(sum2);
Student.teacherName="sayuri";
Student s1=new Student("asuka",19,"w");
ArrUtill.objectPrint(s1);
}
}
建立一个学生类,定义学生信息
package homework;
//定义学生类
public class Student {
private String name;
private int age;
private String gender;
static String teacherName;
public Student(){
}
public Student(String name,int age,String gende){
this.name=name;
this.age=age;
this.gender=gende;
}
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 void setGender(String gender){
this.gender=gender;
}
public String getGender(){
return gender;
}
}
建立自己需要的静态方法
package homework;
public class ArrUtill {
private ArrUtill(){}//设置为私有
//输出整数数组的静态方法
public static void arrPrint(int[] arr){
for (int j : arr) {
System.out.print(j+" ");
}
System.out.println();
}
//求和的静态方法
public static int getSum(int[] arr){
int sum=0;
for(int j : arr){
sum+=j;
}
return sum;
}
//输出学生对象内容的静态方法
public static void objectPrint(Student student){
String name=student.getName();
int stuAge= student.getAge();
String gender=student.getGender();
System.out.print(name+" ");
System.out.print(stuAge+" ");
System.out.println(gender);
}
}
标签:静态方法,String,int,gender,System,static,使用,public,name From: https://www.cnblogs.com/yytm/p/18451501