1 public class Main {
2 // Ejemplo de encapsulamiento con getters y setters
3
4 public static void main(String[] args) {
5 // Crear una instancia de la clase Perro con raza y edad
6 Perro miPerro = new Perro("Labrador", 3);
7
8 // Llamar al getter para obtener la raza inicial del perro
9 System.out.println("Raza inicial: " + miPerro.getRaza()); //
10 Imprime "Labrador"
11
12 // Llamar al getter para obtener la edad inicial del perro
13 System.out.println("Edad inicial: " + miPerro.getEdad()); //
14 Imprime 3
15
16 // Llamar al setter para cambiar la raza y la edad del perro
17 miPerro.setRaza("Pastor Alemán");
18 miPerro.setEdad(4);
19
20 // Llamar al getter nuevamente para obtener la nueva raza y
21 edad del perro
22 System.out.println("Nueva raza: " + miPerro.getRaza()); //
23 Imprime "Pastor Alemán"
24 System.out.println("Nueva edad: " + miPerro.getEdad()); //
25 Imprime 4
26 }
27 }
28
29 public class Perro {
30 // Clase que representa a un perro
31
32 private String raza; // Atributo privado para la raza del perro
33 private int edad; // Atributo privado para la edad del perro
34
35 // Constructor que acepta la raza y la edad del perro como
36 parámetros
37 public Perro(String raza, int edad) {
38 this.raza = raza;
39 this.edad = edad;
40 }
41
42 // Getter para obtener la raza del perro
43 public String getRaza() {
44 return raza;
45 }
46
47 // Setter para establecer la raza del perro
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller johanossaserna. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.56. You're not tied to anything after your purchase.