Skip to content

Commit c0a90ef

Browse files
authored
Merge pull request mouredev#1886 from raulfauli/08-dart
#8 - Dart
2 parents a61b653 + ee69ba4 commit c0a90ef

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/// #08 CLASES
2+
3+
void main() {
4+
final person = Person('Raul', 25);
5+
person.age = 101; // Print 'Invalid age'
6+
person.printPerson(); // Age: 25
7+
8+
// Public constant
9+
print('Max age: ${Person.MAX_AGE}');
10+
// Static method
11+
Person.printMaxAge(); // Max age: 100
12+
13+
// Setter
14+
person.age = 40;
15+
person.printPerson(); // Age: 40
16+
17+
// Getter
18+
final age = person.age;
19+
print('Age: $age'); // Age: 40
20+
21+
// Name constructor
22+
final anonym = Person.anonym();
23+
anonym.printPerson(); // Name: Anonymous, Age: 18
24+
25+
// Extra
26+
27+
// Queue
28+
final queue = Queue();
29+
queue.add('Raul');
30+
queue.add('Fauli');
31+
queue.add('Garcia');
32+
print(queue.get()); // Raul
33+
print(queue.getAll()); // [Fauli, Garcia]
34+
35+
// Stack
36+
final stack = Stack();
37+
stack.add('Raul');
38+
stack.add('Fauli');
39+
stack.add('Garcia');
40+
print(stack.get()); // Garcia
41+
print(stack.getAll()); // [Raul, Fauli]
42+
}
43+
44+
// Clase con constructor
45+
class Person {
46+
// Constantes
47+
static const int MAX_AGE = 100;
48+
// Atributos
49+
String name;
50+
// Atributo privado
51+
int _age = 0;
52+
53+
// Constructor
54+
Person(this.name, this._age);
55+
56+
// Constructor con nombre
57+
Person.anonym()
58+
: name = 'Anonymous',
59+
_age = 18;
60+
61+
// Métodos
62+
void printPerson() {
63+
print('Name: $name, Age: $_age');
64+
}
65+
66+
// Getters y Setters
67+
int get age => _age;
68+
set age(int value) {
69+
if (value <= MAX_AGE && value >= 0) {
70+
_age = value;
71+
} else {
72+
print('Invalid age');
73+
}
74+
}
75+
76+
// Métodos estáticos
77+
static void printMaxAge() {
78+
print('Max age: $MAX_AGE');
79+
}
80+
81+
// Métodos privados
82+
void _privateMethod() {
83+
print('Private method');
84+
}
85+
86+
void publicMethod() {
87+
_privateMethod();
88+
}
89+
}
90+
91+
class Queue {
92+
final List<String> _queue = [];
93+
94+
void add(String value) {
95+
_queue.add(value);
96+
}
97+
98+
String? get() {
99+
if (_queue.isEmpty) {
100+
return null;
101+
}
102+
return _queue.removeAt(0);
103+
}
104+
105+
List<String> getAll() {
106+
return _queue;
107+
}
108+
}
109+
110+
class Stack {
111+
final List<String> _stack = [];
112+
113+
void add(String value) {
114+
_stack.add(value);
115+
}
116+
117+
String? get() {
118+
if (_stack.isEmpty) {
119+
return null;
120+
}
121+
return _stack.removeLast();
122+
}
123+
124+
List<String> getAll() {
125+
return _stack;
126+
}
127+
}

0 commit comments

Comments
 (0)