形参是对象,返回类型是对象数组

2018-07-17  22:47:22 

 1 package test3_student;
 2 /**
 3  * 学生类
 4  * @author ljj
 5  *
 6  */
 7 public class Student {
 8     public String name;//学生名
 9     public int age;//年龄
10     public String gender;//性别
11 }
 1 package test3_student;
 2 /**
 3  * 学生的过渡类
 4  * @author ljj
 5  *
 6  */
 7 public class StudentImpl {
 8     
 9     Student[] stu = new Student[5];
10     //添加学生对象的方法
11     public Student[] addStu(Student student){
12         //把学生对象添加到学生数组中
13         for(int i=0;i<stu.length;i++){
14             //添加到空的位置
15             if(null==stu[i]){
16                 stu[i]=student;
17                 //添加完成后结束
18                 break;
19             }
20         }
21         return stu;
22     }
23 
24 }
 1 package test3_student;
 2 /**
 3  * 学生测试类
 4  * @author ljj
 5  *
 6  */
 7 import java.util.Scanner;
 8 public class StudentTest {
 9 
10     /**
11      * @param args
12      */
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         Scanner input = new Scanner(System.in);
16         
17         Student[] s=null;
18         StudentImpl si= new StudentImpl();
19         
20         String answer ="";
21         do{
22             Student student = new Student();
23             System.out.println("请输入学生姓名:");
24             student.name = input.next();
25             System.out.println("请输入年龄:");
26             student.age = input.nextInt();
27             System.out.println("请输入性别:");
28             student.gender = input.next();
29             //调用添加对象的方法
30             s=si.addStu(student);
31             
32             //用户选择是否继续
33             System.out.println("是否继续添加:y/n");
34             answer=input.next();
35             
36         }while("y".equals(answer));
37         
38         //输出添加成功的学生信息
39         System.out.println("学生信息如下;");
40         for(int i=0;i<s.length;i++){
41             if(null!=s[i]){
42                 System.out.println("姓名:"+s[i].name);
43                 System.out.println("年龄:"+s[i].age);
44                 System.out.println("性别:"+s[i].gender);
45             }
46         }
47 
48     }
49 
50 }
年轻人能为世界年轻人能为世界做些什么
原文地址:https://www.cnblogs.com/twinkle-star/p/9321407.html