**Employee(์ฌ์) ํด๋์ค๋ฅผ ์ ์ํ์ฌ ์ฌ์๊ด๋ฆฌ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ ค๊ณ ํ๋ค.
๊ด๋ฆฌ๋์ ์์ฑ : ์ฌ์๋ฒํธ(empno), ์ด๋ฆ(ename), ๋ถ์๋ฒํธ(deptno), ๊ธ์ฌ(salary)
5๋ช ์ ์ฌ์์ ๋ณด๋ฅผ ์์ฑํ์ฌ ํ๋ฉด์ ์ฌ์์ ๋ณด ๋ชฉ๋ก์ ํ์ํด๋ณด์ธ์
(์์ฑ์, ๊ฐ์ฒด ๋ฐฐ์ด, for ๋ฐ๋ณต๋ฌธ ์ฌ์ฉ)
Main Class ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.ezen.javatest;
import com.ezen.javaoop.Employee;
public class Main
{
public static void main(String[] args)
{
Employee[] emplist = new Employee[5];
emplist[0] = new Employee(12,"Smith",45, 45000000);
emplist[1] = new Employee(13,"Jay",45, 55000000);
emplist[2] = new Employee(14,"Paul",56, 2300000);
emplist[3] = new Employee(15,"Jun",23, 6230000);
emplist[4] = new Employee(16,"Bob",11, 21300000);
System.out.println("์ฌ์๋ฒํธ\t"+"์ด๋ฆ\t"+"๋ถ์๋ฒํธ\t"+"๊ธ์ฌ");
for(int i =0;i<emplist.length;i++) {
emplist[i].Employeelist();
}
}
}
|
cs |
Employee Class ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.ezen.javaoop;
public class Employee
{
public int empno;
public String ename;
public int deptno;
public int salary;
public Employee() {}
public Employee(int empno, String ename, int deptno, int salary) {
this.empno=empno;
this.ename=ename;
this.deptno=deptno;
this.salary=salary;
}
public void Employeelist() {
String info = String.format("%d\t%s\t%d\t%d",
empno,ename,deptno,salary);
System.out.println(info);
}
}
|
cs |
gett, sett ๋ฉ์๋
pravite ๋ณ์๋ ๋ค๋ฅธ ํจํค์ง์์ ๊ฐ์ ธ์ค๊ฑฐ๋ ์ ํด์ค ์ ์๋๋ฐ, [Source] - [Generate Getter and Setter]๋ฅผ ์ฌ์ฉํ๋ฉด pravite ๋ณ์๋ฅผ ์ ์ธํ์ง ์์ ๋ค๋ฅธ ํจํค์ง Class์์ ์ค์ ์ ํด์ฃผ๊ฑฐ๋ ๋ถ๋ฌ์ฌ ์ ์๋ค.
์ง์ ์ ๊ทผํ์ฌ ๋ณ์๊ฐ์ ์ ์ธ ํ์ง ์๊ณ sett ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ ์ ๊ทผ ํด์ ์ ์ธํ๋ ์ด์ ๋ ์ง์ ์ ๊ทผ ๊ฐ์ด ์ ์ธํด ๋ฒ๋ฆฌ๋ฉด ์์ ์ด ๋ถ๊ฐํด์ง์ง๋ง sett ๋ฉ์๋๋ ํ๋์ ๊ธฐ๋ฅ์ด๊ธฐ๋๋ฌธ์ ๋ฉ์๋ ์์์ ์๋ชป๋ ๊ฐ์ด ๋ค์ด์ฌ๋ ๋์ฒํ ๊ธฐ๋ฅ์ด๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.
์์ ๊ฐ์ด ์ฌ๋ด ๋ถ์ ๋ฒํธ๊ฐ 10 ๋ฏธ๋ง์ ์๋ ์๋๋ฐ ์ฌ์ฉ์๊ฐ ์ค์๋ก ์๋ชป๋ ์๋ฅผ ์ ๋ ฅํ๊ฒ ๋๋ฉด ์ฌ์ ์ ์๋ ค์ค ์ ์๋ ๊ธฐ๋ฅ์ ๋ฃ์ ์ ์๋ค.
๊ฐ์ฒด(Object): ๊ณ ์ ํ ๊ธฐ๋ฅ๊ณผ ์์ฑ์ ๊ฐ์ง ๋ชจ๋ ์ฌ๋ฌผ
์ธ์คํด์ค(Instance): ์ฌ์ ์ ์๋ฏธ๋ก๋ ์ค์ฒด์ด๋ค
๋ฉ๋ชจ๋ฆฌ์ ์์๋๋ฐ ์๊ธฐ๋ ๊ฒ-( new์ฐ์ฐ์๋ฅผ ์จ์ ๋ฉ๋ชจ๋ฆฌ์ ์์๋๋ฐ class๊ฐ ๋ก๋๋์ด ์๊ธด๊ฒ)
static ๋ณ์๋ ํ๋ก๊ทธ๋จ ์์ํ ๋ ๋ฉ๋ชจ๋ฆฌ์ ๋ณ์ 1๊ฐ๋ง์ด ๋ก๋๋๋ค. ์์ : ํ์ฌ๋ช ๊ฐ์ ๊ฒ ๊ทธ๋์ ๋ฐ๋ก ์ด๊ธฐํ๊น์ง ํด์ค๋ค
static String company = "์ ๋ฆผ ์ฃผ์ํ์ฌ";
this(); ->์์ฑ์ ์์์ ์์ฑ์๋ฅผ ํธ์ถํ ๋ ์ด๋ค.
extends Object;
super();
์ด ๋๊ฐ๋ ์ฐ๋ฆฌ๊ฐ ์์ฑํ์ง ์์๋ ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก ์์ฑํด์ ๋๋ ค์ค๋ค.
**์์ฑ์์ ๊ตฌ๊ตฌ๋จ์๋ฅผ ์ ๋ฌํ๊ณ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๊ทธ ๊ฐ์ฒด์ ํฌํจ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ํด๋น ๊ตฌ๊ตฌ๋จ์ด ํ๋ฉด์ ํ์ํ๋๋ก ํ์์ค
Main Class ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
|
import com.ezen.javaoop.Gugudan;
public class Main
{
public static void main(String[] args)
{
Gugudan g = new Gugudan(3);
g.printGugudan();
}
}
|
cs |
g.printGugudan(); ๋ผ๋ ๊ฑด
g์ ์๋ printGugudan() ๋ฉ์๋๋ฅผ ํธ์ถ ํด๋ผ
Gugudan Class ์ฝ๋
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
|
public class Gugudan
{
public int dan;
public Gugudan() {}
public Gugudan(int dan) {
this.setDan(dan); //this ์๋ต ๊ฐ๋ฅ (๊ฐ์ ํด๋์ค์ด๊ธฐ ๋๋ฌธ)
}
public void setDan(int dan) {
if(dan>=2 && dan<=9) {
this.dan = dan;
}
else {
System.err.println("2~9์ฌ์ด ์ ์๋ง ๊ฐ๋ฅํฉ๋๋ค.");
}
}
public void printGugudan(){
String line = null;
if(dan>=2 && dan<=9) {
for(int i=1;i<10;i++) {
line=String.format("%d * %d = %d", dan,i,dan*i);
System.out.println(line);
}
}
}
}
|
cs |
public Gugudan() {}
public Gugudan(int dan) {}
์ด๋ ๊ฒ ์์ฑ์๋ฅผ ์ฌ๋ฌ๊ฐ๋ฅผ ๋ง๋๋ ๊ฑธ ์์ฑ์ ์ค๋ฒ๋ก๋๋ผ๊ณ ํ๋ค.
์ฝ์ ๊ฐ:
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
**์ฅ๋ฐ๊ตฌ๋ ๋ง๋ค๊ธฐ
์ผํ๋ชฐ์์ ๋ค์ํ ์ข ๋ฅ์ ๋ฌผ๊ฑด์ ๊ตฌ๋งคํ์ฌ, ๊ตฌ๋งค๋ ๋ชจ๋ ๋ฌผ๊ฑด์ ๋ชฉ๋ก์ ํ์ํ๊ณ ์ด ๊ตฌ๋งค์ก๋ ํ์ํ๋ ค๊ณ ํ๋ค.
Item (name, price, producer)
Main Class ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import com.ezen.javaoop.Item;
public class Main
{
public static void main(String[] args)
{
Item[] tem = new Item[5];
tem[0] = new Item ("๋ฉด๋๊ธฐ", 30000, "๋ธ๋ผ์ด");
tem[1] = new Item ("๋น๊ทผ", 1500, "๊น์จ๋ค ํ
๋ฐญ");
tem[2] = new Item ("์ ๋ผ๋ฉด", 1050, "๋์ฌ");
tem[3] = new Item ("๋ปฅํ๊ธฐ", 2000, "๋ด์");
tem[4] = new Item ("ํฐ์
์ธ ", 20000, "BYC");
System.out.printf("%s\t%s\t%s\n","์ด๋ฆ","๊ฐ๊ฒฉ(์)","์ ์กฐ์ฌ");
int sum = 0;
for(int i =0;i<tem.length;i++) {
tem[i].printItem();
sum += tem[i].printPrice();
}
System.out.print("์ด ๊ตฌ์
๊ธ์ก:");
System.out.print(sum+"์");
}
}
|
cs |
Employee Class ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class Item
{
private String name; //๋ณ์ ์ ์ธ
private int price;
private String producer;
public Item() {}
public Item(String name, int price, String producer) {
this.name=name; //๋ณ์ ์ด๊ธฐํ
this.price=price;
this.producer=producer;
}
public void printItem() {
System.out.printf("%s\t%d\t%s\t \n",name, price, producer);
}
public int printPrice() {
return price;
}
}
|
cs |
์ฝ์ ๊ฐ:
๋๊ธ