-
Notifications
You must be signed in to change notification settings - Fork 0
/
35-Lists-3.dart
47 lines (36 loc) · 1.57 KB
/
35-Lists-3.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// LIST e GENERICS
void main() {
List<StudentGeneric> studentsGenerics = [StudentGeneric('Valdemar'),StudentGeneric('Jaqueline'),
StudentGeneric('Julia'),StudentGeneric('Sarah'),StudentGeneric('Niels Roberto'),
StudentGeneric('Cecilia'),StudentGeneric('Catarina')];
print(studentsGenerics);
List<Student> students = [Student('Valdemar'),Student('Jaqueline'),
Student('Julia'),Student('Sarah'),Student('Niels Roberto'),
Student('Cecilia'),Student('Catarina')];
print(students);
students.add(Student('outra crianca'));
print(students);
students[7] = Student('Substituida');
print(students);
students.insert(0,Student('Niels Augusto'));
print (students);
students.insert(1,Student('Setsuko'));
print (students);
}
class StudentGeneric<T> { // genérico. Pode escrever qualquer coisa <A>, <X> etc.
final T name;
StudentGeneric(this.name); // construtor
void printName() {
print(name);
}
}
class Student { // genérico. Pode escrever qualquer coisa <A>, <X> etc.
final String name;
Student(this.name); // construtor
@override // sem o metodo toString definido aqui em @override, ou seja, substituindo o original de qualquer class,
String toString() => 'Student: $name'; // a cada chamada de print student ia aparecer a frase "Instance of Student". toString original eh padrao
}
class StudentDynamic { // em vez de usar generico, pode-se usar uma variavel dynamic, mas aih perde a
final dynamic name; // a checagem de tipo que o generico oferece
StudentDynamic(this.name);
}