Orientados a Objetos
Omar Alba Hernndez
Objetivos
Definir encapsulamiento y sus ventajas
Definir los modificadores public y private y su
uso en programas
Describir las reglas para el alcance de variables
Escribir cdigo que encapsule miembros privados
Explicar el propsito de los constructores
Escribir cdigo que use el constructor por defecto
Escribir cdigo para definir un nuevo constructor y
usarlo
2
Encapsulamiento
Las operaciones y atributos son miembros
Los miembros pueden ser public o private
La mayora o todas las variables deben mantenerse
privadas
Las variables son definidas por mtodos en su
propia clase
La interfaz con otras clases es con una pequea
parte hacia cada una de las otras clases
La implementacin puede cambiar sin modificar la
interfaz
3
Implementando
encapsulamiento
Coloca public o private antecediendo los
miembros
private int myInt;
public String name;
public void getName()
{
return name;
}
4
Implementando
encapsulamiento
modificador public
1 public class PublicExample
2 {
3
public static void main (String args[])
4
{
5
PublicClass pc = new PublicClass();
6
pc. publicInt = 27;
7
pc. publicMethod();
8
}
9 }
1 public class PublicClass
2 {
3
public int publicInt;
4
public void publicMethod()
5
{
6
// method statements
7
}
8 }
Implementando
encapsulamiento
private modifier
1 public class PrivateExample
2 {
3
public static void main (String args[])
4
{
5
PrivateClass pc = new PrivateClass();
6
pc. privateInt = 27;
7
pc. privateMethod();
8
}
9 }
1 class PrivateClass
2 {
3
private int privateInt;
4
private void privateMethod()
5
{
6
System.out.println( privateInt);
7
}
8 }
10
11
12
13
14
15
16
17
18
Ejercicio 1: Escribiendo
aplicaciones encapsuladas
Objetivo
Tareas
19
21
Constructores
Constructores te permiten especificar valores
para los objetos cuando son creados
Has estado usando un constructor por defecto
a lo largo de este curso
El constructor tiene el mismo nombre que la
clase
23
Constructores
1 public class Hat
2 {
3
private String type;
4
public Hat( String hatType)
5
{
6
type = hatType;
7
}
8 }
1 class Order
2 {
3
Hat hat1 = new Hat(" Fedora");
4 }
5
24
25
27
28
Declarando y usando
constructores
Declarando
modificadores Class nombre_clase
{
Nombre_clase([argumentos])
{
[sentencias_inicializain_variables];
}
}
29
Declarando y usando
constructores
Usando
Modificadores class clase
{
nombre_clase ref_objeto = new nombre_clase([valores_argumentos]);
}
30
Declarando y usando
constructores
Declarando
1 //Declaration of constructor with arguments:
2 public class Shirt
3 {
4
private String type;
5
private char size;
6
public Shirt(String theType, char theSize)
7
{
8
type = theType;
9
size = theSize;
10
}
11}
31
Declarando y usando
constructores
Uso correcto
1
2
3
4
5
//Use:
public class MakeOrder
{
Shirt myShirt = new Shirt("Polo", L);
}
//Use:
public class MakeOrder
{
Shirt myShirt = new Shirt();
}
32
Declarando y usando
constructores
1 // Declaration of constructor with no arguments:
2 public class OxfordShirt
3 {
4
private String type;
5
private char size;
6
public OxfordShirt()
7
{
8
type = "Oxford Shirt";
9
}
10}
1
2
3
4
5
6
// Use:
public class OrderShirt
{
OxfordShirt myShirt = new OxfordShirt();
}
33
Sobrecargando constructores
1 public class Shirts
2 {
3
private String type;
4
private char size;
5
6
// Shirt() is the constructor that takes no arguments
7
public Shirts()
8
{
9
type = "Oxford Shirt";
10
}
11
12
// Shirts( String type) is another constructor, which takes
13
// one argument
14
public Shirts( String shirtType)
15
{
16
type = shirtType;
17
}
18 }
34
Programa reloj
1 public class Clock
2 {
3
private int second, minute, hour;
4
5
public Clock(int s, int m, int h)
6
{
7
second = s;
8
minute = m;
9
hour = h;
10
}
11
12
public Clock()
13
{
14
second = 0;
15
minute = 0;
16
hour = 0;
17
}
18
35
Programa reloj
19
20
21
22
23
24
25
26
27
28
29
30
36
Programa reloj
31
32
33
34
35
36
37
38
39
40
41
37
Programa reloj
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Programa reloj
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
39
Programa reloj
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 }
98
40
Programa reloj
99 class TestClocks
100 {
101
public static void main(String args[])
102
{
103
Clock c1 = new Clock();
104
Clock c2 = new Clock(50, 30, 5);
105
106
System.out.println("Initial values:");
107
System.out.println(c1.getHour() + ":" + c1.getMinute()
108
+ ":" + c1.getSecond());
109
System.out.println(c2.getHour() + ":" + c2.getMinute()
110
+ ":" + c2.getSecond());
111
c1.tick();
112
c2.tick();
113
System.out.println("Values after ticking:");
114
System.out.println(c1.getHour() + ":" + c1.getMinute()
115
+ ":" + c1.getSecond());
116
System.out.println(c2.getHour() + ":" + c2.getMinute()
117
+ ":" + c2.getSecond());
118
41
Programa reloj
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135}
c1.tick();
c2.tick();
System.out.println("Values after ticking:");
System.out.println(c1.getHour() + ":" + c1.getMinute()
+ ":" + c1.getSecond());
System. out. println( c2. getHour() + ":" + c2. getMinute()
+ ":" + c2. getSecond());
c1. setHour( 7);
c2. setHour( 33); // Produces Error
System. out. println(" Values after
System. out. println( c1. getHour()
+ ":" + c1. getSecond());
System. out. println( c2. getHour()
+ ":" + c2. getSecond());
Message
changing hour:");
+ ":" + c1. getMinute()
+ ":" + c2. getMinute()
42
Ejercicio 2: Usando
constructores
Objetivo
Tareas
Resumen
43
Resumen
Definir encapsulamiento y sus ventajas
Definir los modificadores public y private y su
uso en programas
Describir las reglas para el alcance de variables
Escribir cdigo que encapsule miembros privados
Explicar el propsito de los constructores
Escribir cdigo que use el constructor por defecto
Escribir cdigo para definir un nuevo constructor y
usarlo
44