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();
}
}
}
public class practice_everyday09_Run {
public static void main(String[] args) {
practice_everyday09 pe9 = new practice_everyday09();
pe9.Stream_outputByte();
pe9.Stream_inputByte();
}
}
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_everyday09 {
// 1.바이트 기반+보조
// 2.문자 기반+보조
// 1.바이트 기반+보조 : 입력
public void Stream_outputByte() {
// 목적 : 파일에 바이트기반으로 데이터를 빠르게 쓰고 싶다
FileOutputStream fos;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("D:\\test\\test.txt");
bos = new BufferedOutputStream(fos);
bos.write(65);
byte[] arr = {66,67,68,69};
bos.write(arr);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void Stream_inputByte() {
// 목적 :파일에 있는 데이터를 바이트 기반으로 빠르게 읽어오고 싶다
FileInputStream fis;
try {
fis = new FileInputStream("D:\\test\\test.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int val;
while((val=bis.read()) != -1) {
System.out.println(val);
}
} catch (IOException e) { // IOE
e.printStackTrace();
}
// 2.문자 기반+보조
}
}