java ์ ๋ฌธ๋ฐ 4์ฃผ์ฐจ
(์์์ผ)
# 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("์ ์คํ๋จ");
}
}
# ์ ๊ทผ์ ํ์
(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();
}
}
}