- import java.util.ArrayList;
- import java.util.Scanner;
- class Course{
- //如何从分别键盘中录入一个人的信息(姓名、年龄、性别、电话、住址),并封装成一个对象,最后把对象存储在集合中。并做简单的代码实现。
- public static void main(String[] args){
- ArrayList<Student> al = new ArrayList<>();
- Scanner sc = new Scanner(System.in);
- System.out.println("请按格式输入信息:姓名,年龄,性别,电话,地址");
- String s = sc.nextLine();
- String[] arr = s.split(",");
- al.add(new Student(arr[0],Integer.parseInt(arr[1]),arr[2],arr[3],arr[4]));
- }
- }
- class Student {
- private String name;
- private int age;
- private String sex;
- private String tel;
- private String add;
- public Student() {
- super();
-
- }
- public Student(String name, int age, String sex, String tel, String add) {
- super();
- this.name = name;
- this.age = age;
- this.sex = sex;
- this.tel = tel;
- this.add = add;
- }
- 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 getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public String getTel() {
- return tel;
- }
- public void setTel(String tel) {
- this.tel = tel;
- }
- public String getAdd() {
- return add;
- }
- public void setAdd(String add) {
- this.add = add;
- }
- @Override
- public String toString() {
- return "Student [name=" + name + ", age=" + age + ", sex=" + sex
- + ", tel=" + tel + ", add=" + add + "]";
- }
-
- }
复制代码 |