package 학교_자바수업.week5;
public class PersonDemo {
public static void main(String[] args) {
Person p1 = new Person("홍 길도", 28);
Person p2 = new Person("홍","길도", 28);
Person p3 = new Person("홍","길동", 28);
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println("p1.equals(p2) = " + p1.equals(p2));
System.out.println("p1.equals(p3) = " + p1.equals(p3));
System.out.println("p2.equals(p3) = " + p2.equals(p3));
}
}
package 학교_자바수업.week5;
public class Person {
private String name1;
private String name2;
private int age;
public Person(String fName, String gName, int age) {
this.name1 = fName;
this.name2 = gName;
this.age = age;
}
public Person(String name, int age) {
int index = name.indexOf(" ");
name1 = name.substring(0, index);
name2 = name.substring(index + 1);
this.age = age;
}
public String toString() {
return "이름=" + name1 + " " + name2 + ", 나이=" + age;
}
public boolean equals(Person other) {
return name1.equals(other.name1)
&& name2.equals(other.name2)
&& age == other.age;
}
}
Created by 송바래
✉ gihun3645@naver.com
🚩경기도, 성남시