用java编写一个类,
发布网友
发布时间:2022-05-03 07:46
我来回答
共1个回答
热心网友
时间:2023-10-14 22:24
import java.util.ArrayList;
import java.util.List;
class Course{
String courseName;
List<String> studentName;
Course(String courseName){
this.courseName = courseName;
this.studentName = new ArrayList<String>();
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public List<String> getStudentName() {
return studentName;
}
public void setStudentName(List<String> studentName) {
this.studentName = studentName;
}
}
/**
*
* @author ww
*
*/
public class TestCourse {
/**
*
* @param args
*/
public static void main(String[] args) {
Course course1 = new Course("高数");
course1.getStudentName().add("张三");
course1.getStudentName().add("李四");
course1.getStudentName().add("赵五");
Course course2 = new Course("英语");
course2.getStudentName().add("张三");
course2.getStudentName().add("李四");
course2.getStudentName().add("刘六");
System.out.println("课程名:"+course1.getCourseName());
System.out.print("学生名单:");
for(String studentName:course1.getStudentName()){
System.out.print(studentName+"\t");
}
System.out.println();
System.out.println("课程名:"+course2.getCourseName());
System.out.print("学生名单:");
for(String studentName:course2.getStudentName()){
System.out.print(studentName+"\t");
}
System.out.println();
}
}