创建Person类为父类,键盘输入姓名,年龄,数组列表存储。完成之后,遍历输出。- import java.util.*;
- public class Test1 extends Person {
- public Test1(String name, String age) {
- super(name, age);
- }
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- List<Person> list=new ArrayList<Person>();
- while(true){
- System.out.println("请输入姓名和年龄:");
- String s=sc.nextLine();
- if("over".equalsIgnoreCase(s)){
- break;
- }
- String[]st=s.split(" +");
- list.add(new Person(st[0],st[1]));
- }
- Iterator<Person> it=list.iterator();
- while(it.hasNext()){
- Person p=it.next();
- System.out.println(p.getName()+"---"+p.getAge());
- }
- }
- }
复制代码 |
|