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
|
class Triangle
{
int width;
int height;
int area;
public void initTriangle(int w, int h)
{
width = w;
height = h;
}
public void calArea()
{
area = width * height / 2;
}
public void printCS()
{
System.out.println("밑변 " + width);
System.out.println("높이 " + height);
System.out.println("넓이 " + area);
}
}
class TriangleMain
{
public static void main(String[] args)
{
Triangle t1 = new Triangle();
t1.initTriangle(10, 5);
t1.calArea();
t1.printCS();
}
}
|
cs |
매개변수와 지역변수 이름이 같으면 지역변수가 없어진다.
this.width = width;
this.height = height;
this: 자기 자신, 멤버변수
<실습 문제>
과일장수 클래스
인스턴스 변수
사과값
사과개수
가지고 있는 돈
메소드
인스턴스 변수를 초기화 한다.
구매자에게 돈을 받으면 사과를 건네준다.
현재 자신의 상태를 출력.
구매자 클래스
인스턴스 변수
사과개수
가지고 있는 돈
메소드
인스턴스 변수를 초기화 한다.
과일장수에게 돈을 주고 사과를 받는다.
현재 자신의 상태를 출력.
과일메인 클래스
사과장수 두명을 만든다.
한명은 사과하나의 값은 2000 원이고 사과를 50 개 들고 있고 그리고 가진돈은 50000원이다.
또 다른 사과장수는 사과하나의 값은 1000원이고 사과를 100개를 들고 있고 그리고 가진돈은 100000원이다.
구매자는 20000원의 돈을 가지고 있으며 처음에는 사과가 하나도 없다.
2000원짜리 사과 3개와 1000원짜리 사과 5개를 구매한다.
우선은 정확히 자신이 사고싶은 만큼의 돈을 넘겨주고 거스름돈은 없는 걸로 하자.
사과 구매후 3명(과일장수, 구매자)의 상태를 출력한다.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
class Seller
{
int PRICE; //상수는 대문자
int numOfApples;
int money;
public void initSeller(int price, int numOfApples, int money)
{
this.PRICE = price;
this.numOfApples = numOfApples;
this.money = money;
}
public int sell(int money)
{
int num = 0;
this.money += money;
num = money / PRICE;
numOfApples -= num;
return num;
}
public void printCS()
{
System.out.println("사과 가격 " + PRICE);
System.out.println("사과 개수 " + numOfApples);
System.out.println("돈 " + money);
}
}
class Buyer
{
int numOfApples;
int money;
public void initBuyer(int money)
{
this.numOfApples = 0;
this.money = money;
}
public void printCS()
{
System.out.println("사과 개수 " + numOfApples);
System.out.println("돈 " + money);
}
public void buy(Seller seller, int money)
{
this.money -= money;
numOfApples += seller.sell(money);
}
}
class FruitMain
{
public static void main(String[] args)
{
Seller seller1 = new Seller();
seller1.initSeller(2000, 50, 50000);
Seller seller2 = new Seller();
seller2.initSeller(1000, 100, 100000);
Buyer buyer = new Buyer();
buyer.initBuyer(20000);
buyer.buy(seller1, 6000);
seller1.printCS();
seller2.printCS();
buyer.printCS();
}
}
|
cs |
생성자
1. 생성자의 이름과 클래스의 이름은 대소문자하나까지 완벽하게 같아야 한다.
2. 반환하는 값이 있어서도 안되고, 정의해서도 안된다.
3. 생성자에서는 상수를 초기화 할 수 있다.(메소드에서는 상수를 초기화 할 수 없다.)
4. 모든 클래스는 반드시 생성자를 갖고 있어야 한다.
생성자를 만들지 않으면 디폴트 생성자가 자동으로 삽입된다. 생성자를 만들면 디폴트 생성자가 삽입이 안된다.
생성자 예시
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
class FruitSeller
{
final int PRICE;
int numOfApples;
int money;
public FruitSeller(int PRICE, int numOfApples, int money)
{
this.PRICE = PRICE; // 상수는 메소드 내에서 초기화 할 수 없다.
this.numOfApples = numOfApples;
this.money = money;
}
public int sell(int money)
{
this.money += money;
int num = money / PRICE;
this.numOfApples -= num;
return num;
}
public void printCS()
{
System.out.println("사과가격 " + PRICE);
System.out.println("사과개수 " + numOfApples);
System.out.println("돈 " + money);
}
}
class Buyer
{
int numOfApples;
int money;
public Buyer(int money)
{
this.numOfApples = 0;
this.money = money;
}
public void buy(FruitSeller seller, int money)
{
this.money -= money;
this.numOfApples += seller.sell(money);
}
public void printCS()
{
System.out.println("사과개수 " + numOfApples);
System.out.println("돈 " + money);
}
}
class FruitMain
{
public static void main(String[] args)
{
FruitSeller seller1 = new FruitSeller(2000, 50, 50000);
FruitSeller seller2 = new FruitSeller(1000, 100, 100000);
Buyer buyer = new Buyer(20000);
buyer.buy(seller1, 6000);
seller1.printCS();
seller2.printCS();
buyer.printCS();
System.out.println("===================");
buyer.buy(seller2, 5000);
seller1.printCS();
seller2.printCS();
buyer.printCS();
}
}
|
cs |
생성자를 이용하는 형태로 변경하기
1.
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
32
33
34
35
36
37
38
39
40
41
42
|
class Triangle
{
int width;
int height;
int area;
public void initTriangle(int width, int height)
{
this.width = width;
this.height = height;
}
public void areaOfTriangle()
{
area = width * height / 2;
}
public void printCS()
{
System.out.println("밑변 : " + width);
System.out.println("높이 : " + height);
System.out.println("넓이 : " + area);
}
}
class TriangleMain
{
public static void main(String[] args)
{
Triangle t1 = new Triangle();
t1.initTriangle(10, 5);
t1.areaOfTriangle();
t1.printCS();
Triangle t2 = new Triangle();
t2.initTriangle(4, 2);
t2.areaOfTriangle();
t2.printCS();
Triangle t3 = new Triangle();
t3.initTriangle(20, 10);
t3.areaOfTriangle();
t3.printCS();
}
}
|
cs |
2.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
class Circle
{
double radius;
double area;
double perimeter;
double pi;
public void initCircle(double radius)
{
this.radius = radius;
pi = 3.14;
perimeterOfCircle();
areaOfCircle();
printCS();
}
public void perimeterOfCircle()
{
perimeter = 2 * pi * radius;
}
public void areaOfCircle()
{
area = pi * radius * radius;
}
public void printCS()
{
System.out.println("반지름 " + radius);
System.out.println("둘레 " + perimeter);
System.out.println("면적 " + area);
}
}
class CircleMain
{
public static void main(String[] args)
{
/*
Circle c1 = new Circle();
c1.initCircle(5);
c1.perimeterOfCircle();
c1.areaOfCircle();
c1.printCS();
*/
Circle c1 = new Circle();
c1.initCircle(5);
Circle c2 = new Circle();
c2.initCircle(10);
Circle c3 = new Circle();
c3.initCircle(30);
}
}
|
cs |
3.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
class FruitSeller
{
int PRICE;
int numOfApples;
int money;
public void initFruitSeller(int PRICE, int numOfApples, int money)
{
this.PRICE = PRICE; // 상수는 메소드 내에서 초기화 할 수 없다.
this.numOfApples = numOfApples;
this.money = money;
}
public int sell(int money)
{
this.money += money;
int num = money / PRICE;
this.numOfApples -= num;
return num;
}
public void printCS()
{
System.out.println("사과가격 " + PRICE);
System.out.println("사과개수 " + numOfApples);
System.out.println("돈 " + money);
}
}
class Buyer
{
int numOfApples;
int money;
public void initBuyer(int money)
{
this.numOfApples = 0;
this.money = money;
}
public void buy(FruitSeller seller, int money)
{
this.money -= money;
this.numOfApples += seller.sell(money);
}
public void printCS()
{
System.out.println("사과개수 " + numOfApples);
System.out.println("돈 " + money);
}
}
class FruitMain
{
public static void main(String[] args)
{
FruitSeller seller1 = new FruitSeller();
FruitSeller seller2 = new FruitSeller();
seller1.initFruitSeller(2000, 50, 50000);
seller2.initFruitSeller(1000, 100, 100000);
Buyer buyer = new Buyer();
buyer.initBuyer(20000);
buyer.buy(seller1, 6000);
seller1.printCS();
seller2.printCS();
buyer.printCS();
System.out.println("===================");
buyer.buy(seller2, 5000);
seller1.printCS();
seller2.printCS();
buyer.printCS();
}
}
/*
과일메인 클래스
2000원짜리 사과 3개와 1000원짜리 사과 5개를 구매한다.
우선은 정확히 자신이 사고싶은 만큼의 돈을 넘겨주고 거스름돈은 없는 걸로 하자.
사과 구매후 3명(과일장수, 구매자)의 상태를 출력한다.
*/
|
cs |
'JAVA' 카테고리의 다른 글
20220118 - JAVA 10회차까지 복습 (0) | 2022.01.19 |
---|---|
20220112 - JAVA 10회차 강의 내용 (0) | 2022.01.12 |
20220107 - JAVA 8회차 강의 내용 (0) | 2022.01.07 |
20220105 - JAVA 7회차 강의 내용 (0) | 2022.01.05 |
20220104 - JAVA 6회차 강의 복습 (0) | 2022.01.05 |