鱼C论坛

 找回密码
 立即注册
查看: 864|回复: 7

[已解决]java方法的调用

[复制链接]
发表于 2022-12-10 13:53:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大佬帮我看看我用笔标注的地方那里是什么意思
最佳答案
2022-12-10 18:29:54
本帖最后由 holistic杀手 于 2022-12-10 18:35 编辑
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. //测试学生类
  4. public class StudentTest {
  5.     public static void main(String[] args) {
  6.         ArrayList list = new ArrayList();

  7.         System.out.println("添加学生");
  8.         Student.addStudent(list);
  9.         Student.addStudent(list);
  10.         Student.addStudent(list);


  11.         for (int i=0;i<list.size();i++){
  12.             System.out.println(list.get(i));
  13.         }
  14.     }
  15. }


  16. //学生类
  17. class Student {
  18.     String name;
  19.     String uid;
  20.     public Student(String name,String uid){
  21.         this.name= name;
  22.         this.uid=uid;
  23.     }

  24.     public static void addStudent(ArrayList list){
  25.         Scanner s = new Scanner(System.in);
  26.         System.out.print("输入学号:");
  27.         String uid = s.next();
  28.         System.out.print("输入姓名:");
  29.         String name= s.next();

  30.         Student stu = new Student(name,uid);
  31.         list.add(stu);
  32.         System.out.println("添加成功");
  33.     }

  34.     @Override
  35.     public String toString() {
  36.         return this.name+","+this.uid;
  37.     }
  38. }



复制代码


我输入的学生信息添加到了列表里面,遍历出来了。
屏幕截图 2022-12-10 135214.png
屏幕截图 2022-12-10 135236.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-10 14:11:22 | 显示全部楼层
不知道你想问什么,添加学生的方法的参数是一个数组列表,通过控制台键入信息完成一个学生类型对象的创建,然后把这个学生对象添加到列表上,你可以把代码都发出来看看,不知道有没有帮到你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-10 14:30:48 | 显示全部楼层
holistic杀手 发表于 2022-12-10 14:11
不知道你想问什么,添加学生的方法的参数是一个数组列表,通过控制台键入信息完成一个学生类型对 ...

就是控制台1下面调用addStudent(list)  为什么括号里面要加集合
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2022-12-10 16:34:36 | 显示全部楼层
主方法哪里是不是有创建过一个数组列表吧,这里作为参数传进去,是把对象放到列表上的一个操作
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-10 17:42:03 | 显示全部楼层
holistic杀手 发表于 2022-12-10 16:34
主方法哪里是不是有创建过一个数组列表吧,这里作为参数传进去,是把对象放到列表上的一个操作

还是没懂什么意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-10 18:23:32 | 显示全部楼层
本帖最后由 holistic杀手 于 2022-12-10 18:33 编辑



我是这样测试的,主方法里new了一个ArrayList对象
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-10 18:29:54 | 显示全部楼层    本楼为最佳答案   
本帖最后由 holistic杀手 于 2022-12-10 18:35 编辑
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. //测试学生类
  4. public class StudentTest {
  5.     public static void main(String[] args) {
  6.         ArrayList list = new ArrayList();

  7.         System.out.println("添加学生");
  8.         Student.addStudent(list);
  9.         Student.addStudent(list);
  10.         Student.addStudent(list);


  11.         for (int i=0;i<list.size();i++){
  12.             System.out.println(list.get(i));
  13.         }
  14.     }
  15. }


  16. //学生类
  17. class Student {
  18.     String name;
  19.     String uid;
  20.     public Student(String name,String uid){
  21.         this.name= name;
  22.         this.uid=uid;
  23.     }

  24.     public static void addStudent(ArrayList list){
  25.         Scanner s = new Scanner(System.in);
  26.         System.out.print("输入学号:");
  27.         String uid = s.next();
  28.         System.out.print("输入姓名:");
  29.         String name= s.next();

  30.         Student stu = new Student(name,uid);
  31.         list.add(stu);
  32.         System.out.println("添加成功");
  33.     }

  34.     @Override
  35.     public String toString() {
  36.         return this.name+","+this.uid;
  37.     }
  38. }



复制代码


我输入的学生信息添加到了列表里面,遍历出来了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-12-10 19:03:26 | 显示全部楼层
holistic杀手 发表于 2022-12-10 18:29
我输入的学生信息添加到了列表里面,遍历出来了。

懂了,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 06:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表