package kr.or.dit.basic;
import java.util.Arrays;
public class T07_WildCardTest {
public static void registorCourse(Course<?> course) {
System.out.println(course.getName()+"수강생 :"+Arrays.toString(course.getStudents()));
}
public static void registorCourseStudent(Course<? extends Student> course) {
System.out.println(course.getName()+"수강생 :"+Arrays.toString(course.getStudents()));
}
public static void registerCourseWorker(Course<? super Worker> course) {
System.out.println(course.getName()+"수강생 :"+Arrays.toString(course.getStudents()));
}
public static void main(String[] args) {
Course<Person> personCourse = new Course<>("일반인 과정",5);
personCourse.add(new Person("일반인1"));
personCourse.add(new Worker("직장인1"));
personCourse.add(new Student("학생1"));
personCourse.add(new HighStudent("고등학생1"));
Course<Worker> workerCourse = new Course("직장인과정", 5);
workerCourse.add(new Worker("직장인1"));
Course<Student> studentCourse = new Course("학생과정",5);
studentCourse.add(new Student("학생1"));
studentCourse.add(new HighStudent("고등학생1"));
Course<HighStudent> highstudentcourse = new Course<>("고등학생과정",5);
highstudentcourse.add(new HighStudent("고등학생1"));
registorCourse(personCourse);
registorCourse(workerCourse);
registorCourse(studentCourse);
registorCourse(highstudentcourse);
System.out.println("--------------------------------------------------");
registorCourseStudent(studentCourse);
registorCourseStudent(highstudentcourse);
System.out.println("--------------------------------------------------");
registerCourseWorker(personCourse);
registerCourseWorker(workerCourse);
System.out.println("--------------------------------------------------");
}
}
class Person{
String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
class Worker extends Person{
public Worker(String name) {
super(name);
}
}
class Student extends Person{
public Student(String name) {
super(name);
}
}
class HighStudent extends Student{
public HighStudent(String name) {
super(name);
}
}
class Course<T> {
private String name;
private T[] students;
public Course(String name, int capacity) {
this.name = name;
students =(T[])(new Object[capacity]);
}
public T[] getStudents() {
return students;
}
public String getName() {
return name;
}
public void add(T t) {
for(int i=0; i<students.length; i++) {
if(students[i] == null) {
students[i]=t;
break;
}
}
}
}