用java语言写1、定义一个Student类,具有name、sex、age属性,具有getName、setName、getSex、setSex、getAge、setAge方法和三个参数的构造方法 2、编写一个类,名字为ListDemo,在main方法中做以下工作: 定义一个可以保存Student类型对象的List类型对象list1,然后向list1中放入2个学生:new Student("张三","男",20)、new Student("李四","男",22) 遍历list1中的学生对象,并将每个学生的姓名、性别、年龄输出到显示器
时间: 2023-07-04 12:16:23 浏览: 110
以下是Java的实现:
```java
public class Student {
private String name;
private String sex;
private int age;
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class ListDemo {
public static void main(String[] args) {
List<Student> list1 = new ArrayList<>();
list1.add(new Student("张三", "男", 20));
list1.add(new Student("李四", "男", 22));
for (Student student : list1) {
System.out.println("姓名:" + student.getName() + ",性别:" + student.getSex() + ",年龄:" + student.getAge());
}
}
}
```
输出结果为:
```
姓名:张三,性别:男,年龄:20
姓名:李四,性别:男,年龄:22
```
阅读全文