์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

java ์ž…๋ฌธ๋ฐ˜ 4์ฃผ์ฐจ

์ˆ˜ํ˜€์ด0812 2024. 11. 29. 10:11
728x90
๋ฐ˜์‘ํ˜•

(์›”์š”์ผ)

 

# String ๊ฐ’ ๋ณ€ํ™˜์— ๋”ฐ๋ฅธ ์ฃผ์†Œ ๋ณ€ํ™˜

 

package thisisjava;

public class str03 {

	public static void main(String[] args) {
		String s1 = "KR";
		s1 = null;
		s1 = "EN";
		s1 = "CH";
		
	}
}

 

 

# String ๋ฉ”์„œ๋“œ

 

package thisisjava;

public class str04 {

	public static void main(String[] args) {
		String s1 = "Welcome to Korea";
		
		System.out.println(s1.length());
		System.out.println(s1.charAt(0));
		System.out.println(s1.charAt(5));
		System.out.println(s1.charAt(7) + ", ");
//		System.out.println(s1.charAt(-1)); -1์€ ํŒŒ์ด์ฌ์—์„œ๋งŒ!
		
		System.out.println(s1.replace("K", "C"));
		System.out.println(s1.replace("Korea", "ํ•œ๊ตญ"));
		System.out.println(s1.replace("e", "2"));
		
		System.out.println(s1.substring(0, 8));
		System.out.println(s1.substring(3, 14));
		
		System.out.println(s1.indexOf("c")); // c์˜ ์œ„์น˜
		System.out.println(
				s1.substring(
						s1.indexOf("c"), s1.indexOf("r") + 1)); // ๋‘๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ผํ•ฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ถ”์ถœ๊ฐ€๋Šฅ
		
		String s2 = "์‚ฌ๊ณผ,๋ฐฐ,๋ฐ”๋‚˜๋‚˜,์ˆ˜๋ฐ•";
		String[] sArr = s2.split(",");
		for(int i = 0; i < sArr.length; i++) {
			System.out.println(sArr[i]);
		}
		
		System.out.println();
		
	}
}

 

 

 

# ์ด์ค‘ for๋ฌธ

 

package thisisjava;

public class arr07 {

	public static void main(String[] args) {
		int[][] iArr01 = {{1, 2, 3}, 
						  {4, 5, 6}, 
						  {7, 8, 9}};
		
		System.out.println(iArr01.length); // 3ํ–‰
		System.out.println(iArr01[0].length); // 1ํ–‰์˜ ์š”์†Œ์˜ ๊ฐœ์ˆ˜
		
		System.out.println(iArr01[0][0]);
		
		// 3, 4, 8
		System.out.println(iArr01[0][2]);
		System.out.println(iArr01[1][0]);
		System.out.println(iArr01[2][1]);
		
		System.out.println();
		
		// 1 2 3 / 4 5 6 / 7 8 9 ์ถœ๋ ฅํ•˜๊ธฐ 
		for(int i = 0; i < iArr01.length; i++) {
			for(int j = 0; j < iArr01[i].length; j++) {
				System.out.print(iArr01[i][j] + " ");
			}
			System.out.println();
		}
	}
}

 

 

# string ๋ฐฐ์—ด์˜ ๋ณต์‚ฌ : ์–•์€ ๋ณต์‚ฌ(shallow copy)

 

package thisisjava;

public class arr11 {

	public static void main(String[] args) {
		int i1 = 11, i2 = 0;
		int[] iArr01 = {10, 20, 30, 40};
		int[] iArr02 = new int[4];
		
		i2 = i1; i2 = 22;
		System.out.println(i1 + ", " + i2);
		iArr02 = iArr01;
		iArr01[3] = 400;
		
		// ---> 10 20 30 40
		for(int i : iArr02) {
			System.out.print(i + " ");
		}
		
	}
}

 


 

# class ๊ธฐ์ดˆ

 

package thisisjava;

/*
 class
 - ํ•„๋“œ(์†์„ฑ), ๋ฉ”์„œ๋“œ, ์ƒ์„ฑ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค
 - ๊ฐ’๊ณผ ์ฐธ์กฐ ํƒ€์ž…์ด ํ˜ผํ•ฉ๋˜์–ด ์žˆ๋‹ค
 - ๊ฐ’์€ 0, ์ฐธ์กฐ๋Š” null์ด ๋“ค์–ด์žˆ๊ณ  ๋‚˜์ค‘์— ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ณ  ์ฐธ์กฐ๋œ๋‹ค
 - ๋ถ•์–ด๋นต ํ‹€์— ๋ถ•์–ด๋ฅผ ์ฐ์–ด๋‚ด๋“ฏ ๊ฐ™์€ ํ˜•์‹์˜ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค
 */

class Book00 {
	String title; 	// ์ฑ…๋ช…
	String author; 	// ์ €์ž
	int price; 		// ๊ฐ€๊ฒฉ
	
	// ๋ฉค๋ฒ„ ๋ฉ”์†Œ๋“œ
	// ๋ฐ˜ํ™˜Type ์ด๋ฆ„(์ž…๋ ฅ Type ์ž…๋ ฅ) {์ฒ˜๋ฆฌ; return ๋ฐ˜ํ™˜๊ฐ’;}
	// void : ๋ฐ˜ํ™˜๊ฐ’ ์—†์Œ
	void pBook() {
		System.out.println(title + ", " + author + ", " + price);
	}
}

public class Cla00 {
	public static void main(String[] args) {
		Book00 b1 = new Book00();
		b1.title = "์ž๋ฐ”"; b1.author = "ํ™๊ธธ๋™"; b1.price = 30000;	
		b1.pBook();
//		System.out.println(b1.title + ", " + b1.author + ", " + b1.price);
		
		
		Book00 b2 = new Book00();
		b2.title = "์†Œ๊ณต"; b2.author = "๊น€์€๋น„"; b2.price = 40000;
		b2.pBook();
//		System.out.println(b2.title + ", " + b2.author + ", " + b2.price);
	}
}

 

 

 

# ํด๋ž˜์Šค ๊ตฌ์„ฑ 3๊ฐ€์ง€(ํ•„๋“œ, ์ƒ์„ฑ์ž, ๋ฉ”์„œ๋“œ)

 

package thisisjava;

public class Book01 {
	// ๋ฉค๋ฒ„ ํ•„๋“œ
	String title;
	String author;
	int price;
	
	// ๋ฉค๋ฒ„ ์ƒ์„ฑ์ž(์—†๋”๋ผ๋„ ์€๋‹‰๋˜์–ด ์žˆ์Œ)
	Book01(String t, String a, int p){
		this.title = t;
		this.author = a;
		this.price = p;
	}
	
	// ๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ
	void pBook() {
		System.out.println(title + ", " + author + ", " + price);
	}
	
}

 

 

 

# ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ

 

package thisisjava;

class Book03{
	String title; String author; int price;
	// ๊ฐ•์‚ฌ๋‹˜์€ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋ฌด์ œ/์ž‘์ž๋ฏธ์ƒ/0 ๋„ฃ์–ด์ฃผ์‹ฌ
	
	// ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ
	Book03(String title, String author, int price) {
		this.title = title;
		this.author = author;
		this.price = price;
	}
	
	Book03(String title, String author) {
		this(title, author, 0);
//		this.title = title;
//		this.author = author;
//		this.price = 0;
	}
	
	Book03(String title) {
		this(title, "์ž‘์ž๋ฏธ์ƒ", 0);
//		this.title = title;
//		this.author = "์ž‘์ž๋ฏธ์ƒ";
//		this.price = 0;
	}
	
	Book03() {
		this("๋ฌด์ œ", "์ž‘์ž๋ฏธ์ƒ", 0);
//		this.title = "๋ฌด์ œ";
//		this.author = "์ž‘์ž๋ฏธ์ƒ";
//		this.price = 0;
	}
	
	void pBook() {
		System.out.println(this.title + "," + 
						   this.author + "," + this.price);
	}
}

public class Cla03 {

	public static void main(String[] args) {
		Book03 b1 = new Book03("APT", "๋กœ์ œ", 10000);
		b1.pBook(); // APT, ๋กœ์ œ, 10000;
		Book03 b2 = new Book03("APT", "Tom");
		b2.pBook(); // ์ž๋ฐ”, Tom, 0
		Book03 b3 = new Book03("๊ธฐ์‚ฌ");
		b3.pBook(); // ๊ธฐ์‚ฌ, ์ž‘์ž๋ฏธ์ƒ, 0
		Book03 b4 = new Book03();
		b4.pBook(); // ๋ฌด์ œ, ์ž‘์ž๋ฏธ์ƒ, 0
	}
}

/*
์ƒ์„ฑ์ž ์ฒด์ด๋‹ :
- ํ˜„์—…์—์„œ ์ƒ์„ฑ์ž ์ฒด์ด๋‹์€ ์ค‘๋ณต ์ฝ”๋“œ ์ œ๊ฑฐ, ์œ ์ง€ ๋ณด์ˆ˜์„ฑ ํ–ฅ์ƒ, ์ฝ”๋“œ ์ผ๊ด€์„ฑ ํ™•๋ณด ๋“ฑ์˜ ์ด์œ ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ฒ•
- ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™” ๋ฐฉ์‹์„ ๋‹จ์ˆœํ•˜๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋Š” ๋ฐ ํฐ ๋„์›€์ด ๋˜๋ฏ€๋กœ 
- ์ดˆ๊ธฐํ™”์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๊ณ  ์ค‘๋ณต์„ ์ค„์ด๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์„ค๊ณ„ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ
*/

 

 

(ํ™”์š”์ผ)

 

# ๊ฐ์ฒด(Object)

- ๋ฌผ๋ฆฌ์ ์œผ๋กœ ์กด์žฌํ•˜๊ฑฐ๋‚˜ ๊ฐœ๋…์ ์ธ ๊ฒƒ ์ค‘์—์„œ ๋‹ค๋ฅธ ๊ฒƒ๊ณผ ์‹๋ณ„ ๊ฐ€๋Šฅํ•œ ๊ฒƒ

- ์†์„ฑ๊ณผ ๋™์ž‘์œผ๋กœ ๊ตฌ์„ฑ, ์ž๋ฐ”๋Š” ์ด๋Ÿฌํ•œ ์†์„ฑ๊ณผ ๋™์ž‘์„ ๊ฐ๊ฐ ํ•„๋“œ์™€ ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ๋ถ€๋ฆ„

- ์‹ค์ œ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ์ธ์Šคํ„ด์Šค!

 

# ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(OOP)

- ๊ฐ์ฒด๋“ค์„๋จผ์ € ๋งŒ๋“ค๊ณ , ์ด ๊ฐ์ฒด๋“ค์„ํ•˜๋‚˜์”ฉ ์กฐ๋ฆฝํ•ด์„œ ์™„์„ฑ๋œ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š” ๊ธฐ๋ฒ•

  

# ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ

package thisisjava;

class Meth06 {
	int Sum(int x, int y) {
		int res = x + y;
		return res;
	}
	
	String Sum(String x, String y) {
		String res = x + y;
		return res;
	}
	
	String Sum(String x, int y, int z) {
		String res2 = x + "๊ฒฐ๊ณผ : " + Sum(y, z); 
		return res2;
	}	
}

public class Cla06 {
	public static void main(String[] args) {
		Meth06 m1 = new Meth06();
		System.out.println(m1.Sum(20, 10));	
		System.out.println(m1.Sum("๋Œ€ํ•œ", "๋ฏผ๊ตญ"));
		System.out.println(m1.Sum("๋ง์…ˆ", 30, 4));
	}
}

 

 

 

# ๊ฐ€๋ณ€๊ธธ์ด ๋งค๊ฐœ๋ณ€์ˆ˜ & ์˜ค๋ฒ„๋กœ๋”ฉ

 

package thisisjava;

import java.util.List;

class Meth07 {
	//  ๋ฐฐ์—ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ๋ฐฐ์—ด๋งŒ ํ—ˆ์šฉํ•˜๊ธฐ์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†๋‹ค
	int Sum00(int[] iArray) {
		int ans = 0;
		for(int i : iArray) {
			ans += i;
		}
		return ans;
	}
	
	int Sum01(int ... iArray) {
		int ans = 0;
		for(int i : iArray) {
			ans += i;
		}
		return ans;
	}
	
	String Sum01(String ... sArray) {
		String ans2 = "";
		for(String s : sArray) {
			ans2 += s;
		}
		return ans2;
 	}
	
	String Sum00(List<String> string) {
		StringBuilder ans = new StringBuilder();
		for(String s : string) {
			ans.append(s);
		}
		return ans.toString();
	}
}

public class Cla07 {

	public static void main(String[] args) {
		Meth07 m1 = new Meth07(); int res = 0;
		int[] iArr01 = {20, 10, 40};
		res = m1.Sum00(iArr01);
		System.out.println(res);
		res = m1.Sum00(new int[] {20, 10, 5});
		System.out.println(res);
		res = m1.Sum00(new int[] {20, 10, 5, 3});
		System.out.println(res);
		res = m1.Sum00(new int[] {20, 10, 5, 3, 2});
		System.out.println(res);
		
		res = m1.Sum01(20, 10);
		System.out.println(res);
		res = m1.Sum01(20, 10, 5);
		System.out.println(res);
		res = m1.Sum01(20, 10, 5, 3);
		System.out.println(res);
		res = m1.Sum01(20, 10, 5, 3, 2);
		System.out.println(res);
		
		String s;
		s = m1.Sum01("์„œ์ดˆ", "์—ฌ์„ฑ", "์ธ๋ ฅ");
		System.out.println(s);
		s = m1.Sum01("๊ฐœ๋ฐœ", "์„ผํ„ฐ");
		System.out.println(s);
	}
}

 

 

# staic ์‚ฌ์šฉ

package thisisjava;

class Meth08 {
	static int iMoney; // static์ด ๋ถ™์œผ๋ฉด ๋ฉ”์†Œ๋“œ ์˜์—ญ์— static์ด ์ƒ๊ธฐ๊ณ  ํž™ ์˜์—ญ์—๋Š” ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š๋Š”๋‹ค
	void setMoney(int i, int o) {
		iMoney = iMoney + i - o;
	}
	void getMoney() { System.out.println(iMoney); }
}

public class Cla08 {
	public static void main(String[] args) {
		Meth08 m = new Meth08();
		Meth08 w = new Meth08();
		
		m.setMoney(20000, 13000); m.getMoney();
		w.setMoney(30000, 10000); w.getMoney();
		
	}
}

 

 

# static ์‚ฌ์šฉ 2

package thisisjava;

class Meth09 {
	// instanceํ˜• method
	void Sum(int x, int y) {
		System.out.println(x + y);
	}
	
	// static ํ˜• method : ๊ณต๊ฐ„์„ ๋Š˜๋ฆด ํ•„์š”๊ฐ€ ์—†์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์•„๋‚„ ์ˆ˜ ์žˆ๋‹ค!
	static void Sub(int x, int y) {
		System.out.println(x - y);
	}
}

public class Cla09 {
	public static void main(String[] args) {
		int a = 20, b = 10;
		// instance ์ƒ์„ฑ ํ›„, instance.๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค
		Meth09 m1 = new Meth09(); m1.Sum(20, 10);
		Meth09 m2 = new Meth09(); m2.Sum(20, 10);
		
		// class๋ช….๋ฉค๋ฒ„๋กœ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค
		Meth09.Sub(20, 10);
	}
}

 

 

# static ์‚ฌ์šฉ 3 

 

- ์‚ฌ์น™์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ๋ฅผ static์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์€ ์ด์œ !!

package thisisjava;

class Meth10 {
	static int Sum(int x, int y) {
		return x + y;
	}
	
	static void Sub(int x, int y) {
		System.out.println(x - y);
	}
	
	static int Mul(int x, int y) {
		return x * y;
	}
	
	static void Div(int x, int y) {
		System.out.println(x / y);
	}
}

public class Cla10 {
	public static void main(String[] args) {
		int a = 20, b = 10;
		System.out.println(Meth10.Sum(a, b));
		Meth10.Sub(a, b);
		System.out.println(Meth10.Mul(a, b));		
		Meth10.Div(a, b);
	}
}

 

 

# static ์‚ฌ์šฉ 4

 

package thisisjava;

class Meth13 {

	static int cnt0 = 0;
	int cnt3 = 0;
	void count01(int cnt1) {
		cnt0++; cnt1++; cnt3++;
		System.out.printf("%d, %d, %d\n", cnt0, cnt1, cnt3);
	}
	
	void count02(int cnt2) {
		cnt0++; cnt2++; cnt3++;
		System.out.printf("%d, %d, %d\n", cnt0, cnt2, cnt3);
	}
}

public class Cla13 {

	public static void main(String[] args) {
		Meth13 m1 = new Meth13(); m1.count01(10); // 1, 11, 1 
		Meth13 m2 = new Meth13(); m2.count02(20); // 2, 21, 1
	}
}

 

 


 

(์ˆ˜์š”์ผ)

 

# static / instance / local ๋ณ€์ˆ˜ ๋น„๊ตํ•˜๊ธฐ

 

package thisisjava;

class Meth14 {
	static int staticCnt = 0;
	int instanceCnt = 0;
	void Count() {
		int localCnt = 0;
		staticCnt++; instanceCnt++; localCnt++;
		System.out.printf("%d, %d, %d\n",staticCnt, instanceCnt, localCnt);
	}
}

public class Cla14 {	
	public static void main(String[] args) {
		Meth14 m1 = new Meth14(); 
		m1.Count(); // 1, 1, 1
		m1.Count(); // 2, 2, 1
		m1.Count(); //a3, 3, 1
		
		Meth14 m2 = new Meth14(); 
		m2.Count(); // 4, 1, 1
		m2.Count(); // 5, 2, 1
		m2.Count(); // 6, 3, 1
	}
}

 

static : ๋ชจ๋‘์˜ ๊ณต์šฉ ๊ณต๊ฐ„์— ์กด์žฌ!! ํ•ญ์ƒ ๊ฐ’์ด ๋ฐ”๋€๋‹ค

instance : ๋‚ด๊ฐ€ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ• ๋•Œ๋งˆ๋‹ค ๊ฐ’์ด ๋ฐ”๋€๋‹ค

local : method๊ฐ€ ์‹คํ–‰ํ• ๋•Œ๋งˆ๋‹ค ๊ฐ’์ด ์ƒ๊ธฐ๊ณ  ์—†์–ด์ง„๋‹ค!!

 

 

 

# private๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ getter, setter ๋ฉ”์„œ๋“œ๋ฅผ ์šฐํšŒ์ ์œผ๋กœ ์‚ฌ์šฉํ–ˆ์„๋•Œ..

 

package thisisjava;

class Book16{
	// ํ•„๋“œ๋ช…์ด ๊ทธ๋Œ€๋กœ ๋…ธ์ถœ๋˜๋Š”๊ฒƒ์ด ์ข‹์ง€ ์•Š์•„์„œ private๋กœ ์ฒ˜๋ฆฌ๋ฅผ ๋งŽ์ด ํ•˜๋Š”๋ฐ
	// ๊ทธ๋•Œ getter, setter ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ฆ์œผ๋กœ์จ ์šฐํšŒ์ ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“œ๋Š”๊ฒƒ์ด ์ข‹๋‹ค
	private String title = "๋ฌด์ œ"; 
	private String author = "๋ฏธ์ƒ";
	
	public String getTitle() { return this.title; }
	public void setTitle(String title) { this.title = title; }
}

public class Cla16 {
	public static void main(String[] args) {
		Book16 b1 = new Book16();
		b1.setTitle("Java"); 
		System.out.println(b1.getTitle());
		
	}
}

 

 

# Class์˜ ํ™œ์šฉ

 

package thisisjava;

class Point17 {
    int x, y;
    
    Point17() { }; 
    
    Point17(int i, int j) {
        this.x = i;
        this.y = j;
        System.out.println(this.x + ", " + this.y);
    }
    
    void Sum(Point17 p1, Point17 p2) {
        this.x = p1.x + p2.x;
        this.y = p1.y + p2.y;
        System.out.println(this.x + ", " + this.y);
    }
    
    // ๊ฐ•์‚ฌ๋‹˜ ์ฝ”๋“œ
    static Point17 pSum(Point17 i, Point17 j) {
    	int a = i.x + j.x;
    	int b = i.y + j.y;
    	return new Point17(a, b);
    }
    
    // ์ง์‚ฌ๊ฐํ˜• ๋„“์ด ๊ตฌํ•˜๊ธฐ
    static int pRect(Point17 i, Point17 j) {
//    	int height;
//    	int width;
//    	if(i.x >= j.x) {
//    		width = i.x - j.x;
//    	} else width = j.x - i.x;
//    	
//    	if(j.y >= i.y) {
//    		height = j.y - i.y;
//    	} else height = j.y - i.y;
    	int height = Math.abs(i.x - j.x);
    	int width = Math.abs(i.y - j.y);
    	int res = height * width;
    	return res;
    	
    }
}

public class Cla17 {
    public static void main(String[] args) {
        Point17 p1 = new Point17(20, 10); 
        Point17 p2 = new Point17(35, 25); 
        Point17 p3 = new Point17();
        p3.Sum(p1, p2);      // 55, 35
        
        // ๊ฐ•์‚ฌ๋‹˜ ์ฝ”๋“œ
//        Point17 p4 = Point17.pSum(p1, p2);
        System.out.println(p3.pRect(p1, p2));
    }
}

 

 

# ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด

  • ์ƒ์„ฑ์ž๋ฅผ private ์ ‘๊ทผ ์ œํ•œํ•ด์„œ ์™ธ๋ถ€์—์„œ new ์—ฐ์‚ฐ์ž๋กœ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋„๋ก ๋ง‰์•„์„œ ์™ธ๋ถ€์—์„œ ๋งˆ์Œ๋Œ€๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•จ
  • ๋Œ€์‹  ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์ด ์ œ๊ณตํ•˜๋Š” ์ •์  ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ„์ ‘์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Œ
  • ์‹ฑ๊ธ€ํ†ค : ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋”ฑ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•˜๋„๋ก ๋ณด
package thisisjava;

class Sing18 {
	// ๋”ฑ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ์• ๋“คํ•œํ…Œ ๊ฐ•์ œ๋กœ ํ•˜๊ฒŒ ํ•œ๋‹ค!! 
    // Sing18 ํด๋ž˜์Šค๋Š” ์ž๊ธฐ ์ž์‹ ์˜ ๊ฐ์ฒด๋ฅผ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ๋‹จ ํ•œ ๋ฒˆ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
	private static Sing18 sing = new Sing18();
    // ์ƒ์„ฑ์ž๋ฅผ private์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋„๋ก ์ œํ•œํ•ฉ๋‹ˆ๋‹ค.
	private Sing18() {}
	
	static Sing18 getInstnace() {
		return sing;
	}
}

public class Cla18 {
	public static void main(String[] args) {
		Sing18 s1 = Sing18.getInstnace();
		Sing18 s2 = Sing18.getInstnace();
		
		System.out.println(s1 == s2);
	}
}

 

 

# ์ƒ์† (& ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ)

 

package thisisjava;

class Ani20 {
	String name = "์ด๋ฆ„";
	void cry() { System.out.println("์œผ์•™"); }	
}

class Dog20 extends Ani20 {
	String name = "ํ•ดํ”ผ";
	@Override
	void cry() { System.out.println("๋ฉ๋ฉ"); } // Method Overriding
}

class Cat20 extends Ani20 {
	String name = "์•„์น˜";
	@Override
	void cry() { System.out.println("์•ผ์˜น"); 	}
}

class Duck20 extends Ani20 { 
	String name = "๋„ํŠธ";
	@Override
	void cry() { System.out.println("๊ฝค๊ฝฅ"); 	}
}

public class Cla20 {
	public static void main(String args[]) {
		Dog20 d1 = new Dog20();
		System.out.print(d1.name); d1.cry();
		
		Cat20 c1 = new Cat20();
		System.out.print(c1.name); c1.cry();
		
		Duck20 d2 = new Duck20();
		System.out.print(d2.name); d2.cry();
	}
}

 

 

# ์ƒ์† ( & final & Override)

package thisisjava;

class Vehicle22{
	static final int a = 1;
	void start() {System.out.println("์ถœ๋ฐœ");}
	void move() {System.out.println("์ด๋™");}
	void stop()  {System.out.println("๋ฉˆ์ถค");}
}
class Car22 extends Vehicle22{
	// final์ด ๋ถ™์€ ๊ฒฝ์šฐ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋ถˆ๊ฐ€๋Šฅ 
//	@Override
//	int a = 2;
	
	// final + ๋ณ€์ˆ˜ -> ์ƒ์ˆ˜๊ฐ™์€ ๋Š๋‚Œ
	static final int Normal = 1;
	static final int Accel = 2;
	static final int Slow = 3;
	
	int moveMode = Normal;
	
	void move() {
		if (moveMode == Normal) { super.move(); }
		else if (moveMode == Accel) 
			{System.out.println("๋น ๋ฅธ ์ด๋™");}
		else {System.out.println("์ฒœ์ฒœํžˆ ์ด๋™");}
	}
}
public class Cla22 {
	public static void main(String[] args) {
		Car22 c1 = new Car22();
		c1.start();
		c1.move();
		c1.moveMode = Car22.Accel;
		c1.move();
		c1.moveMode = Car22.Slow;
		c1.move();
		c1.stop();
	}

}

 

 

# ์ฐธ์กฐํ˜• ํ˜•๋ณ€ํ™˜

 

- ๋ถ€๋ชจ ๋ณด๋‹ค ์ž์‹์— ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค!!

- ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์—†์–ด์„œ ๋งˆ์ง€๋ง‰ 4๋ฒˆ์€ ํ‹€๋ฆฐ๊ฑฐ์ž„

package thisisjava;

class A25 { int a = 200; }
class B25 extends A25{ int b = 100; }


public class Cla25 {
	public static void main(String[] args) {
		// ์ฐธ์กฐํ˜• ํ˜•๋ณ€ํ™˜
		A25 z1 = new A25();
		B25 z2 = new B25();
		A25 z3 = new B25();
		// B25 z4 = new A25(); ๋ฌธ๋ฒ•์ ์ธ ์—๋Ÿฌ๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ์ ์ธ ์—๋Ÿฌ
		
		short s1 = 10;
		int i1 = s1;
		int i2 = 20;
		short s2 = (short) i2;
	}
}

 

 


(๋ชฉ์š”์ผ)

 

# ์ƒ์†

 

package thisisjava;

class A26 {}
class B26 extends A26 {}
class C26 extends A26 {}
class D26 extends B26 {}
class E26 extends C26 {}

public class Cla26 {

	public static void main(String[] args) {
		B26 b = new B26();
		C26 c = new C26();
		D26 d = new D26();
		E26 e = new E26();
		
		A26 a1 = b; A26 a2 = c;
		A26 a3 = d; A26 a4 = e;
		
		B26 b1 = d; C26 c1 = e;
//		B26 b3 = e; C26 c3 = d;  -> ์ง๊ณ„๊ฐ€ ์•„๋‹ˆ๋ฉด ํ˜•๋ณ€ํ™˜ ๋ถˆ๊ฐ€
		
		System.out.println(d instanceof B26);
		System.out.println(d instanceof A26);
		System.out.println(b instanceof D26); // false 
		
		System.out.println(e instanceof C26);
		System.out.println(e instanceof A26);
	}

}

 

 

# ๋‹ค์ค‘ ์ƒ์†(์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ€๋Šฅ) 

 

- ๋ถˆ๊ฐ€๋Šฅํ•œ ์ด์œ  : ํ•„๋“œ๋ž‘ ๋ฉ”์„œ๋“œ๋ฅผ ์•„๋ฌด๋ฆฌ ์•ˆ ๋งŒ๋“ ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ทผ๋ณธ์ ์œผ๋กœ object์— ์ƒ์†์„ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์—†๋‹ค๊ณ  ๋Š๊ปด๋„ ์‹ค์งˆ์ ์œผ๋กœ equals()๋‚˜ toString()๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์ค‘ ์ƒ์†์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค 

 

- Java๋Š” ํด๋ž˜์Šค ๊ฐ„์˜ ๋‹ค์ค‘ ์ƒ์†์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹ค์ค‘ ์ƒ์†์ด ํ—ˆ์šฉ๋  ๊ฒฝ์šฐ, ๋‘ ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๋‚˜ ํ•„๋“œ๊ฐ€ ์ •์˜๋˜์—ˆ์„ ๋•Œ ์–ด๋–ค ๊ฒƒ์„ ์‚ฌ์šฉํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๊ณผ์ •์—์„œ ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค(๋‹ค์ด์•„๋ชฌ๋“œ ๋ฌธ์ œ). ๋˜ํ•œ, Java์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Object๋ฅผ ์ƒ์†๋ฐ›๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์ค‘ ์ƒ์†์ด ํ—ˆ์šฉ๋˜๋ฉด ์ƒ์† ๊ด€๊ณ„๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋ณต์žกํ•ด์ง€๊ณ  ์ถฉ๋Œ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ Java๋Š” ํด๋ž˜์Šค ๊ฐ„์˜ ๋‹ค์ค‘ ์ƒ์†์„ ๊ธˆ์ง€ํ•˜๊ณ , ๋Œ€์‹  ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ๋‹ค์ค‘ ๊ตฌํ˜„์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

 

- interface๋Š” ๊ฐ€๋Šฅํ•œ ์ด์œ  : ์ƒํƒœ(ํ•„๋“œ)๊ฐ€ ์—†๊ณ  ๋™์ž‘(ํ–‰์œ„)๋งŒ ์ •์˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์ค‘ ๊ตฌํ˜„์ด ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.

 

package thisisjava;


//class A27{}
//class B27{}
//class C27 extends A27,B27 { }

interface A27{ }
interface B27{ }
class C27 implements A27, B27{ }

public class Cla27 {

	public static void main(String[] args) {
		System.out.println("์ž˜ ์‹คํ–‰๋จ");
	}
}

๋ณธ์งˆ์ ์œผ๋กœ ๊ฐ™์€๊ฑธ ์ƒ์†๋ฐ›์œผ๋ฉด extends ๋‹ค๋ฅด๋ฉด implements abstract class -> interface(implements)์ด๋‹ค!!

 

 

 

# ์ ‘๊ทผ์ œํ•œ์ž

(public / protected / (default) / private) 

package thisisjava;

class A28 {
	
	int aa1 = 10;				// default (๊ฐ™์€ ํŒจํ‚ค์ง€) / ์ˆจ๊ฒจ์ ธ ์žˆ๋‹ค / ๊ณต๊ฐœ ๋ฒ”์œ„ 3๋“ฑ
	public int aa2 = 100; 		// public (ํ•ญ์ƒ ๊ณต๊ฐœ) / ๊ณต๊ฐœ ๋ฒ”์œ„ 1๋“ฑ
	private int aa3 = 1000;		// private(์ž๊ธฐ ํด๋ž˜์Šค) / ๊ณต๊ฐœ ๋ฒ”์œ„ ๊ผด๋“ฑ
	protected int aa4 = 10000;	// protected (์ƒ์†๊ด€๊ณ„ ๋‚ด + default) / ๊ณต๊ฐœ ๋ฒ”์œ„ 2๋“ฑ
}

class B28 extends A28 {
	int bb1 = 20;
}

public class Cla28 {
	public static void main(String[] args) {
		A28 a = new A28(); 
		B28 b = new B28(); 
	}
}

 

 

# ์ƒ์†์—์„œ์˜ ์ ‘๊ทผ ๋ฒ”์œ„

 

- ๋ถ€๋ชจ์—์„œ ์ƒ์†๋ฐ›์€ ๋ฉค๋ฒ„๋Š” ์ ‘๊ทผ ๋ฒ”์œ„๊ฐ€ ์ž‘์•„์งˆ ์ˆ˜ ์—†๋‹ค

- ์˜ค๋ฒ„๋ผ์ด๋”ฉ๋œ ๋ฉ”์„œ๋“œ๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ **๋Œ€์ฒด(replace)**ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ์ž์‹ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•  ๋•Œ๋„, ์˜ค๋ฒ„๋ผ์ด๋”ฉ๋œ ๋ฉ”์„œ๋“œ์— ๋™์ผํ•œ ์ ‘๊ทผ ์ˆ˜์ค€์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

package thisisjava;

class A29 {
	void meth01() {}
}

class B29 extends A29 {
	// ๋ถ€๋ชจํ•œํ…Œ์„œ ์ƒ์†๋ฐ›์€ ๋ฉค๋ฒ„๋Š” ์ ‘๊ทผ๋ฒ”์œ„๊ฐ€ ์ž‘์•„์งˆ ์ˆ˜ ์—†๋‹ค
//	@Override
//	private void meth01() {}
}

public class Cla29 {

	public static void main(String[] args) {

	}

}

 

 

#  ์ถ”์ƒ ํด๋ž˜์Šค์˜ ํ™œ์šฉ

 

- ์ƒ์† ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ๋‹คํ˜•์„ฑ์„ ์ด์šฉํ•˜๊ณ ์ž ํ• ๋•Œ

- ๋ถ€๋ชจ๋Š” ๊ตฌํ˜„๋ถ€๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ 

- abstract๋ฅผ ๋ถ™์—ฌ์„œ ์ถ”์ƒํด๋ž˜์Šค๋กœ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค

 

package ch07.sec08.exam02;

public abstract class Vehicle {
	//๋ฉ”์†Œ๋“œ ์„ ์–ธ
	public abstract void run(); 
}

 

 

 

# main ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค์—์„œ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ static์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋˜๋Š” ์ด์œ 

 

1. static ๋ฉ”์„œ๋“œ์™€ non-static ๋ฉ”์„œ๋“œ ํ˜ผ์šฉ์˜ ๋ฌธ์ œ

  • main() ๋ฉ”์„œ๋“œ๋Š” static ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. main() ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ static ๋ฉ”์„œ๋“œ์™€ non-static ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ผ์šฉ๋˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, test02()๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๋ฐ˜๋ฉด, test01()์€ ๊ฐ์ฒด ์—†์ด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ฝ์„ ๋•Œ ์ด๋Ÿฌํ•œ ํ˜ผ์šฉ์€ ํ˜ผ๋ž€์„ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. ๊ฐ์ฒด ์ƒ์„ฑ ์˜ค๋ฒ„ํ—ค๋“œ

  • non-static ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๋ถˆํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๊ณผ ์ฒ˜๋ฆฌ ๋น„์šฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ๋งŒ์•ฝ ๋ฉ”์„œ๋“œ๊ฐ€ ์ธ์Šคํ„ด์Šค ์ƒํƒœ๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, static์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. ์ฝ”๋“œ์˜ ์ผ๊ด€์„ฑ ์œ ์ง€

  • main() ๋ฉ”์„œ๋“œ๊ฐ€ ์†ํ•œ ํด๋ž˜์Šค์—์„œ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ static์œผ๋กœ ์„ ์–ธํ•˜๋ฉด, ๊ฐ์ฒด ์ƒ์„ฑ ์—†์ด ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋ชจ๋“  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ผ๊ด€์„ฑ ์žˆ๋Š” ์ฝ”๋“œ ์Šคํƒ€์ผ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. ์œ ์ง€๋ณด์ˆ˜์„ฑ

  • ์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ์˜ ํ๋ฆ„์„ ์˜ˆ์ธกํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.
  • ๋น„์ •์  ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ผ์žฌ๋˜๋ฉด ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ๊ด€๋ฆฌ๋ฅผ ๋”ฐ๋กœ ์ถ”์ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
package thisisjava;

class Cla31 {
	public static void main(String[] args) {
		System.out.println("Cla30-main() ์‹คํ–‰");		
	}
	
	static void test01() {
		System.out.println("Cla31-test01() ์‹คํ–‰");
	}
	
	void test02() {
		System.out.println("Cla31-test02() ์‹คํ–‰");
	}
}

public class Cla30 {

	public static void main(String[] args) {
		System.out.println("Cla30-main() ์‹คํ–‰");
		Cla31.test01();
		Cla30.test01();
		test01();
		
		Cla31 c31 = new Cla31();
		c31.test02();
		
		// ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š” ์ด์œ  : main ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ๊ณณ์—์„œ๋Š” main ๋ฉ”์„œ๋“œ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ ๋˜ํ•œ static์„ ๋ถ™์—ฌ์ฃผ๋Š”๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ๊ถŒ์žฅ (์•ˆ๋˜๋Š”๊ฑด ์•„๋‹˜)
		Cla30 c30 = new Cla30();
		c30.test02();
		
	}
	static void test01() { System.out.println("Cla30-test01() ์‹คํ–‰"); }
	void test02() {
		System.out.println("Cla31-test02() ์‹คํ–‰");
	}
}

 

 

# ๋ด‰์ธ๋œ ํด๋ž˜์Šค

 

- sealed ~ permits : ์ƒ์†์„ ํ—ˆ๋ฝํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค

- nonsealed : ๋ˆ„๊ฐ€ ์ƒ์†ํ•˜๋“  ์ƒ๊ด€ x

- final : ์ƒ์†ํ•˜์ง€ x

package ch07.sec11;

// Person ํด๋ž˜์Šค๋Š” Employee์™€ Manager์—๊ฒŒ๋งŒ ์ƒ์†์„ ํ—ˆ๋ฝํ•œ๋‹ค
public sealed class Person permits Employee, Manager {
	//ํ•„๋“œ
	public String name;
	
	//๋ฉ”์†Œ๋“œ
	public void work() {
		System.out.println("ํ•˜๋Š” ์ผ์ด ๊ฒฐ์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.");
	}
}


// non-sealed๋Š” ๋ˆ„๊ฐ€ ์ƒ์†ํ•˜๋“ ์ง€ ์ƒ๊ด€ ์—†๋‹ค!
public non-sealed class Manager extends Person {
	@Override
	public void work() {
		System.out.println("์ƒ์‚ฐ ๊ด€๋ฆฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.");
	}
}


// final์€ Employee์ดํ›„ ํด๋ž˜์Šจ ๋” ์ด์ƒ ์ƒ์† ํ•ด์ฃผ์ง€ ์•Š์Œ
public final class Employee extends Person {
	@Override
	public void work() {
		System.out.println("์ œํ’ˆ์„ ์ƒ์‚ฐํ•ฉ๋‹ˆ๋‹ค.");
	}
}

 

 

# interface ์‚ฌ์šฉ!!

 

- ๊ตฌํ˜„์€ ํ•„์š”์—†๊ณ  ์ •์˜๋งŒ ํ•˜๋Š” ๊ฒฝ์šฐ interface ์‚ฌ์šฉ 

- class๊ฐ€ interface๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” extends -> implements๋กœ ๋ณ€๊ฒฝ

- ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ public abstract์ด๋ฏ€๋กœ interface์—์„œ ์ƒ์†๋ฐ›์•„์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ ‘๊ทผ์ œํ•œ์ž๋ฅผ public ๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฐ ๋ฒ”์œ„์ธ public ์„ค์ • ํ•„์ˆ˜

 

package thisisjava;

// ๊ตณ์ด ์“ฐ์ง€๋„ ์•Š์€ ๊ตฌํ˜„์ฒด๊ฐ€ ์žˆ์„๋•Œ interface๋ฅผ ์‚ฌ์šฉ!!
interface TV{
	void PowerOn();
	void PowerOff();
}

class LGTV implements TV{
	// ๋ถ€๋ชจ ๋ฉค๋ฒ„์˜ ์ ‘๊ทผ ์ œํ•œ์ž๋ณด๋‹ค ๋ฒ”์œ„๊ฐ€ ์ปค์•ผํ•ด์„œ public์ด ํ•„์ˆ˜์ž„
	public void PowerOn() {
		System.out.println("LGTV ์ผœ์ง");
	}
	public void PowerOff() {
		System.out.println("LGTV ๊บผ์ง");
	}
}

class SSTV implements TV{
	public void PowerOn() {
		System.out.println("SSTV ์ผœ์ง");
	}
	public void PowerOff() {
		System.out.println("SSTV ๊บผ์ง");
	}
}

class KTTV implements TV{
	public void PowerOn() {
		System.out.println("KTTV ์ผœ์ง");
	}
	public void PowerOff() {
		System.out.println("KTTV ๊บผ์ง");
	}
}

public class Cla32 {
	public static void main(String[] args) {
		TV[] tvs = { new LGTV(), new SSTV(), new KTTV() };
		for(TV tv : tvs) {
			tv.PowerOn();
			tv.PowerOff();
		}
	}
}
728x90
๋ฐ˜์‘ํ˜•