Java - accidentlywoo/sec GitHub Wiki

Java ์ •๋ฆฌ

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜์ž!

  • ๋ชฉ์  : ํด๋ž˜์Šค ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด์ž.

๊ฐ์ฒด์ง€ํ–ฅ์–ธ์–ด์˜ ์กฐ๊ฑด

  1. ์ƒ์† ๋‹จ์ผ ์ƒ์†

    extends : class ํ•˜์œ„ํด๋ž˜์Šค extends ์ƒ์œ„ํด๋ž˜์Šค{}

    ์ƒ์†์€ "Is A"||"Kind Of" ๊ด€๊ณ„์ผ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋…ผ๋ฆฌ์ ์ธ ์„ค๊ณ„๋ฅผ ํ•˜์ž!

    "Has A"๊ด€๊ณ„๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  2. ์บก์Šํ™” - ์ ‘๊ทผ์ œ์–ด

  3. ๋‹คํ˜•์„ฑ

์ ‘๊ทผ์ œ์–ด

  • public : ๋ˆ„๊ตฌ๋‚˜
  • protected : ๋™์ผ ํŒจํ‚ค์ง€์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅ, ๋‹ค๋ฅธ ํŒจํ‚ค์ง€์˜ ํ•˜์œ„ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅ
  • deault(package) : ๋™์ผ ํŒจํ‚ค์ง€์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅ
  • private : ์ž๊ธฐ์ž๊ธด๋งŒ ์Œ‰ ๊ฐ€๋Šฅ

์บก์Šํ™”

์€๋‹‰๋œ ์ •๋ณด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์„ค์ •(set) / ์–ป๊ธฐ(get)

์ปดํฌ๋„ŒํŠธ[JavaBean]

์ž๋ฐ”๋นˆ์ด ๋˜๊ธฐ์œ„ํ•œ ์กฐ๊ฑด 4๊ฐ€์ง€.

  1. public class

  2. public ๋งค๊ฐœ๋ณ€์ˆ˜์—†๋Š” ์ƒˆ์„ฑ์ž

  3. ํ”„๋กœํผํ‹ฐ์šฉ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜๋Š” public ์ด๋ฉด ์•ˆ๋จ

  4. ํ”„๋กœํผํ‹ฐ์šฉ public setter ๋ฉ”์„œ๋“œ / public getter ๋ฉ”์„œ๋“œ

public class Star{
 private String color;
 private String size;

 public void setColor(String color) {
  // ์„ค์ • ์ œ์–ด๋ฌธ ์ž‘์„ฑ 
  this.color = color;
 }
 public String getColor(){return color;}
}

์ •์  ๋ฉค๋ฒ„์™€ static

ํด๋ž˜์Šค ๋กœ๋”ฉ์‹œ ์ดˆ๊ธฐํ™”๋œ๋‹ค. this ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ๋ถˆ๊ฐ€. ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผํ•˜๋Š”๊ฒŒ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์— ์ข‹๋‹ค.

class Single{
	static int sv;
	private Single() {}
	static void plus(int i) {
		sv =  sv+i;
	}
	static Single getInstance() {
		return new Single();
	}
}

class Main{
 public static void main(String[] args) {
  System.out.println(Single.sv);

  Single s1 = Single.getInstance();
  System.out.println(s1.sv);
 }
}

static ํ•„๋“œ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•ด์„œ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋กœ ์ ‘๊ทผํ•˜๋ฉด ์Šคํƒ์—์„œ ํž™์˜์—ญ์„ ๋’ค์ง€๊ณ  ์—†์œผ๋ฉด ClassArea๋ฅผ ๋’ค์ง€๊ธฐ ๋•Œ๋ฌธ์— ํšจ์œจ์ด ๋–จ์–ด์ง„๋‹ค.

System.out.println(s1.sv); //ํšจ์œจ ์Œ‰ ์†ํ•ด, IDE์—์„œ warning์„ ํ‘œ์‹œํ•œ๋‹ค.
System.out.println(Single.sv); // ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜์ž.
lass Single{
	int iv;
	static int sv;
	private Single() {}
	static void test() {
//		System.out.println(this);
//		System.out.println(iv);
//		System.out.println(this.iv);
//		System.out.println(plus);
//		System.out.println(this.plus);
	}
	static void plus(int i) {
		sv =  sv+i;
	}
	static Single getInstance() {
		return new Single();
	}
}

static method๋Š” ์ธ์Šคํ„ด์Šคํ™”ํ•œ ๊ฐ์ฒด์˜ method์˜์—ญ์—์„œ ๋ฉ”์„œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜๊ฐ€ ์—†๋‹ค. class Area์—์„œ ๋’ค์ ๊ฑฐ๋ฆผ

static method๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์žฅ์ ์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ

  • ์œ ํ‹ธ ํด๋ž˜์Šค
  • ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ
  • ๋นŒ๋” ํŒจํ„ด
  • ์‹ฑ๊ธ€ํ†ค

์ƒ์†

ํž™ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ทธ๋ฆด๋•Œ, ์ž์‹๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ์•ˆ์— ๋ถ€๋ชจ๊ฐ์ฒด์˜ ์˜์—ญ์ด ๋จผ์ € ์ƒ๊ธด๋‹ค. (์ฑ…์ด ์ž˜๋ชป๋จ. ๋”ฐ๋กœ ์ƒ๊ฒจ์„œ ์ƒ์†๋ฐ›๊ฑฐ๋‚˜ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค.)

Is A ๊ด€๊ณ„

class Person{
  String name;
  String addr;
}

class Customer extends Person{
  String id;
  String pwd;
}

Customer์€ Person์ด๋‹ค.

class Person{
  String name;
  String addr;
}

class Account extends Person{
  String id;
  String pwd;
}

Account๋Š” Person์ด ์•„๋‹ˆ๋‹ค.

Has A ๊ด€๊ณ„

class Person{
  String name;
  String addr;
}

class Account{
  String id;
  String pwd;
  Person PersonInfo;
}

Account๋Š” Person์ •๋ณด๋ฅผ ๊ฐ–๋Š”๋‹ค.

์ƒ์†์‹œ ๋ฉค๋ฒ„ํ•„๋“œ๋ช… ์ค‘๋ณต

class GrandParent{
int i;
}
class Parent extends GrandParent{
  String i;
  void pm(){
    super.i = 10;
  }
}
class Child extends Parent{
  boolean i;
  void m(){
    this.i = true;
    super.i = "hello";
    pm();
  }
}

new Child().i = false;

super ํ‚ค์›Œ๋“œ๋Š” ํ˜„์žฌ ๊ฐ์ฒด์—์„œ ๋ถ€๋ชจ๊ฐ์ฒด์— ์ ‘๊ทผ.

super() ๋ถ€๋ชจ ์ƒ์„ฑ์ž ํ˜ธ์ถœํ•˜๋ผ!

๋‹คํ˜•์„ฑ์˜ ๊ฐœ๋…์„ ์ฒดํฌํ•˜์ž.

  • auto up casting
  • ๊ฐ•์ œ down casting

์˜ค๋ฒ„๋ผ์ด๋“œ ๊ทœ์น™

  1. ์ƒ์†๊ด€๊ณ„ ๋ฉ”์„œ๋“œ
  2. ๋ฉ”์„œ๋“œ๋ช… ๋™์ผ, ๋งค๊ฐœ๋ณ€์ˆ˜(์ž๋ฃŒํ˜•, ๊ฐœ์ˆ˜, ์ˆœ์„œ) ๋™์ผ, ๋ฐ˜ํ™˜ํ˜•๋„ ๋™์ผ
  3. ์ƒ์œ„ ๋ฉ”์„œ๋“œ ์ ‘๊ทผ๋ฒ”์œ„ <= ํ•˜์œ„๋ฉ”์„œ๋“œ ์ ‘๊ทผ๋ฒ”์œ„
class P{  // ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ class P extends Object{ ๋กœ ๋ณ€ํ™˜
 void m(){}
 void m1(){}
}
class C extends P{
 public void m(){}
 private void m1(){} // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜
}
  1. ์˜ˆ์™ธ์„ ์–ธ

Object ์ตœ์ƒ์œ„ ๊ฐ์ฒด

Object

  • +hashCode():int
  • +toString():String
  • +equals(Object):boolean

String

  • +hashCode():int
  • +toString():String
  • +equals(Object):boolean

StringBuffer

  • +toString():String

Final Class ์™€ Abstract Class

ํด๋ž˜์Šค์˜ ์—ญํ• 

  • ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ํ‹€(final class)
  • ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์˜ ๊ณตํ†ต์ ์„ ๋ชจ์•„๋‘ (abstract class)

์ผ๋ฐ˜์ ์ธ ํด๋ž˜์Šค๋“ค์€ ์ด๋‘˜์˜ ํŠน์ง•์„ ๋ชจ๋‘ ๊ฐ–๋Š”๋‹ค.

Final Method ์™€ Abstract Method

  • final ๋ฉ”์„œ๋“œ : ์žฌ์ •์˜(overriding ๋ชปํ•จ) ๊ธˆ์ง€
  • abstract ๋ฉ”์„œ๋“œ : ๋ฐ˜๋“œ์‹œ ์žฌ์ •์˜ ํ•„์š”, ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ ์ถ”์ƒ ํด๋ž˜์Šค์—ฌ์•ผ ํ•œ๋‹ค.

Abstract Class

new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šคํ™” ํ•  ์ˆ˜ ์—†๋‹ค.

์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋Š”๋ฐ protected ์ ‘๊ทผ ์ œ์–ด์ž๊ฐ€ ๋ถ™์–ด์žˆ๋‹ค.

-> ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ํ™•์žฅํ–ˆ์„๋•Œ, ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ๋“ค์–ด์ ธ์žˆ๋‹ค.

ex) Abstract Class ์ธ Calendar ํด๋ž˜์Šค๋Š” Calendar(), Calendar(TimeZone zone, Locale aLocale) ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋Š”๋‹ค.

Calendar ํด๋ž˜์Šค์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ธ GregorianCalendar๋Š” Calendar.getInstance()๋กœ ์ธ์Šคํ„ด์Šคํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

GregorianCalendarํด๋ž˜์Šค๋Š” ํ™•์žฅํ•œ Calendar ์ถ”์ƒ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด(super()) ์ƒ์„ฑ์ž ํ™œ์šฉํ•œ๋‹ค!

Interface

interface A{
  int A_CNT = 1;
  void a();
}

interface B{
  void b();
}

interface ABC extends A, B{
  void abc(); // public abstract void abc();
}

class ABCImp implements ABC{
 //void abc(){} ์ ‘๊ทผ์ œ์–ด์ž ์ปดํŒŒ์ผ ์—๋Ÿฌ
 public void abc(){}
 public void a(){}
 public void b(){} 
}

abstract class ABCImp2 implements ABC{} // ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋‚˜์ง€ ์•Š๋Š”๋‹ค.

class ABCImp3 extends Parents implements ABC{
 public void abc(){}
 public void a(){}
 public void b(){} 
}

Exception

  • ํšจ์œจ์„ฑ์„ ์ƒ๊ฐํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

ex) for๋ฌธ์—์„œ Exception ์ฒดํฌ๋ฅผ ํ•ด์•ผํ•  ๋•Œ๋Š”

// Bad Code
for(int i = 0 ; i < 10000; i++){
  try{
    exceptionCheckPlease(i);
  }catch{}
}
// good code
try{
  for(int i = 0; i < 10000; i ++){
    exceptionCheckPlease(i);
  }
}catch{}

๋งค๋ฒˆ ์ฒดํฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค. for๋ฌธ์ด ๊นจ์งˆ๋•Œ catchํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. (์„ค๊ณ„์— ๋”ฐ๋ผ ๋‹ค๋ฆ„)

Exception ์—์„œ setMessage() ๋Š”์—†๋‹ค. ๋ฉ”์„ธ์ง€๋ฅผ ์„ค์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

๊ฐ•์ œ๋กœ Exception์„ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , ์ƒ์„ฑ์ž์— ๋ฉ”์‹œ์ง€๋ฅผ ๋‹ด๊ณ , ํด๋ž˜์Šค ์ƒ์„ฑํ•œ๋‹ค.

throw new IOException("์˜ˆ์™ธ ๋ฐœ์ƒ");

throws๋ฅผ JVM์œผ๋กœ?

throws๋ฅผ ๋’ค์ง€๊ฒŒ ์จ์„œ main๋ฉ”์†Œ๋“œ๊นŒ์ง€ ๋„˜๊ธฐ๋ฉด JVM๊นŒ์ง€ ๋„˜๊ธธ ์ˆ˜ ์žˆ๋‹ค.

๋ฉ€ํ‹ฐ Exception

๋””ํ…Œ์ผํ•œ Exception catch๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ํ•˜์œ„ํด๋ž˜์Šค๋ถ€ํ„ฐ catch ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผ flow์— ๋งž๋‹ค.

์‚ฌ์šฉ์ž ์ •์˜ Exception

์„œ๋น„์Šค๋‹จ์˜ Exception๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•ด์งˆ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž ์ •์˜ Exception์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฃผ๋กœ ๊ธฐ๋Šฅ๋ณ„, ๋ฉ”์†Œ๋“œ๋ณ„๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.