Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

๐ŸŒทJava: ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ ๋ชฉ๋ก [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 24์ผ์ฐจ~] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

๐ŸŒทJava: ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ ๋ชฉ๋ก [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 24์ผ์ฐจ~]

yjyuwisely 2023. 2. 18. 07:00

230218 Sat

ํ˜„์žฌ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๋Š” ์ค‘์ธ๋ฐ ๋‚ด๊ฐ€ ๋‹ค์‹œ ๋ณด๊ฒŒ ๋˜๋Š” ๊ฒƒ์„ ๊ธ€๋กœ ์ •๋ฆฌํ–ˆ๋‹ค.๐Ÿ˜Š

๋ชฉ์ฐจ
1. ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด ์ƒ์„ฑ: new ์—ฐ์‚ฐ์ž
2. return; return true; return false; 
3. this ์ฐธ์กฐ ๋ณ€์ˆ˜

4. ์ •์  ๋ฉค๋ฒ„ static
5. Scanner

์šฉ์–ด ๋ชฉ๋ก 

public class A{
          //ํ•„๋“œ
          public int a;

          //์ƒ์„ฑ์ž
          public A(){}

          //๋ฉ”์„œ๋“œ
          public void add(){}
          (protected / default(์ƒ๋žต) / private) 

ํ•„๋“œ field [6์žฅ 212์ชฝ] ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ(๋ฉค๋ฒ„ ๋ณ€์ˆ˜)๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณณ. ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ ์ „์ฒด์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜์ง€ ์•Š๋Š” ํ•œ ๊ฐ์ฒด์™€ ํ•จ๊ป˜ ์กด์žฌํ•œ๋‹ค.

์ƒ์„ฑ์ž constructor [6์žฅ 221์ชฝ] new ์—ฐ์‚ฐ์ž๋กœ ํ˜ธ์ถœ๋˜๋Š” ํŠน๋ณ„ํ•œ ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก. ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ, ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋˜์–ด ์žˆ๊ณ  ๋ฐ˜ํ™˜ํ˜•์ด ์—†๋‹ค.
๊ฐ์ฒด ์ดˆ๊ธฐํ™”๋ž€ ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜, ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๋ฅผ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

๋ฉ”์„œ๋“œ method [1์žฅ 37์ชฝ] ์–ด๋–ค ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์‹คํ–‰๋ฌธ๋“ค์„ ๋ชจ์•„ ๋†“์€ ๋ธ”๋ก
•๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ: ๋ฉ”์†Œ๋“œ๋ฅผ ์ฝ”๋“œ ๋‚ด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ


2023.02.09 - [1. Revision 2023/Java] - [30] 230209 Ch. 6 ํด๋ž˜์Šค(1): ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ํ•„๋“œ, ์ƒ์„ฑ์ž

์ธ์Šคํ„ด์Šค: ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด
์ธ์Šคํ„ด์Šคํ™”
ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •

๊ฐœ๋ฐœ์ž  [์„ค๊ณ„] → ํด๋ž˜์Šค(์„ค๊ณ„๋„) → [์ธ์Šคํ„ด์Šคํ™”] → ์ธ์Šคํ„ด์Šค(๊ฐ์ฒด)

์ƒ์„ฑ์ž: new ํ‚ค์›Œ๋“œ๋กœ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋˜์–ด ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.
(๊ฐ์ฒด ์ƒ์„ฑ: Car mycar = new Car();)

์ดˆ๊ธฐํ™”: initialize [2์žฅ 50์ชฝ] ๋ณ€์ˆ˜์— ์ดˆ๊ธฐ๊ฐ’์„ ์ฃผ๋Š” ํ–‰์œ„


๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€ method signature [6์žฅ 247์ชฝ]
๋ฆฌํ„ดํ˜•, ๋ฉ”์†Œ๋“œ ์ด๋ฆ„, ๋งค๊ฐœ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ํฌํ•จํ•˜๋Š” ๊ฒƒ. ์˜๋ฌธ๋ช… ๊ทธ๋Œ€๋กœ ๋ฉ”์„œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ผ๊ณ ๋„ ํ•œ๋‹ค.


๋ฉ”์„œ๋“œ, ํž™, ์Šคํƒ ์˜์—ญ

2023.02.06 - [1. Revision 2023/Java] - [27] 230206 Ch5 ์ž๋ฐ” ์ฐธ์กฐ ํƒ€์ž…(1): ์ฐธ์กฐ ํƒ€์ž…๊ณผ ์ฐธ์กฐ ๋ณ€์ˆ˜

- ๋ฉ”์„œ๋“œ ์˜์—ญ(Method Area)์—๋Š” ์ •์  ํ•„๋“œ, ์ƒ์ˆ˜, ๋ฉ”์„œ๋“œ ์ฝ”๋“œ, ์ƒ์„ฑ์ž ์ฝ”๋“œ๊ฐ€ ์œ„์น˜ํ•˜๋ฉฐ, (๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.) 
ํž™ ์˜์—ญ(Heap Area)์—๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
- ์Šคํƒ ์˜์—ญ(Stack)์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.


2023.02.01 - [1. Revision 2023/Java] - [24] 230201 Ch. 3 ์ž๋ฐ” ์—ฐ์‚ฐ์ž: ์—ฐ์‚ฐ์ž์™€ ์—ฐ์‚ฐ์‹, ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜

์—ฐ์‚ฐ์ž(operator): ์—ฐ์‚ฐ์— ์‚ฌ์šฉ๋˜๋Š” ํ‘œ์‹œ๋‚˜ ๊ธฐํ˜ธ

ํ”ผ์—ฐ์‚ฐ์ž(operand): ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์—ฐ์‚ฐ๋˜๋Š” ๋ฐ์ดํ„ฐ


[1] ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด ์ƒ์„ฑ: new ์—ฐ์‚ฐ์ž

2023.02.09 - [1. Revision 2023/Java] - [30] 230209 Ch. 6 ํด๋ž˜์Šค(1): ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ํ•„๋“œ, ์ƒ์„ฑ์ž

6.1.5 ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ํด๋ž˜์Šค ๋ณ€์ˆ˜ p. 218

ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

public class Car{ //Carํด๋ž˜์Šค ์„ ์–ธ

}

//Carํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ
new Car();

new๋Š” ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚ค๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.
new ์—ฐ์‚ฐ์ž๋Š” ํž™ ์˜์—ญ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚จ ํ›„ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ๋˜์–ด ์žˆ๋‹ค. 

//ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์— new ์—ฐ์‚ฐ์ž๊ฐ€ ๋ฆฌํ„ดํ•œ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ์ €์žฅํ•œ๋‹ค.
ํด๋ž˜์Šค ๋ณ€์ˆ˜;
๋ณ€์ˆ˜ = new ํด๋ž˜์Šค();

//1๊ฐœ์˜ ์‹คํ–‰๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
ํด๋ž˜์Šค ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค();

 

Student์™€ StudentExample ํด๋ž˜์Šค์˜ ์šฉ๋„ p. 220

์˜ˆ์ œ 1) Student ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ  StudentExample ํด๋ž˜์Šค์˜ main() ๋ฉ”์„œ๋“œ์—์„œ Student ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

package ch06;
//ํด๋ž˜์Šค ์„ ์–ธ
public class Student {

}
package ch06;
//main๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค์— ๊ฐ์ฒด ์ƒ์„ฑ 
public class StudentExample {
	public static void main(String[] args) {
		Student s1 = new Student();
		System.out.println("s1 ๋ณ€์ˆ˜๊ฐ€ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.");
	
		Student s2 = new Student();
		System.out.println("s2 ๋ณ€์ˆ˜๊ฐ€ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.");
	}
}

๊ฒฐ๊ณผ)

s1 ๋ณ€์ˆ˜๊ฐ€ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
s2 ๋ณ€์ˆ˜๊ฐ€ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ 2)

public class Car {//Car ํด๋ž˜์Šค ์„ ์–ธ
	//์†์„ฑ
	//์ฐจ์˜ ์ƒ‰๊น”
	String color;
	//์†๋„์˜ ์ˆซ์ž
	int speed;
	//๊ธฐ๋Šฅ
	//๋‹ฌ๋ฆฐ๋‹ค.
	//๋ฉˆ์ถ˜๋‹ค.
}
//main๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด ์ƒ์„ฑ
public class CarExample {
	public static void main(String[] args) {
	// 	int a = 10
		Car c1 = new Car();//Carํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ c1์ฐธ์กฐ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค. 
		Car c2 = new Car();//Carํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ c2์ฐธ์กฐ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค. 
	}
}

[2] return; return true; return false; 

2023.02.10 - [1. Revision 2023/Java] - [31] 230210 Ch. 6 ํด๋ž˜์Šค(2): ๋ฉ”์„œ๋“œ

6.4.2 ๋ฆฌํ„ด(return)๋ฌธ p. 254

1) ๋ฆฌํ„ด๊ฐ’์ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ

return ๋ฆฌํ„ด๊ฐ’;

return๋ฌธ ์ดํ›„์˜ ์‹คํ–‰๋ฌธ์€ ๊ฒฐ์ฝ” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. 


2) ๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ: void p. 255

๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ void๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
void๋กœ ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ์—์„œ๋„ return๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌํ„ด๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ
๋ฉ”์„œ๋“œ ์‹คํ–‰์„ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ์—ญํ• ์ด๋‹ค.

return;
return (null): null๋กœ returnํ•œ๋‹ค.
submit์„ ํ•˜์ง€ ๋ชปํ•˜๊ณ  null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•ด๋‹น return ์•„๋ž˜๋ฅผ ์ „๋ถ€ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  return ์ค„์—์„œ ํด๋ž˜์Šค๋ฅผ ๋๋‚ด๋ฒ„๋ฆฐ๋‹ค.

return true: submit ํ•˜๊ธฐ ์ „์— ํ•จ์ˆ˜์—์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ  ํ†ต๊ณผ๊ฐ€ ๋˜๋ฉด true(=submit true)๋กœ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

return false: ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์ง€ ์•Š์œผ๋ฉด submit์„ ํ•˜์ง€ ์•Š๊ณ  fasle๋กœ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๋œป(return null๊ณผ ์•„์ฃผ ์กฐ๊ธˆ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.)์ด๋‹ค.

์ฐธ๊ณ : https://diaryofgreen.tistory.com/80

์˜ˆ์ œ 1) return๋ฌธ

public class Car {
	//ํ•„๋“œ
	int gas;
	
	//์ƒ์„ฑ์ž
	
	//๋ฉ”์„œ๋“œ
	void setGas(int gas) {
		this.gas = gas; //๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋กœ ๋งค๊ฐœ๊ฐ’์„ ๋ฐ›์•„์„œ gas ํ•„๋“œ๊ฐ’์„ ๋ณ€๊ฒฝ
	}
	
	//๋ฆฌํ„ด๊ฐ’์ด boolean์ธ ๋ฉ”์„œ๋“œ๋กœ gasํ•„๋“œ๊ฐ’์ด 0์ด๋ฉด false๋ฅผ, 0์ด ์•„๋‹ˆ๋ฉด true๋ฅผ ๋ฆฌํ„ด
	boolean isLeftGas() {
		if(gas == 0) {
			System.out.println("gas๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
			return false; //false๋ฅผ ๋ฆฌํ„ด
		}
		System.out.println("gas๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.");
		return true; //true๋ฅผ ๋ฆฌํ„ด
	}
	
	//๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋กœ gas ํ•„๋“œ๊ฐ’์ด 0์ด๋ฉด return๋ฌธ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ•์ œ์ข…๋ฃŒ
	void run() {
		while(true) {
			if(gas > 0) {
				System.out.println("๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:" + gas + ")");
				gas -= 1;
			}else {
				System.out.println("๋ฉˆ์ถฅ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:" + gas + ")");
				return;
			}
		}
	}
}



์˜ˆ์ œ 2) return๋ฌธ

public class CarExample {
	public static void main(String[] args) {
		Car myCar = new Car();
		
		//Car์˜ setGas ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		myCar.setGas(5); 
		
		//isLeftGas๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		boolean gasState = myCar.isLeftGas();
		if(gasState){
			System.out.println("์ถœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.");
			myCar.run(); //Car์˜ run() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		}
		
		//Car์˜ isLeftGas() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		if(myCar.isLeftGas()) {
			System.out.println("gas๋ฅผ ์ฃผ์ž…ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
		}else {
			System.out.println("gas๋ฅผ ์ฃผ์ž…ํ•˜์„ธ์š”.");
		}
	}
}

๊ฒฐ๊ณผ) 

gas๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์ถœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:5)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:4)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:3)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:2)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:1)
๋ฉˆ์ถฅ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:0)
gas๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
gas๋ฅผ ์ฃผ์ž…ํ•˜์„ธ์š”.

[3] this ์ฐธ์กฐ ๋ณ€์ˆ˜

2023.02.13 - [1. Revision 2023/Java] - [32] 230213 Ch6 ํด๋ž˜์Šค(3): ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ ์ •์  ๋ฉค๋ฒ„

'this.ํ•„๋“œ'๋Š” this๋ผ๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค.

this ์ฐธ์กฐ ๋ณ€์ˆ˜
this ์ฐธ์กฐ ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”๋กœ ์ž๊ธฐ ์ž์‹ ์„ ์ฐธ์กฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ this ์ฐธ์กฐ ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ : http://www.tcpschool.com/java/java_methodConstructor_this

this, this() ์ฐจ์ด
this๋Š” ์ธ์Šคํ„ด์Šค ์ž์‹ ์„ ๊ฐ€๋ฅดํ‚ค๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๊ณ  this()๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋œปํ•œ๋‹ค.
์ฐธ๊ณ : https://lowelllll.github.io/java/2018/12/09/java-this-%EC%B0%A8%EC%9D%B4%EC%A0%90/
์ฐธ๊ณ : https://itprogramming119.tistory.com/149


2) this p. 272

๊ฐ์ฒด ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์ฐธ์กฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ๋„ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด this๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ž์‹ ์„ '๋‚˜'๋ผ๊ณ  ๊ฐ€๋ฆฌํ‚ค๋“ฏ์ด ๊ฐ์ฒด๋Š” ์ž์‹ ์„ this๋ผ๊ณ  ํ•œ๋‹ค.
ex) this.model = ์ž์‹ ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” model ํ•„๋“œ

this๋Š” ์ฃผ๋กœ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ํ•„๋“œ์™€ ๋™์ผํ•œ ๊ฒฝ์šฐ, ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์ธ ํ•„๋“œ๋ฅผ ๋ช…์‹œํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

๋‹ค์Œ์€ ๋งค๊ฐœ ๋ณ€์ˆ˜ model์˜ ๊ฐ’์„ ํ•„๋“œ model์— ์ €์žฅํ•œ๋‹ค.

Car(String model){
	this.model = model;
}
void setModel(String model){
	this.model = model;
}



์˜ˆ์‹œ) ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ this: Car.java

public class Car3 {
	//ํ•„๋“œ
	String model;
	int speed;
	
	//์ƒ์„ฑ์ž
	Car3(String model){
		this.model = model; 
	}
	
	//๋ฉ”์„œ๋“œ
	void setSpeed(int speed) {
		this.speed = speed;
	}
	
	void run() {
		for(int i = 10; i<=50; i+=10) {
			this.setSpeed(i);
			System.out.println(this.model +"๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:" + this.speed + "km/h)");			
		}
	}
}

์˜ˆ์‹œ) ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ this: CarExample.java

public class Car3Example {
	public static void main(String[] args) {
		Car3 myCar = new Car3("ํฌ๋ฅด์‰");
		Car3 yourCar = new Car3("๋ฒค์ธ ");
		
		myCar.run();
		yourCar.run();
	}
}

๊ฒฐ๊ณผ)

ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:10km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:20km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:30km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:40km/h)
ํฌ๋ฅด์‰๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:50km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:10km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:20km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:30km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:40km/h)
๋ฒค์ธ ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†:50km/h)

[4] ์ •์  ๋ฉค๋ฒ„ static

2023.02.13 - [1. Revision 2023/Java] - [32] 230213 Ch6 ํด๋ž˜์Šค(3): ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ ์ •์  ๋ฉค๋ฒ„

์–ด๋–ค ๊ฐ์ฒด์— ์†Œ์†๋˜์–ด์žˆ๋‹ค๋Š” ๋œป์€ new ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ์•ผ๋งŒ ํ•ด๋‹น ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. static ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ๊ฐ์ฒด ์ƒ์„ฑ ์—†์ด๋„ ๋ฉ”์„œ๋“œ๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฐธ๊ณ : https://jooona.tistory.com/m/164

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

์ •์ (static): ๊ณ ์ •๋œ์ด๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ์ •์  ๋ฉค๋ฒ„๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.

์ •์  ๋ฉค๋ฒ„: ํด๋ž˜์Šค์— ๊ณ ์ •๋œ ๋ฉค๋ฒ„๋กœ์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•œ๋‹ค. 
๊ฐ๊ฐ ์ •์  ํ•„๋“œ, ์ •์  ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
์ •์ ๋ฉค๋ฒ„๋Š” ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค์— ์†Œ์†๋œ ๋ฉค๋ฒ„์ด๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ๋ฉค๋ฒ„๋ผ๊ณ ๋„ ํ•œ๋‹ค. 

1) ์ •์  ๋ฉค๋ฒ„ ์„ ์–ธ

์ •์  ํ•„๋“œ์™€ ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜๋ ค๋ฉด ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ static ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ๋ถ™์ด๋ฉด ๋œ๋‹ค.

public class ํด๋ž˜์Šค{
     //์ •์  ํ•„๋“œ
     static ํƒ€์ž… ํ•„๋“œ [= ์ดˆ๊ธฐ๊ฐ’];
     
     //์ •์  ๋ฉ”์„œ๋“œ
     static ๋ฆฌํ„ด ํƒ€์ž… ๋ฉ”์„œ๋“œ (๋งค๊ฐœ๋ณ€์ˆ˜์„ ์–ธ, ...){...}
}

๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•  ๋ฐ์ดํ„ฐ: ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋กœ ์„ ์–ธ ex) ์ƒ‰๊น”์ด ๋‹ค๋ฅด๋‹ค๋ฉด color
๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ณต์šฉ ๋ฐ์ดํ„ฐ: ์ •์  ํ•„๋“œ๋กœ ์„ ์–ธ ex) ํŒŒ์ด(π)


2) ์ •์  ๋ฉค๋ฒ„ ์‚ฌ์šฉ

ํด๋ž˜์Šค๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋กœ๋”ฉ๋˜๋ฉด ์ •์  ๋ฉค๋ฒ„๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ๋„ํŠธ(.) ์—ฐ์‚ฐ์ž๋กœ ์ ‘๊ทผํ•œ๋‹ค. 

ํด๋ž˜์Šค.ํ•„๋“œ;
ํด๋ž˜์Šค.๋ฉ”์„œ๋“œ(๋งค๊ฐœ๊ฐ’, ...);
//์ •์  ํ•„๋“œ
double result1 = 10 * 10 * Calculator.pi;
//์ •์  ๋ฉ”์„œ๋“œ
int result2 = Calculator.plus(10, 5);
int result3 = Calculator.minus(10, 5);

์˜ˆ์‹œ) ์ •์  ๋ฉค๋ฒ„ ์‚ฌ์šฉ

public class Calculator3 {
	static double pi = 3.14159;
	
	static int plus(int x, int y) {
		return x + y;
	}
	
	static int minus(int x, int y) {
		return x - y;
	}
}

์˜ˆ์‹œ) ์ •์  ๋ฉค๋ฒ„ ์‚ฌ์šฉ

public class Calculator3Example {
	public static void main(String[] args) {
		double result1 = 10 * 10 * Calculator3.pi;
		
		int result2 = Calculator3.plus(10, 5);
		
		int result3 = Calculator3.minus(10, 5);
		
		System.out.println("result1 : " + result1);
		System.out.println("result2 : " + result2);
		System.out.println("result3 : " + result3);
	}
}

๊ฒฐ๊ณผ)

result1 : 314.159
result2 : 15
result3 : 5

3) ์ •์  ๋ฉค๋ฒ„ ์„ ์–ธ ์‹œ ์ฃผ์˜ํ•  ์  p. 277

๊ฐ์ฒด๊ฐ€ ์—†์–ด๋„ ์‹คํ–‰๋œ๋‹ค๋Š” ํŠน์ง• ๋•Œ๋ฌธ์— ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ๋Š” ์ด๋“ค ๋‚ด๋ถ€์— ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋‚˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋˜ํ•œ ๊ฐ์ฒด ์ž์‹ ์˜ ์ฐธ์กฐ์ธ this ํ‚ค์›Œ๋“œ๋„ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. 

์ •์  ๋ฉ”์„œ๋“œ์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ  ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

static void method3{
	ClassName obj = new ClassName();
    obj.field1 = 10;
    obj.method1();
}
public class StaticMember {
	//ํ•„๋“œ
	//์ธ์Šคํ„ด์Šค ํ•„๋“œ ์„ ์–ธ
	int instanceField;
	//์ •์  ํ•„๋“œ ์„ ์–ธ
	static int staticField;
	
	//๋ฉ”์„œ๋“œ
	//์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ์„ ์–ธ
	void instanceMethod(){
		//์ธ์Šคํ„ด์Šค ํ•„๋“œ ํ˜ธ์ถœ
		this.instanceField=10;
		//์ •์  ํ•„๋“œ ํ˜ธ์ถœ
		staticField = 20;
	}
	//์ •์  ๋ฉ”์„œ๋“œ ์„ ์–ธ
	static void staticMethod(){
		//์ •์  ๋ฉ”์„œ๋“œ์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ  ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.
		StaticMember sm = new StaticMember();
	//์ธ์Šคํ„ด์Šค ํ•„๋“œ ํ˜ธ์ถœ
	sm.instanceField = 30;
	//์ •์  ํ•„๋“œ ํ˜ธ์ถœ
	staticField = 40;
	}
}
static ๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ€์งˆ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•„๋“œ ํ˜น์€ ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋ฉ”์„œ๋“œ
the value is the same for every instance of the class.
final ํ•œ ๋ฒˆ ๊ฐ’์ด ์ •ํ•ด์ง€๊ณ  ๋‚˜๋ฉด ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋Š” ํ•„๋“œ
once the variable is assigned a value it can never be changed.
static final ๋ชจ๋“  ์˜์—ญ์—์„œ ๊ณ ์ •๋œ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์ˆ˜

์ฐธ๊ณ : https://gobae.tistory.com/3


230224 Fri
Scanner ์˜ˆ์‹œ

2023.02.24 - [๐ŸŒณK-Digital Revision 2023โœจ/Python] - [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 41์ผ] 230224 Ch. 3 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ: 3. ํŒŒ์ด์ฌ ์ œ์–ด๋ฌธ, 4. ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•, 5. ํŒŒ์ด์ฌ ํด๋ž˜์Šค์™€ ๋ชจ๋“ˆ

์˜ˆ์ œ 1)

class If01{
		public static void main(String[] args){
        		int a = 23;
                if (a < 50){
                			System.out.println("50๋ณด๋‹ค ์ž‘๊ตฐ์š”");
                }
                if(a < 20){
                			System.out.println("20๋ณด๋‹ค ์ž‘๊ตฐ์š”");
                }else{
                			System.out.println("50๋ณด๋‹ค ์ž‘๊ตฐ์š”");
                }
        }
        
        Scanner scanner = new Scanner(System.in);
        int age = Integer.parseInt(scanner.next())
        if(age < 10){
        			System.out.println("์œ ๋…„์ธต ์ž…๋‹ˆ๋‹ค.");
        }else if(age < 20){
        			System.out.println("10๋Œ€ ์ž…๋‹ˆ๋‹ค.");
        }else if(age < 30){
        			System.out.println("20๋Œ€ ์ž…๋‹ˆ๋‹ค.");
        }else if(age < 40){
        			System.out.println("30๋Œ€ ์ž…๋‹ˆ๋‹ค.");
        }else{
        			System.out.println("์žฅ๋…„์ธต์ž…๋‹ˆ๋‹ค.");
		}
    }
}

์˜ˆ์ œ 2)

import java.util.Scanner;
public class While01 {
		public static void main(String[] args){
        		String str = "๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค";
                int i = 0;
                while (i < 3){
                		System.out.println(str);
                        i = i + 1;
              
                System.out.println("---------------------");
                
                Scanner scanner = new Scanner(System.in);
                i = Integer.parseInt(scanner.next());
                int j = 1;
                boolean flag = true;
                while flag{
                		j = j + 1;
                        if (i < j){
                        		flag = false;
                        }
                        System.out.println(str);
                }
                }
}
}
728x90
๋ฐ˜์‘ํ˜•
Comments