Java 10일차 1(오브젝트)
2022. 11. 23. 12:13ㆍ코딩배움일지/JAVA
클래스 상속은 일대일 관계
인터페이스는 여러개의 다중구현 가능
// Scanner scanner = new Scanner(System.in);
// String s = "김준일"; /*language 폴더에 있으면 import 하지 않는다.*/
// Integer integer = 100; /* int 와 동일*/
// System.out.println(integer);
오브젝트
/*모든 클래스는 오브젝트 클래스를 상속 받는다*/
public class StringTest { /*모든 클래스는 오브젝트 클래스를 상속 받는다*/
public static void main(String[] args) {
String str = "A";
String str2 = "A";
String str3 = new String("A");
System.out.println(str == str2); /*true*/
System.out.println(str.equals(str2)); /*true*/
System.out.println(str); /*값이 나옴*/
System.out.println(str2); /*true*/
System.out.println(str3);
System.out.println(str==str3); /*이미 어딘가에 "A" 라는게 str 에 할당 받고 str2 에도 받았고 new 는 주소 할당을 받고 str3 에 갔기 때문*/
System.out.println(str.toString()); /*오브젝트 클래스 안에 있는 toString메소드*/
}
}
getClass
Class c = student.getClass();
System.out.println(c);
class j13_Object.Student
System.out.println(c.getName());
j13_Object.Student
System.out.println(c.getSimpleName()); /* 클래스 명만 가지고 옴*/
Student
Field[] fields = c.getDeclaredFields();
for (int i= 0; i<fields.length; i++){
System.out.println(fields[i].getName());
}
Method[]methods = c.getDeclaredMethods();
for (int i= 0; i <methods.length; i++){
System.out.println(methods[i].getName());
}
code
name
equals
toString
hashCode
hash Code () = 주소
System.out.println(student.hashCode()); /*실제주소*/
System.out.println(student2.hashCode());
940060004
234698513
Process finished with exit code 0
@Override
public int hashCode() {
return Objects.hash(code, name/*이 값들을 가지고 hash 를 만들지롱*/);
}
677071097
671335484
Process finished with exit code 0
public class StudentMain {
public static void print(Object obj){
System.out.println(obj);
}
public static void main(String[] args) {
Student student = new Student(20220001, "양진구");
Student student2 = new Student(20220001, "양진구");
System.out.println(student); /*원래는 toString을 실행 시켜야 하지만 안해도 됨.*/
System.out.println(student2);
System.out.println(student == student2);
System.out.println(student.equals(student2)); /*주소비교*/
System.out.println();
print(student); /*student의 주소값이 업캐스팅(Student클래스는 Object 클래스를 상속받음)해서 print()의 매개변수로 들어간것*/ /* 오브젝트 객체가 들어와야 하는데 학생이 오브젝트*/
System.out.println(student.hashCode()==student2.hashCode()); /*실제주소*/
System.out.println(student2.hashCode());
}
}
Student{code=20220001, name='양진구'}
Student{code=20220001, name='양진구'}
false
true
Student{code=20220001, name='양진구'}
true
677071097
Process finished with exit code 0
equals=
@Override /* 매개변수 칸에 업캐스팅으로 모든 클래스(오브젝트 상속받음) 가 들어올수 있다*/
public boolean equals(Object o) { /* 오버라이드 하면 매개변수를 바꿀수 있다, 자료형은 바꾸면 안된다.*/
if (this == o) return true;
if (o == null || getClass()/*자료형을 말한다.*/ != o.getClass()) return false; /* getClass() != o.getClass() == instanceof */ /*!(o instanceof Student) getClass() != o.getClass()*/
Student student = (Student) o; /*다운캐스팅*/
return code == student.code && Objects.equals(name, student.name); /* Objects = 유틸리티, String.equals 와 동일하게 사용*/
}
clone
to String
public class StudentMain {
public static void main(String[] args) {
Student student = new Student(20220001, "양진구");
System.out.println(student.toString()); /*원래는 toString을 실행 시켜야 하지만 안해도 됨.*/
System.out.println(student);
}
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode()); /*16진수로 바꿔라 */
}
j13_Object.Student@49e4cb85
j13_Object.Student@49e4cb85
Process finished with exit code 0
public class Student {
private int code;
private String name;
public Student(int code, String name) {
this.code = code;
this.name = name;
}
@Override /*alt + insert*/
public String toString() {
return "Student{" +
"code=" + code +
", name='" + name + '\'' +
'}';
}
}
public class StudentMain {
public static void main(String[] args) {
Student student = new Student(20220001, "양진구");
Student student2 = new Student(20220001, "김준일");
System.out.println(student); /*원래는 toString을 실행 시켜야 하지만 안해도 됨.*/
System.out.println(student2);
System.out.println(student == student2);
System.out.println(student.equals(student2)); /*주소비교*/
}
}
'코딩배움일지 > JAVA' 카테고리의 다른 글
Java 10일차 2(Entity) (0) | 2022.11.23 |
---|---|
Java 10일차 2(오브젝트) (0) | 2022.11.23 |
Java 9일차 4(인터페이스) (0) | 2022.11.22 |
Java 9일차 3 (0) | 2022.11.22 |
Java9일차 2(추상) (0) | 2022.11.22 |