Está en la página 1de 6

Ejemplo en JAVA

*
Ing. Christian Roberto Ibáñez Nangüelú
10 de Febrero de 2021

Resumen
Se abordaron los temas encapsulación y constructores durante la
sesión de 10-12 hrs. En el documento encontrarán el diagrama UML
y las clases correspondientes.

*
cribn@ib.upchiapas.edu.mx
Ing. Biomédica Programación Orientada a objetos

Índice
1. Clase principal 1

2. Clase Perro 2

Índice de figuras
1. Diagrama UML . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Lista de Algoritmos
1. Clase principal . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2. Clase Perro . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

i Universidad Politécnica de Chiapas


Ing. Biomédica Programación Orientada a objetos

Figura 1: Diagrama UML

1. Clase principal
1 package ex1_9_feb_5b ;
2
3 /* *
4 *
5 * @author cribn
6 */
7 public class Ex1_9_FEB_5B {
8
9 /* *
10 * @param args the command line arguments
11 */
12 public static void main ( String [] args ) {
13 Perro p1 = new Perro ( " Solo - vino " ," Pug " ,12) ;
14 Perro p2 = new Perro () ;
15
16 System . out . println ( " --- Datos del can 1 ---" ) ;
17 System . out . println ( " Nombre : " + p1 . getNombre () ) ;
18 System . out . println ( " Raza : " + p1 . getRaza () ) ;
19 System . out . println ( " Altura : " + p1 . getAltura () ) ;
20 }
21
22 }
Algoritmos 1: Clase principal

1 Universidad Politécnica de Chiapas


Ing. Biomédica Programación Orientada a objetos

2. Clase Perro
1 package ex1_9_feb_5b ;
2
3 /* *
4 *
5 * @author cribn
6 */
7 public class Perro {
8 /*
9 Atributos
10 */
11 private String Nombre ;
12 private String Raza ;
13 private float Altura ;
14
15 /*
16 Constructor
17 */
18 Perro () {
19 this . Altura = 13;
20 this . Raza = " Pug " ;
21 this . Nombre = " august " ;
22 System . out . println ( " --- Datos del can default ---" ) ;
23 System . out . println ( " Nombre : " + this . Nombre ) ;
24 System . out . println ( " Raza : " + this . Raza ) ;
25 System . out . println ( " Altura : " + this . Altura ) ;
26 }
27
28 Perro ( float Altura ) {
29 System . out . println ( " Constructor " ) ;
30 this . Altura = Altura ;
31 }
32

33 Perro ( String Nombre ) {


34 this . Nombre = Nombre ;
35 }
36
37 Perro ( String Raza , float Altura ) {
38 this . Raza = Raza ;
39 this . Altura = Altura ;
40 }
41
42 Perro ( float Altura , String Nombre ) {
43 this . Altura = Altura ;

2 Universidad Politécnica de Chiapas


Ing. Biomédica Programación Orientada a objetos

44 this . Nombre = Nombre ;


45 }
46
47 Perro ( String Nombre , String Raza , float Altura ) {
48 this . Altura = Altura ;
49 this . Nombre = Nombre ;
50 this . Raza = Raza ;
51 }
52
53 /*
54 Metodo get
55 */
56 public String getNombre () {
57 return this . Nombre ;
58 }
59
60 public String getRaza () {
61 return this . Raza ;
62 }
63
64 public float getAltura () {
65 return this . Altura ;
66 }
67
68

69 /*
70 Metodo set
71 */
72 public void setNombre ( String Nombre ) {
73 this . Nombre = Nombre ;
74 }
75
76 public void setRaza ( String Raza ) {
77 this . Raza = Raza ;
78 }
79
80 public void setAltura ( float Altura ) {
81 this . Altura = Altura ;
82 }
83
84 /*
85 Metodos propios de la clase
86 */
87 public void Comer () {
88 System . out . println ( " Comer " ) ;

3 Universidad Politécnica de Chiapas


Ing. Biomédica Programación Orientada a objetos

89 }
90
91 public void Dormir () {
92 System . out . println ( " Dormir " ) ;
93 }
94
95 public void Ladrar () {
96 System . out . println ( " Ladrar " ) ;
97 }
98 }
Algoritmos 2: Clase Perro

4 Universidad Politécnica de Chiapas

También podría gustarte