JAVA의 정석 (ch 7) (2)

Du-Hyeon, KimΒ·2023λ…„ 7μ›” 28일
0

JAVA

λͺ©λ‘ 보기
9/11
post-thumbnail

πŸ’» 7. OOPs - 2

ch7

1. static import

static import문을 μ‚¬μš©ν•˜λ©΄ static 멀버λ₯Ό ν˜ΈμΆœν•  λ–„ 클래슀 이름을 μƒλž΅ν•  수 μžˆλ‹€.

ex)

import static java.lang.Integar.*;
import static java.lang.Math.random;
//Math.random()만. κ΄„ν˜Έ μ•ˆλΆ™μž„
import static java.lang.System.out;
//System.out을 out만으둜 μ°Έμ‘° κ°€λŠ₯

System.out.println(Math.random());
//을 μ•„λž˜μ™€ 같이 ν‘œμ‹œ κ°€λŠ₯
out.println(random());

2.μ œμ–΄μž(modifier)

μ œμ–΄μžλŠ” 클래슀, λ³€μˆ˜ λ˜λŠ” λ©”μ„œλ“œμ˜ 선언뢀에 ν•¨κ»˜ μ‚¬μš©λ˜μ–΄ 뢀가적인 의미λ₯Ό λΆ€μ—¬ν•œλ‹€.
μ ‘κ·Ό μ œμ–΄μž, κ·Έ μ™Έμ˜ μ œμ–΄μžλ‘œ λ‚˜λ‰œλ‹€.

μ ‘κ·Ό μ œμ–΄μž: public, protected, default, private
κ·Έ μ™Έ: static, final, abstract, native,transient, synchronized, volatile, strictfp

  • 클래슀, λ©€λ²„λ³€μˆ˜, λ©”μ„œλ“œμ— 주둜 μ‚¬μš©
  • ν•˜λ‚˜μ˜ λŒ€μƒμ— μ—¬λŸ¬ μ œμ–΄μž μ‘°ν•©ν•˜μ—¬ μ‚¬μš©κ°€λŠ₯ν•˜λ‹€.
  • 단, μ ‘κ·Ό μ œμ–΄μžλŠ” 4κ°œμ€‘ ν•˜λ‚˜λ§Œ μ‚¬μš©κ°€λŠ₯ν•˜λ‹€

3.static μ œμ–΄μž

'클래슀의' λ˜λŠ” '곡톡적인'의 의미λ₯Ό μ§€λ‹˜

μ‚¬μš©μ— λ”°λ₯Έ 의미

  1. λ§΄λ²„λ³€μˆ˜μ—μ„œ staticμ‚¬μš©
  • λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ—μ„œ κ³΅ν†΅μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” 클래슀 λ³€μˆ˜κ°€ λœλ‹€.
  • μΈμŠ€ν„΄μŠ€ 생성 없이 μ‚¬μš©κ°€λŠ₯
  • λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ— λŒ€ν•΄ λ™μΌν•œ κ°’
  • ν΄λž˜μŠ€κ°€ λ©”λͺ¨λ¦¬μ— λ‘œλ“œλ  λ•Œ 생성됨 / new method 이전에 이미 있음
  1. λ©”μ„œλ“œμ—μ„œ μ‚¬μš©
  • μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€ μ•Šκ³ λ„ 호좜 κ°€λŠ₯ν•œ static λ©”μ„œλ“œκ°€ 됨
  • static λ©”μ„œλ“œ λ‚΄μ—μ„œλŠ” instance variable을 μ§μ ‘μ μœΌλ‘œ μ‚¬μš©ν•  수 μ—†μŒ
  1. μ΄ˆκΈ°ν™” λΈ”λŸ­μ—μ„œ μ‚¬μš©
  • μΈμŠ€ν„΄μŠ€ 생성 전에 클래슀 μ΄ˆκΈ°ν™” λΈ”λŸ­μ€ μ‹€ν–‰λœλ‹€.

4. final μ œμ–΄μž

'λ§ˆμ§€λ§‰μ˜' λ˜λŠ” '변경될 수 μ—†λŠ”'의 의미λ₯Ό 가짐

μ‚¬μš©μ— λ”°λ₯Έ 의미

  1. ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©
  • 변경될 수 μ—†λŠ” 클래슀, ν™•μž₯λΆˆκ°€ν•œ ν΄λž˜μŠ€κ°€ λœλ‹€.
  • λ‹€λ₯Έ 클래슀의 쑰상이 될 수 μ—†μŒ
  1. λ©”μ„œλ“œμ—μ„œ μ‚¬μš©
  • final둜 μ •μ˜λœ λ©”μ„œλ“œλŠ” overriding될 수 μ—†λ‹€.
  1. λ©€λ²„λ³€μˆ˜ / μ§€μ—­λ³€μˆ˜μ—μ„œ μ‚¬μš©
  • 값을 λ³€κ²½ν•  수 μ—†λŠ” μƒμˆ˜κ°€ 됨!!
final class FinalTest{
	final int MAX_SIZE = 10;

	final void getMaxSize(){
		final int LV = MAX_SIZE;
		return MAX_SIZE;
	}
}

5. abstractμ œμ–΄μž

μ‚¬μš©μ— λ”°λ₯Έ 의미

  1. ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©
  • 클래슀 내에 좔상 λ©”μ„œλ“œκ°€ μ •μ˜λ˜μ–΄ μžˆμŒμ„ 의미
  1. λ©”μ„œλ“œμ—μ„œ μ‚¬μš©
  • μ„ μ–ΈλΆ€λ§Œ μž‘μ„±ν•˜κ³  κ΅¬ν˜„λΆ€λŠ” μž‘μ„±ν•˜μ§€ μ•Šμ€ 좔상 λ©”μ„œλ“œμž„

μΆ”μƒλ©”μ„œλ“œμ˜ μžμ„Έν•œ λ‚΄μš©μ€ p.257('좔상 클래슀')μ—μ„œ λ‹€λ£Έ

6. μ ‘κ·Ό μ œμ–΄μž(access modifier)

멀버 λ˜λŠ” 클래슀λ₯Ό μ™ΈλΆ€μ—μ„œ μ ‘κ·Όν•˜μ§€ λͺ»ν•˜λ„둝 μ œν•œν•˜λŠ” 역할을 ν•œλ‹€.

μ ‘κ·Ό μ œμ–΄μžκ°€ μ‚¬μš©λ  수 μžˆλŠ” κ³³ - 클래슀, λ©€λ²„λ³€μˆ˜, λ©”μ„œλ“œ, μƒμ„±μž

private - 같은 클래슀 λ‚΄μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯
default - 같은 νŒ¨ν‚€μ§€ λ‚΄μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯
protected - 같은 νŒ¨ν‚€μ§€ λ‚΄μ—μ„œ, 그리고 λ‹€λ₯Έ νŒ¨ν‚€μ§€μ˜ μžμ† ν΄λ ˆμŠ€μ—μ„œ μ ‘κ·Ό κ°€λŠ₯
public - μ ‘κ·Ό μ œν•œ μ—†μŒ

public > protected > (default) > private


7. μΊ‘μŠν™”μ™€ μ ‘κ·Ό μ œμ–΄μž

클래슀 내뢀에 μ„ μ–Έλœ 데이터λ₯Ό λ³΄ν˜Έν•˜κΈ° μœ„ν•΄μ„œμ΄λ‹€.
데이터가 μœ νš¨ν•œ 값을 μœ μ§€ν•˜λ„λ‘, λΉ„λ°€λ²ˆν˜Έ 같은 데이터λ₯Ό μ™ΈλΆ€μ—μ„œ ν•¨λΆ€λ‘œ λ³€κ²½ν•˜μ§€ λͺ»ν•˜λ„둝 ν•˜κΈ° μœ„ν•΄μ„œ μ œν•œ ν•„μš”

ex) μ‹œκ°„ 클래슀

public class Time{
	public int hour;
	public int minute;
	public int second;
}
Time t = new Time();
t.hour = 25;

μœ„μ™€ 같이 μ™ΈλΆ€μ—μ„œ 클래슀 λ‚΄ hour값을 μœ νš¨ν•˜μ§€ μ•Šμ€ κ°’(25)둜 변경해도 막을 방법이 μ—†μŒ.
λ”°λΌμ„œ private int hour을 μ„€μ •ν•˜κ³ ,
public λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•¨μœΌλ‘œμ„œ λ©”μ„œλ“œλ‚΄ hour의 ν—ˆμš©λ²”μœ„λ₯Ό μ„€μ •ν•  수 μžˆλ‹€.

getter(κ²Ÿν„°) : "getλ©€λ²„λ³€μˆ˜μ΄λ¦„"
-> λ©€λ²„λ³€μˆ˜ 값을 μ½λŠ” λ©”μ„œλ“œ 이름

setter(μ…‹ν„°) : "setλ©€λ²„λ³€μˆ˜μ΄λ¦„"
-> λ©€λ²„λ³€μˆ˜ 값을 λ³€κ²½ν•˜λŠ” λ©”μ„œλ“œ

별 일 μ—†λŠ”ν•œ λ”°λ₯΄λ„둝 ν•˜μž(암묡적 약속)

public class Time {
	private int hour;
	private int minute;
	private int second;
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		if (hour < 0 || hour > 23)
			return;
		this.hour = hour;
	}
	public int getMinute() {
		return minute;
	}
	public void setMinute(int minute) {
		if (minute < 0 || minute > 59)
			return;
		this.minute = minute;
	}

	public int getSecond() {
		return second;
	}
	public void setSecond(int second) {
		if (second < 0 || second > 59)
			return;
		this.second = second;
	}
}

8. λ‹€ν˜•μ„±(polymorphism)

μžλ°”μ—μ„œλŠ” ν•œ νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ μ—¬λŸ¬ νƒ€μž…μ˜ 객체λ₯Ό μ°Έμ‘°ν•  수 μžˆλ„λ‘ ν•¨μœΌλ‘œμ¨ λ‹€ν˜•μ„±μ„ ν”„λ‘œκ·Έλž¨μ μœΌλ‘œ κ΅¬ν˜„ν•˜μ˜€λ‹€.

ex)

Tv(쑰상) CaptionTv(μžμ†)

Tv t = new CaptionTv();

쑰상 클래슀 νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ‘œ μžμ† 클랜슀의 μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•˜λ„λ‘ ν•˜λŠ” 것도 κ°€λŠ₯

  • 쑰상 클래슀 μ°Έμ‘°λ³€μˆ˜λ‘œ μ •μ˜ν•œ μžμ† κ°μ²΄λŠ” 쑰상 클래슀 λ‚΄μ˜ λ©€λ²„λ§Œ μ‚¬μš©κ°€λŠ₯ν•˜λ‹€.

9. μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•λ³€ν™˜

μƒμ†λœ 클래슀 λ‘κ°œ(쑰상, μžμ†)에 λŒ€ν•΄μ„œ 클래슀λ₯Ό μ„ μ–Έν•œ μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•μ„ (쑰상 or μžμ†)ν΄λž˜μŠ€ν˜•μœΌλ‘œ λ³€ν™˜ κ°€λŠ₯ν•˜λ‹€.

class Car{}
class FireEngine 	extends Car {}
class Ambulance 	extends Car {}

FireEngine f = new FireEngine();
Car c = (Car)f;
//μžμ† 클래슀의 쑰상 클래슀둜의 ν˜•λ³€ν™˜
FireEngine f2 = (FireEngine)c;
Ambulance a = (Ambulance)f;
//상속 관계가 μ•„λ‹Œ κ΄€κ³„λ‘œλŠ” ν˜•λ³€ν™˜ λΆˆκ°€!!!

μ£Όμ˜μ‚¬ν•­

  • μžμ† -> μ‘°μƒμœΌλ‘œμ˜ ν˜•λ³€ν™˜μ€ ν˜• μƒλž΅κ°€λŠ₯
  • 쑰상 -> μžμ†μœΌλ‘œμ˜ ν˜•λ³€ν™˜μ€ ν˜• μƒλž΅λΆˆκ°€
class Ex7_7 {
	public static void main(String args[]) {
		Car car = null;
		FireEngine fe = new FireEngine();
		FireEngine fe2 = null;

		fe.water();
		car = fe; // car = (Car)fe;
		// car.water();
		// car은 Car ν΄λž˜μŠ€ν˜• μ°Έμ‘°λ³€μˆ˜μ΄κΈ° λ•Œλ¬Έμ— water λ§€μ„œλ“œ μ‘΄μž¬μ•ˆν•¨
		fe2 = (FireEngine) car; //ν˜• μƒλž΅λΆˆκ°€!!!
		fe2.water();
	}
}

class Car {
	String color;
	int door;

	void drive() {
		System.out.println("drive, Brrrr~");
	}

	void stop() {
		System.out.println("stop!!!");
	}
}

class FireEngine extends Car {
	void water() {
		System.out.println("water!!!");
	}
}

public class Time {
	private int hour;
	private int minute;
	private int second;
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		if (hour < 0 || hour > 23)
			return;
		this.hour = hour;
	}
	public int getMinute() {
		return minute;
	}
	public void setMinute(int minute) {
		if (minute < 0 || minute > 59)
			return;
		this.minute = minute;
	}

	public int getSecond() {
		return second;
	}
	public void setSecond(int second) {
		if (second < 0 || second > 59)
			return;
		this.second = second;
	}
}

μ‰½κ²Œ μ„€λͺ…ν•˜λ©΄,
상속 될 λ•Œ μ°Έμ‘°λ³€μˆ˜μ˜ μ£Όμ†Œ μ•ž 뢀뢄은 쑰상 클래슀의 λ§€μ„œλ“œμ™€ 속성듀을 가지기 λ•Œλ¬Έμ— λ³€ν™˜μ΄ μš©μ΄ν•˜λ‹€.

java κΈ°λ³Έ package μœ„μΉ˜

0개의 λŒ“κΈ€