서노썬
sun noes sun
서노썬
전체 방문자
오늘
어제
  • 카테고리 (142)
    • Java (89)
      • Day1 (20)
      • Day2 (16)
      • Day3 (4)
      • Day4 (5)
      • Day5 (2)
      • Day6 (2)
      • Day7 (4)
      • Day8 (6)
      • Day9 (3)
      • Day10 (0)
      • Day11 (0)
      • Day12 (0)
      • Day13 (3)
      • Day14 (0)
      • Day15 (0)
      • Day16 (0)
      • Day19 (0)
      • Day20 (0)
      • Day21 (2)
      • Day22 (4)
      • Day23 (2)
      • Day24 (5)
      • Day25 (4)
      • Day27 (2)
      • Day28 (3)
      • Day29 (1)
      • Day30 (1)
      • Day31 (0)
      • Day32 (0)
      • Dat33 (0)
      • Day34 (0)
      • Day35 (0)
      • Day36 (0)
    • HTML (37)
      • Day36 (20)
      • Day37 (3)
      • Day38 (2)
      • Day39 (8)
      • Day40 (3)
    • SQL (4)
      • Day40 (4)
      • Day41 (0)
      • Day42 (0)
      • Day43 (0)
      • Day44 (0)
      • Day45 (0)
    • JSP (0)
      • Day46 (0)
      • Day75 (0)
    • PYTHON (0)
      • Day75 (0)
      • Day76 (0)
    • Photo (12)

블로그 메뉴

  • 홈
  • 방명록

인기 글

최근 글

태그

  • 자바
  • 자바continue
  • Java
  • java데이터타입
  • 자바연산자
  • 자바break
  • java메뉴입력
  • 자바자료형
  • 논리연산자
  • java자료형

티스토리

hELLO · Designed By 정상우.
서노썬

sun noes sun

Java/Day6

[Java] 생성자 constructor

2021. 11. 7. 17:20

1. 생성자 constructor

 

생성자는 인스턴스가 생성되는 순간에 자동으로 호출되는 특별한 메소드로 인스턴스에 필요한 공간을 만들고 초기화하는 역할을 한다.

 

- 생성자명은 클래스명과 대소문자까지 동일해야 한다.

- 생성자는 반환하는 값이 있어서도 안되고, 정의해서도 안된다.
- 생성자 내에서는 상수의 초기화가 가능하다.

 

 

2. 생성자 예제 - 삼각형 클래스

 

(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();
    }
}
 

 

 

(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
class Triangle
{
    int width;
    int height;
    int area;
 
    public Triangle(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(10,5);
        t1.areaOfTriangle();
        t1.printCS();
 
        Triangle t2 = new Triangle(4,2);
        t2.areaOfTriangle();
        t2.printCS();
 
        Triangle t3 = new Triangle(20,10);
        t3.areaOfTriangle();
        t3.printCS();
    }
}
 

 

①  public void initTriangle(int width, int height) → public Triangle(int width, int height)

(inintTriangle에서 class명 Triangle과 동일하게, 반환값이 없어야 하며 정의하지 않아야 하므로 void도 수정)

 

②

 
 
        Triangle t1 = new Triangle(); → Triangle t1 = new Triangle(10, 5);
        t1.initTriangle(10, 5);
 
        Triangle t2 = new Triangle(); → Triangle t2 = new Triangle(4, 2);
        t2.initTriangle(4, 2);
 
        Triangle t3 = new Triangle(); → Triangle t1 = new Triangle(20, 10);
        t3.initTriangle(20, 10);
 
 
 
 
 
2. 생성자 예제 - 원 클래스
 
 
(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
43
44
45
46
47
48
49
50
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 c2 = new Circle();
      c2.initCircle(10);
       c2.perimeterOfCircle();
        c2.areaOfCircle();
        c2.printCS();
 
        Circle c3 = new Circle();
      c3.initCircle(30);
       c3.perimeterOfCircle();
        c3.areaOfCircle();
        c3.printCS();
    }
}
 
cs

 

 

(2) 수정

 

①  public void initCircle(double radius) → public Circle(double radius)

 

②

 
class CircleMain
{
    public static void main(String[] args)
    {
        Circle c1 = new Circle();    →    Circle c1 = new Circle(5);
       c1.initCircle(5);


        Circle c2 = new Circle();    →    Circle c2 = new Circle(10);
      c2.initCircle(10);


        Circle c3 = new Circle();    →   Circle c3 = new Circle(30);
      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
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();
    }
}
 
cs

 

 

(2) 수정

 

① public void initFruitSeller(int PRICE, int numOfApples, int money)

→ public FruitSeller(int PRICE, int numOfApples, int money)

 

② public void initBuyer(int money) → public Buyer(int money)

 

③

1
2
3
4
5
6
7
8
9
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);
    }
 
cs
↓
1
2
3
4
5
6
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);
    }
 
cs

'Java > Day6' 카테고리의 다른 글

[Java] 생성자 constructor - 구슬치기 게임  (0) 2021.11.07
    'Java/Day6' 카테고리의 다른 글
    • [Java] 생성자 constructor - 구슬치기 게임

    티스토리툴바