1+ import java .util .ArrayList ;
2+ import java .util .List ;
3+
4+ public class Generics {
5+
6+
7+ }
8+
9+
10+ class Student {
11+
12+
13+ }
14+
15+ class JuniorStudent extends Student {
16+
17+ }
18+
19+ class SeniorStudent extends Student {
20+
21+ }
22+
23+ class ExamProcessorOld {
24+ public void processJuniorResults (JuniorStudent [] students ) {
25+
26+ }
27+
28+ public void processSeniorResults (SeniorStudent [] students ) {
29+
30+ }
31+
32+ public void processStudentResults (Object [] students ) {
33+
34+ }
35+
36+
37+ }
38+
39+ interface ExamProcessor <Y >
40+ {
41+ void processResult (Y student );
42+ void processResult (Y student ,double height , String fullname );
43+ void processResults (Y [] students );
44+ void processResults2 (List <Y > students );
45+
46+ //JuniorStudent getJuniorStudent(int index);
47+ //SeniorStudent getSeniorStudent(int index);
48+
49+ Y getStudent (int index );
50+ }
51+
52+ interface ExamProcessorInterface
53+ {
54+ void processResult (JuniorStudent student );
55+ void processResult (SeniorStudent student );
56+ void processResults (JuniorStudent [] students );
57+ void processResults (SeniorStudent [] students );
58+ void processResults2 (List <JuniorStudent > students );
59+ void processResults3 (List <SeniorStudent > student );
60+ }
61+
62+ class ExamProcessor2 <T > implements ExamProcessor <T >
63+ {
64+ List <T > list = new ArrayList <T >();
65+
66+ public void processResult (T student ) {
67+ list .add (student );
68+ }
69+
70+ public void processResult (T student ,double height , String fullname )
71+ {
72+
73+ }
74+
75+ public void processResults (T [] students ) {
76+ for (T item : students ) {
77+ list .add (item );
78+ }
79+ }
80+
81+ public void processResults2 (List <T > students ) {
82+ list .addAll (students );
83+ }
84+
85+ public T getStudent (int index )
86+ {
87+ return null ;
88+ }
89+ }
90+
91+
92+ class StudentsList {
93+
94+ ExamProcessor2 <JuniorStudent > juniorStudentExamProcessor = new ExamProcessor2 <>();
95+ ExamProcessor2 <SeniorStudent > seniorStudentExamProcessor2 = new ExamProcessor2 <SeniorStudent >();
96+
97+ private JuniorStudent [] list = new JuniorStudent [100 ];
98+
99+ private List list2 = new ArrayList ();
100+
101+ private List <JuniorStudent > juniorStudents = new ArrayList <JuniorStudent >();
102+ private ArrayList <SeniorStudent > seniorStudents = new ArrayList <SeniorStudent >();
103+
104+ public void addStudent (int index , JuniorStudent student ) {
105+ list [index ] = student ;
106+ }
107+
108+ public JuniorStudent getStudent (int index ) {
109+ return list [index ];
110+ }
111+
112+ public void addJuniorStudent (JuniorStudent student ) {
113+ list2 .add (student );
114+ }
115+
116+ public void addJuniorStudent (int index ,JuniorStudent student ) {
117+ juniorStudents .add (index ,student );
118+ }
119+ public void addJuniorStudent2 (JuniorStudent student ) {
120+ juniorStudents .add (student );
121+
122+ //SeniorStudent seniorStudent=new SeniorStudent();
123+ //juniorStudents.add(seniorStudent);
124+ }
125+
126+ public void addSeniorStudent (SeniorStudent student ) {
127+ list2 .add (student );
128+ }
129+
130+ //5
131+ public JuniorStudent getJuniorStudent (int index ) {
132+ JuniorStudent student = (JuniorStudent ) list2 .get (index );
133+ return student ;
134+ //return list2.get(index);
135+ }
136+
137+ //type safety
138+ public JuniorStudent getJuniorStudent2 (int index ) {
139+ JuniorStudent student = juniorStudents .get (index );
140+ return student ;
141+ //return list2.get(index);
142+
143+ }
144+
145+ public SeniorStudent getSeniorStudent (int index ) {
146+ SeniorStudent student = (SeniorStudent ) list2 .get (index );
147+
148+ return student ;
149+ //return list2.get(index);
150+ }
151+
152+ public void processJuniorResults (List <JuniorStudent > students ) {
153+ juniorStudentExamProcessor .processResults2 (students );
154+
155+ }
156+
157+ }
0 commit comments