ν‹°μŠ€ν† λ¦¬ λ·°

(ꡬ)Java

08 Java λ©”μ„œλ“œ(method)

yeahajeong 2019. 5. 16. 16:16
λ°˜μ‘ν˜•

λ©”μ„œλ“œ


λ©”μ„œλ“œλŠ” λ°˜λ³΅λ˜λŠ” μ½”λ“œ 블둝을 λ¬Άμ–΄ 이름을 뢙여놓은 것이닀.

λ©”μ„œλ“œλŠ” λ°˜λ³΅λ˜λŠ” μ½”λ“œλ₯Ό 쀄여주고, μ’€ 더 κ΅¬μ‘°ν™”μ‹œμΌœ μš°λ¦¬κ°€ μ½”λ“œλ₯Ό μ•Œμ•„λ³΄κΈ° μ‰½κ²Œ ν•΄μ€€λ‹€.

 

λ©”μ„œλ“œ μ„ μ–Έ 방법 

<λ°˜ν™˜ μœ ν˜•> <λ©”μ„œλ“œ μ‹λ³„μž 이름> (λ§€κ°œλ³€μˆ˜) {

        κΈ°λŠ₯상세 ;

}

- λ°˜ν™˜ μœ ν˜• (return type)

λ°˜ν™˜ μœ ν˜•μ€ λ©”μ„œλ“œ μ‹€ν–‰ ν›„ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ 곳에 λ°˜ν™˜ν•˜λŠ” κ°’μ˜ 데이터 νƒ€μž…μ„ λ§ν•œλ‹€.

λ©”μ„œλ“œλŠ” λ°˜ν™˜κ°’μ΄ μžˆμ„ κ²½μš°λ„ 있고 없을 κ²½μš°λ„ μžˆλŠ”λ° μ—†λŠ” κ²½μš°μ—λŠ” voidλ₯Ό μ“΄λ‹€.

λ°˜ν™˜κ°’μ΄ μžˆλŠ” λ©”μ„œλ“œ ν˜ΈμΆœν•  λ•ŒλŠ” ν•΄λ‹Ή λ°˜ν™˜μœ ν˜•μ— λ§žλŠ” λ³€μˆ˜μ— λ°˜ν™˜κ°’μ„ μ €μž₯ν•΄μ•Όν•œλ‹€.

λ©”μ„œλ“œκ°€ μ‹€ν–‰μ˜ 결과값을 λ°˜ν™˜ν•  λ•ŒλŠ” return μ΄λΌλŠ” ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.

 

- 맀개 λ³€μˆ˜ (parameter)

맀개 λ³€μˆ˜λŠ” λ©”μ„œλ“œκ°€ 싀행될 λ•Œ ν•„μš”ν•œ 데이터λ₯Ό μ™ΈλΆ€λ‘œλΆ€ν„° λ°›κΈ° μœ„ν•΄ μ‚¬μš©ν•œλ‹€.

맀개 λ³€μˆ˜λ„ μΌμ’…μ˜ λ³€μˆ˜μ΄κΈ° λ•Œλ¬Έμ— λ©”μ„œλ“œμ˜ μ†Œκ΄„ν˜Έ( ) 뢀뢄에 데이터 νƒ€μž…κ³Ό ν•¨κ»˜ μ„ μ–Έν•΄μ•Ό ν•˜λ©° μ΄ˆκΈ°ν™”λŠ” ν•˜μ§€ μ•ŠλŠλ‹€.

맀개 λ³€μˆ˜λ₯Ό μ—¬λŸ¬κ°œ μ„ μ–Έν•  μˆ˜λ„ 있고 λ©”μ„œλ“œκ°€ μ™ΈλΆ€λ‘œ λΆ€ν„° 받을 데이터가 μ—†λ‹€λ©΄ μ„ μ–Έν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.

public class MethodEx1 {

	public static void main(String[] args) {
		
		/*
		 * 1. λ©”μ„œλ“œλŠ” μ„ μ–Έν•˜λŠ” κ³Όμ •(λ§Œλ“œλŠ” κ³Όμ •) ν˜ΈμΆœν•˜λŠ” κ³Όμ •(μ‚¬μš©ν•˜λŠ” κ³Όμ •)이 μžˆλ‹€.
		 * 2. λ©”μ„œλ“œμ˜ 선언은 λ©”μ„œλ“œ λ‚΄λΆ€μ—μ„œλŠ” ν•  수 μ—†λ‹€.
		 * 3. λ©”μ„œλ“œ μ„ μ–Έ μœ„μΉ˜λŠ” λ©”μ„œλ“œ λ‚΄λΆ€κ°€ μ•„λ‹ˆλΌλ©΄ 클래슀 μ–΄λ””μ„œλ“ μ§€ κ°€λŠ₯ν•˜λ‹€.(mainμ—μ„œ λ§Œλ“€μ§€λ§ˆλΌ)
		 * 4. λ©”μ„œλ“œμ˜ λ‚΄λΆ€μ—μ„œλŠ” λ‹€λ₯Έ λ©”μ„œλ“œμ˜ ν˜ΈμΆœμ€ κ°€λŠ₯ν•˜λ‹€.
		 * 5. ν˜ΈμΆœμ€ μ‚¬μš©ν•˜λŠ” 과정이며 ν•΄λ‹Ή λ©”μ„œλ“œκ°€ ν•„μš”λ‘œν•œ λ§€κ°œκ°’μ„ μ „λ‹¬ν•˜λ©΄ 
		 * 	    λ©”μ„œλ“œλŠ” λ§€κ°œκ°’μ„ 톡해 μ²˜λ¦¬ν•œ κ²°κ³Όλ₯Ό ν˜ΈμΆœλΆ€λ‘œ μ „λ‹¬ν•œλ‹€.
		 */
		
		
		System.out.println( "1λΆ€ν„° 10κΉŒμ§€μ˜ ν•© : " + calSum1(10) );
		System.out.println( "1λΆ€ν„° 20κΉŒμ§€μ˜ ν•© : " + calSum1(20) );
		
		int sum = calSum1(40);
		System.out.println( "1λΆ€ν„° 40κΉŒμ§€μ˜ ν•© : " + sum );

		System.out.println("10λΆ€ν„° 20κΉŒμ§€μ˜ ν•© : " + calSum2(10, 20));

	} //end of main
	
	
	static int calSum1(int end) {
		int sum = 0;
		for(int i = 1; i <= end; i++) {
			sum += i;
		}
		return sum;
	}
	
	static int calSum2(int begin, int end) {
		int sum = 0;
		for(int i = begin; i <= end; i++) {
			sum += i;
		}
		return sum;
	}
}
public class MethodEx2 {
	
	public static void main(String[] args) {
		
		/*
		 * 맀개 λ³€μˆ˜ (parameter)
		 * 1. 맀개 λ³€μˆ˜λŠ” λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ 싀행에 ν•„μš”ν•œ 값을 λ©”μ„œλ“œ 내뢀에 μ „λ‹¬ν•˜κΈ° μœ„ν•œ 맀개체
		 * 2. 맀개 λ³€μˆ˜λ₯Ό λͺ‡κ°œλ₯Ό λ°›μ„μ§€λŠ” λ©”μ„œλ“œλ₯Ό 생성할 λ•Œ κ²°μ •ν•œλ‹€.
		 * 3. 맀개 λ³€μˆ˜λŠ” ν•˜λ‚˜λ„ 받지 μ•Šμ„ 수 있으며 μ΄λ•ŒλŠ” ()λ₯Ό λΉ„μ›Œλ‘”λ‹€.
		 */
		
		System.out.println(calSum());
		
		String result = random();
		System.out.println(result);
	}
	
	static int calSum() {
		int sum = 0;
		for(int i = 1; i <=10; i++) {
			sum += i;
		}
		return sum;
	}
	
	static String random() {
		double d = Math.random();
		
		//String str = null;
		if(d > 0.66) {
			//str = "κ°€μœ„";
			return "κ°€μœ„";
		} else if(d > 0.33) {
			//str = "λ°”μœ„";
			return "λ°”μœ„";
		} else {
			//str = "보";
			return "보";
		}
		//return str;
	}
}
public class MethodEx3 {

	public static void main(String[] args) {
		
		/*
		 * λ°˜ν™˜ μœ ν˜•(return type)
		 * 1. λ°˜ν™˜κ°’μ€ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ 곳으둜 μ΅œμ’… μ‹€ν–‰κ²°κ³Όλ₯Ό μ „λ‹¬ν•˜λŠ” 값이닀.
		 * 2. λ§€κ°œλ³€μˆ˜λŠ” μ—¬λŸ¬κ°œ μ‘΄μž¬ν•  수 μžˆμ§€λ§Œ λ°˜ν™˜κ°’μ€ ν•˜λ‚˜μ—¬μ•Ό ν•œλ‹€.
		 * 3. λ°˜ν™˜ μœ ν˜•μ€ λ°˜ν™˜κ°’μ˜ 데이터 νƒ€μž…μ΄λ‹€.
		 * 4. λ°˜ν™˜ 값이 μžˆλŠ” λ©”μ„œλ“œλŠ” 결둠적으둜 ν•˜λ‚˜μ˜ 값이 되기 λ•Œλ¬Έμ— λ°˜ν™˜λ˜λŠ” 값을 λ‹€λ₯Έ λ³€μˆ˜μ— μ €μž₯ν•  μˆ˜λ„ 있고 λ‹€λ₯Έ λ©”μ„œλ“œμ˜ λ§€κ°œκ°’μœΌλ‘œ μ‚¬μš© κ°€λŠ₯ ν•˜λ‹€.
		 * 5. λ©”μ„œλ“œ μ‹€ν–‰ ν›„ λ°˜ν™˜ 값이 μ—†λ‹€λ©΄ return을 μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€. κ·Έλ•Œ λ°˜ν™˜ μœ ν˜•μ„ void라고 μ„ μ–Έν•œλ‹€.
		 * 6. λͺ¨λ“  λ©”μ„œλ“œλŠ” return을 λ§Œλ‚˜λ©΄ κ°•μ œ μ’…λ£Œλœλ‹€. λ”°λΌμ„œ return μ•„λž˜λ‘œ μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μ—†λ‹€.(ifλ¬Έ μ•ˆμ—μ„œ ν•œμ • κ°€λŠ₯)
		 */
		
		int result = sum(3, 5);
		System.out.println(result);
		
		sum(sum(1,2), sum(1,2)); //μ΄λŸ°μ‹μœΌλ‘œ μ‚¬μš©ν•˜λŠ”κ²ƒλ„ κ°€λŠ₯ν•˜λ‹€.
		
		//voidν˜• λ©”μ„œλ“œλŠ” λ‹¨λ…μœΌλ‘œ 호좜만 ν•΄μ„œ μ‚¬μš©ν•΄μ•Όν•œλ‹€.
		multi(4, 5);
		
		sayBabo("바보");
		
	}
	
	//intν˜• λ§€κ°œλ³€μˆ˜λ‘œ 전달 받은 2개의 κ°’μ˜ 합을 λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œ
	static int sum(int n1, int n2) {
		return n1 + n2;
	}
	
	//λ°˜ν™˜ 값이 μ—†κ³  λ§€κ°œλ³€μˆ˜ λ‘κ°œμ˜ 합을 κ΅¬ν•˜λŠ” λ©”μ„œλ“œ
	static void multi(int n1, int n2) {
		System.out.println("n1 X n2" + " = " + n1*n2);
		return; //μ‚¬μš©μ΄ κ°€λŠ₯ν•˜μ§€λ§Œ μ’…λ£Œν•œλ‹€λŠ” 의미
	}
	
	//λ¬Έμžμ—΄ νƒ€μž…μ˜ λ§€κ°œλ³€μˆ˜λ₯Ό λ°›κ³  λ°˜ν™˜κ°’μ΄ μ—†λŠ” λ§€μ„œλ“œ
	static void sayBabo(String nick) {
		if(nick.equals("바보")) {
			System.out.println("바보ㅋㅋㅋ");
			return; //λ©”μ„œλ“œλ₯Ό 끝내버리고싢닀.
		}
		System.out.println(nick + " μ•„λ‹ˆλΌ 바보λ₯Ό μž…λ ₯ν•˜μ„Έμš”");
	}
	
}
λ°˜μ‘ν˜•
λŒ“κΈ€