@Override
public boolean equals(Object obj) {
// 객체 비교
if (this == obj) { // this는 주소값 비교할려고 넣은 것
return true;
}
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) { // 내 클래스 정보와 상대방의 클래스 정보가 같은지 비교
return false;
}
pModelVo02 other = (pModelVo02)obj; // 다운캐스팅 : obj->Snack
if(name == null) { // other는 레퍼런스 변수
if(other.name != null) {
return false;
}
}else if(!name.equals(other.name)) { // 이름에 대한 비교
return false;
}
if(weight != other.weight) { // 목록에 대한 비교
return false;
}
return true;
}
@Override
public int hashCode()
@Override
public int hashCode() {
final int PRIME = 31; // 컴퓨터가 이해하기 좋은 숫자가 31이라함
int result = 1;
result = PRIME * result + (name == null ? 0 : name.hashCode()); // 내 해쉬코드가 아니라 스트링의 해쉬코드를 가져오는 것
result = PRIME * result + weight; // 형이 안맞아서 에러나니 캐스팅 또는 소수점 없게끔 계산
return result;
}
@Override
public int compareTo(Object o)
@Override
public int compareTo(Object o) {
// Dog 이름 오름차순
// Object o를 다운 캐스팅 해줘야함
pModelVo02 otherP = (pModelVo02)o;
String other = otherP.name;
int result = name.compareTo(other); // compareTO 자체가 인트값 반환
return 0;
}
package controller;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import model.vo.pModelVo02;
public class pController02 {
public void firstR() {
// HashSet
HashSet<pModelVo02> hset =new HashSet<>();
hset.add(new pModelVo02("힘내",1));
hset.add(new pModelVo02("포기 ㄴ",2));
System.out.println(hset);
pModelVo02 pmv = new pModelVo02("a",0);
hset.add(pmv);
hset.add(new pModelVo02("추가",3));
System.out.println(hset);
HashSet<String> strSet = new HashSet<String>();
strSet.add("안녕");
strSet.add(new String("안녕"));
System.out.println(hset);
System.out.println(strSet);
strSet.remove(new String("안녕"));
System.out.println(strSet);
}
public void second() {
// LinkedHashSet
// 순서 유지
LinkedHashSet<pModelVo02> lhSet = new LinkedHashSet();
lhSet.add(new pModelVo02("a",1));
lhSet.add(new pModelVo02("b",2));
lhSet.add(new pModelVo02("c",3));
System.out.println(lhSet);
// TreeSet
// 정렬 가능
// 정렬 기능 + 기준 필요
// 기준이 되는 오버라이딩은 String class 제네릭으로 대체
TreeSet<String> tSet = new TreeSet<>();
// tSet.add(new String("A",1));
// tSet.add(new String("B",2));
System.out.println(tSet);
tSet.add("A");
tSet.add("C");
tSet.add("B"); // 추가 순서는 ACB
System.out.println(tSet); // 결과는 ABC (정렬 적용 확인)
}
}
package model.vo;
public class pModelVo02 {
private String name;
private double weight;
public pModelVo02() {}
public pModelVo02(String name, double weight) {
this.name = name;
this.weight = weight;
}
// getter & setter
public void setName(String name) {
this.name = name;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public double getWeight() {
return weight;
}
//toString
@Override
public String toString() {
return name+"("+weight+"kg)";
}
}
package run;
import controller.pController02;
public class pRun02 {
public static void main(String[] args) {
pController02 p = new pController02();
// p.firstR();
p.second();
}
}
import java.util.Scanner;
public class practice_everyday13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 삼항연산자 (2중) + do while문
int num1;
int num2;
do {
System.out.print("수1 입력 : ");
num = sc.nextInt();
System.out.print("수2 입력 : ");
num2 = sc.nextInt();
System.out.println(num1 == num2 ? "같아 멈춰":(num1>num2 ? "1이 커":"2가 커"));
}while(num1 !=num2);
// for문 초기화 생략
int num3 = 5;
int i = 0;
for(; i < num3; i+=2) { // 초기화 생략 : int i +=0;
System.out.println(i);
}
}
}
자식 클래스 부모 클래스의 이름을 눌러보면 부모클래스와 연결되어있는 코드들의 색깔이 진해진다
이 코드들은 중복이기에 삭제하여도 부모와 자식 클래스의 정보가 연동된다
삭제 전
삭제 후
0번 parent
1번 child1
2번 child2
상속관계가 0 -> 1 -> 2
1번이 0번을 상속하고 2번이 1번을 상속한 것
1번 child1에서 parent에 받았던 a,b부분들을 주석 처리해서 2번 child2에서 보면 정보가 그대로인 걸 알 수 있다
public class practice_everyday04 {
public static void main(String[] args) {
// run
practice_everyday04_0 pe1 = new practice_everyday04_0();
pe1.println();
practice_everyday04_1 pe2 = new practice_everyday04_1();
pe2.println();
practice_everyday04_2 pe3 = new practice_everyday04_2();
pe3.println();
}
}
public class practice_everyday04_0 {
// Parent
private int a;
private int b;
public practice_everyday04_0() {}
public practice_everyday04_0(int a, int b) {
this.a = a;
this.b = b;
}
//getter & setter
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public void println() {
System.out.println("parent");
}
}
public class practice_everyday04_1 extends practice_everyday04_0 {
// child1
// private int a;
// private int b;
private int c;
public practice_everyday04_1() {}
public practice_everyday04_1(int a, int b, int c) {
// this.a = a;
// this.b = b;
this.c = c;
}
// getter & setter
// setter : 데이터를 변수에 저장하는 메소드
// getter : 저장된 데이터를 불러오는 메소드
// public int getA() {
// return a;
// }
// public void setA(int a) {
// this.a = a;
// }
// public int getB() {
// return b;
// }
// public void setB(int b) {
// this.b = b;
// }
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
@Override
public void println() {
System.out.println("child1");
}
}
// 상속 & overriding
public class practice_everyday04_2 extends practice_everyday04_1{
// child2
private int a;
private int b;
private int c;
private int d;
public practice_everyday04_2() {}
public practice_everyday04_2(int a, int b, int c, int d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
// getter & setter
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public int getD() {
return d;
}
public void setD(int d) {
this.d = d;
}
@Override
public void println() {
System.out.println("child2");
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class practice_everyday10 {
// 1.바이트 기반+보조 : 입력
public void Stream_outputByte() {
// 목적 : 파일에 바이트기반으로 데이터를 빠르게 쓰고 싶다
try (BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("D:\\test\\test.txt"))){
bos.write(65);
byte[] arr = {66,67,68,69};
bos.write(arr);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void Stream_inputByte() {
// 목적 :파일에 있는 데이터를 바이트 기반으로 빠르게 읽어오고 싶다
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\test.txt"))) {
int val;
while((val=bis.read()) != -1) {
System.out.println(val);
}
} catch (IOException e) { //IOException이 FileInputStream의 부모이기에 따로 예외처리하지않아도 같이 처리
e.printStackTrace();
}
}
}