-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClase Java 12.java
More file actions
80 lines (49 loc) · 2.06 KB
/
Clase Java 12.java
File metadata and controls
80 lines (49 loc) · 2.06 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//--------------------------------------------------------------|
//CONTENIDO: Sentencia equals
//--------------------------------------------------------------|
package clase.java.pkg12;
import java.util.Objects;
public class ClaseJava12 {
private String nombre;
private int dni;
private int edad;
public ClaseJava12(String nombre, int edad) {
this.nombre = nombre;
this.edad = edad;
}
// Sentencias para el equals
//----------------------------------------------------------------------|
@Override
public int hashCode() {
int hash = 5;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ClaseJava12 other = (ClaseJava12) obj;
if (this.edad != other.edad) {
return false;
}
return Objects.equals(this.nombre, other.nombre);
}
//----------------------------------------------------------------------------------------------------------------|
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//----------------------------------------------------------------------------------------------------------------|
public static void main(String[] args) {
ClaseJava12 cj = new ClaseJava12("Pepe",12);
ClaseJava12 cj2 = new ClaseJava12("Pepe",12);
System.out.println(cj.equals(cj2)); //Son iguales, por lo que devuelve true
ClaseJava12 cj3 = new ClaseJava12("Jaime",22);
ClaseJava12 cj4 = new ClaseJava12("Carlos",20);
System.out.println(cj3.equals(cj4)); //Son distintos, por lo que devuelve false
}
}