μƒμ„±μžμ˜ λͺ©μ 

1. νž™ λ©”λͺ¨λ¦¬ μ˜μ—­μ— 클래슀 ν•„λ“œλ₯Ό μƒμ„±ν•΄μ£ΌλŠ” λͺ©μ 

2. μ΄ˆκΈ°ν™” μ—­ν• 

 

λ©”μ„œλ“œμ˜ λͺ¨μ–‘ :  name ( )

μƒμ„±μžμ˜ λͺ¨μ–‘ : class name ( )

 

μœ„μ²˜λŸΌ μƒμ„±μžλ„ λ©”μ„œλ“œμ™€ μƒκΉ€μƒˆκ°€ λΉ„μŠ·ν•˜λ„€?

μƒμ„±μžλ„ λ©”μ„œλ“œλ‹€

κ·Έλ ‡λ‹€λ©΄ μ™œ μƒμ„±μžλŠ” λ©”μ„œλ“œλΌκ³  ν•˜μ§€ μ•Šμ„κΉŒ?

 


 

μƒμ„±μžλŠ” λ©”μ„œλ“œμ˜ κΈ°λŠ₯κ³Ό λ˜‘κ°™μ§€λ§Œ return이 μ—†κΈ° λ•Œλ¬Έμ— λ©”μ„œλ“œλΌκ³  λΆ€λ₯΄μ§€ μ•ŠλŠ”λ‹€.

 


클래슀λ₯Ό λ§Œλ“œλŠ” μˆœκ°„ ꡳ이 μƒμ„±μžλ₯Ό 일뢀λ₯Ό λ§Œλ“€μ–΄μ£Όμ§€ μ•Šμ•„λ„ κΈ°λ³Έ μƒμ„±μžλΌλŠ” 것이 μƒκΈ°λŠ”λ°, 보이진 μ•Šμ§€λ§Œ λ‚΄λΆ€μ μœΌλ‘œ μ˜¬λΌκ°„ 것이닀.

λ”°λΌμ„œ 선언없이 μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.

 

κΈ°λ³Έ μƒμ„±μž

1. 클래슀 μ„ μ–Έ μ‹œ μžλ™μœΌλ‘œ μƒμ„±λœλ‹€.

2. μ‚¬μš©μžκ°€ 직접 μ„ μ–Έν•˜μ§€ μ•Šμ•„λ„ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.

3. μ‚¬μš©μžκ°€ 직접 μƒμ„±μžλ₯Ό μ„ μ–Έν•˜λŠ” μˆœκ°„ 그것을 κΈ°λ³Έ μƒμ„±μžλ‘œ μ—¬κ²¨μ„œ λ”°λ‘œ κΈ°λ³Έ μƒμ„±μžκ°€ 생기진 μ•ŠλŠ”λ‹€.

 


< λ³€μˆ˜ >

λ§€κ°œλ³€μˆ˜ : { } μ•ˆ, λ‹«λŠ” μ€‘κ΄„ν˜Έλ₯Ό λ§Œλ‚  λ•Œ λλ‚œλ‹€

맀개 λ³€μˆ˜μ™€ μ§€μ—­λ³€μˆ˜λŠ” stack에 μ €μž₯μ΄λœλ‹€.

μ „μ—­ λ³€μˆ˜λŠ” data μ˜μ—­μ— μ €μž₯λœλ‹€.

 


μ˜€λ²„ λ‘œλ”© Overloading

λ§€κ°œλ³€μˆ˜μ˜ κ°œμˆ˜λ‚˜ νƒ€μž…μ΄ λ‹€λ₯Ό λ•Œ, 같은 μ΄λ¦„μœΌλ‘œ μ„ μ–Έν•  수 μžˆλ‹€.

         →  λ©”μ„œλ“œμ˜ 이름은 κ°™μœΌλ‚˜ λ§€κ°œλ³€μˆ˜μ˜ 갯수 ν˜Ήμ€ νƒ€μž…μ΄ λ‹€λ₯΄λ©΄ μ„ μ–Έ κ°€λŠ₯

load : λ‚˜κ°”λ‹€κ°€ λ‹€μ‹œ 뢈러올 λ•Œ

over : λ„˜μΉ˜κ²Œ

overload : λ„˜μΉ˜κ²Œ λΆˆλŸ¬μ˜¨λ‹€ (같은 이름이라) 

 

γ€Š μ˜€λ²„ λ‘œλ”©μ€ λ©”μ„œλ“œμ˜ 첫 번째 κΈ°λŠ₯ ≫

 


Car와 Roadλ₯Ό λ§Œλ“€μ–΄λ³΄μž

 

package studyalone;

public class Car {
	//Car ν΄λž˜μŠ€λŠ” λΈŒλžœλ“œ, 색, 가격, λΉ„λ²ˆμ„ κ°–κ³  μžˆλ‹€
	String brand;
	String color;
	int price;
	String pw="1122";
	
	//μƒμ„±μž , λΉ„λ²ˆ μƒˆλ‘œ
	public Car(String b, String c ,int p,String pw) {
		this.brand=b; this.color=c; this.price=p;this.pw=pw;
	}
	
	//μƒμ„±μž , 초기 λΉ„λ²ˆ κ·ΈλŒ€λ‘œ
	public Car(String b, String c ,int p) {
		this.brand=b; this.color=c; this.price=p;
	}
	
	//μ™ΈλΆ€μ—μ„œ λΉ„λ°€λ²ˆν˜Έ μž…λ ₯λ°›κΈ°
	//μž…λ ₯받은 λΉ„λ°€λ²ˆν˜Έμ™€ μžλ™μ°¨μ˜ λΉ„λ°€λ²ˆν˜Έλ₯Ό λΉ„κ΅ν•˜κΈ°
	//λΉ„λ°€λ²ˆν˜Έκ°€ μΌμΉ˜ν•œλ‹€λ©΄ μ‹œλ™ 켜주기
	//이미 μ‹œλ™μ΄ μΌœμ Έμžˆλ‹€λ©΄ "μ‹œλ™μ΄ 이미 μΌœμ ΈμžˆμŠ΅λ‹ˆλ‹€"좜λ ₯
	//이미 μ‹œλ™μ΄ κΊΌμ Έμžˆλ‹€λ©΄ "μ‹œλ™μ΄ 이미 κΊΌμ ΈμžˆμŠ΅λ‹ˆλ‹€"좜λ ₯
	//λΉ„λ²ˆ 3회 였λ₯˜ μ‹œ κ²½μ°° μΆœλ™
	
	boolean isOn=false;
	int policeCnt;
	
	//μ‹œλ™ν‚€λŠ” λ©”μ„œλ“œ
	boolean engineStart(String pw ){
		boolean policeCheck=false;
		
		if(this.pw.equals(pw)) {
			if(!isOn) {
				System.out.println(this.brand+" μ‹œλ™ ν‚΄");
				isOn=true;
				policeCnt=0;
			}else {
				System.out.println(this.brand+" μ‹œλ™μ΄ 이미 켜져있음");
			}
		}else {
			policeCnt++;
			if(policeCnt==3) {
				System.out.println("κ²½μ°° μΆœλ™");
				policeCheck=true;
			}else {
				System.out.println("λΉ„λ°€λ²ˆν˜Έ 였λ₯˜");
			}
		}
		return policeCheck;
	}
	
	
	//μ‹œλ™λ„λŠ” λ©”μ„œλ“œ
	void engineStop(){
		if(!isOn) {
			System.out.println(this.brand+" μ‹œλ™ 끔");
		}else {
			System.out.println("μ‹œλ™μ΄ 이미 κΊΌμ ΈμžˆμŠ΅λ‹ˆλ‹€.");
		}
	}
	
	
	//μžλ™μ°¨ 정보 좜λ ₯ λ©”μ„œλ“œ
	void show() {
		System.out.println(brand+", "+color+", "+price+"λ§Œμ›");
		//μ§€μ—­λ³€μˆ˜κ°€ 같은 이름이 μ•„λ‹ˆλΌμ„œ ꡳ이 this.brandλ“±λ“±μœΌλ‘œ ν•˜μ§€ μ•Šμ€ 것
	}

	
}
package studyalone;

import java.util.Scanner;

public class Road {
	public static void main(String[] args) {
		Car myCar=new Car("벀틀리","blue",1500,"981122");
		
		String menu= "1. μ‹œλ™ 켜기\n2. μ‹œλ™ 끄기";
		Scanner sc =new Scanner(System.in);
		int choice=0;
		String tryPw="";
		
		myCar.show();
		
		//λ¬΄ν•œλ°˜λ³΅ 
		//μ‹œλ™μ„ ν•œλ²ˆμ΄λΌλ„ 킨 ν›„ μ‹œλ™μ„ 끄면  break
		while(true) {
			System.out.println(menu);
			
			choice=sc.nextInt();
			
			if(choice==1) {
				System.out.println("λΉ„λ°€λ²ˆν˜Έλ₯Ό μž…λ ₯ν•˜μ„Έμš”");
				tryPw=sc.next();
				
				if(myCar.engineStart(tryPw))
					break;
			}else if(choice==2) {
				//μ‹œλ™μ„ ν•œλ²ˆμ΄λΌλ„ 켜기 μ „μ—” isOn이 falseλ‹ˆκΉŒ breakμ•ˆν•¨
				//ν•œλ²ˆμ΄λΌλ„ μ‹œλ™μ„ κ²Όλ‹€λ©΄ isOn은 trueλ‹ˆκΉŒ break함
				if(myCar.isOn) {
					myCar.engineStop();
				}else {
					myCar.engineStop();
				}
			}
		}
		
	}
}

+ Recent posts