hyeonga_code
Java_46_toString 본문
![](https://blog.kakaocdn.net/dn/byVyKs/btswUXjdCho/zAHwbYPSvxHokaVgQZr7Gk/img.jpg)
- toString
- 객체 명 = hip의 주소값(4 바이트)
- >>> 참조 변수(메모리 구조상)_가리키는 곳에 데이터가 있는 변수(포인터)
- java의 주소는 JVM 이 관리하므로 사용자는 접근이 불가능
- 객체 명을 요구하면.toString을 붙여 출력해주지만, 생략되어 출력되는 것
ap = ap.toString()
- 멤버 필드의 수정이 있는 경우 업데이트합니다.
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class A05{}
public class same {
public static void main(String[] args) {
A05 ap = new A05();
System.out.println("ap.getClass = " + ap.getClass()); // ap.getClass = class A05
// A05 클래스 내에 아무런 코드도 작성하지 않았으나 출력되는 이유
// class A05 ( extends Object ){} 상속받았으므로 가능한 것
System.out.println("ap.toString() = " + ap.toString()); // ap.toString() = A05@15db9742
//
System.out.println("ap = " + ap); // ap = A05@15db9742
}
}
|
우클릭 > 'Source' > 'Generate toString()'
> 전체 선택
> 'Generate'
'Customer.java'
'''
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", grade="
+ grade + ", point=" + point + ", ratio=" + ratio + "]";
}
'''
=====
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
|
package oop.inheritance;
public class Customer {
/*
- 고객 명단
- 번호
- 이름
- 등급
- 포인트 // 등급에 따라 다릅니다.
- 할인률 // 등급에 따라 다릅니다.
*/
// 1. 멤버 변수 선언
int id;
String name;
String grade;
int point;
double ratio;
// 2. 생성자 선언
public Customer() {
// 생성자를 호출시, 기본 데이터를 초기화합니다.
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.0;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", grade="
+ grade + ", point=" + point + ", ratio=" + ratio + "]";
}
}
|
'CustomerApp.java'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package oop.inheritance;
//모든 클래스는 패키지 안에 생성합니다.
public class CustomerApp {
// 모든 함수_메소드는 클래스 안에 작성합니다.
public static void main(String[] args) {
// Java의 모든 코드는 함수 안에 작성합니다.
// 생성자 호출
Customer cust1 = new Customer();
// 값 지정
cust1.id = 1010;
cust1.name = "cus1";
// 출력.toString();
System.out.println(cust1.toString());
/*
Customer [id=1010, name=cus1, grade=SILVER, point=0, ratio=0.0]
*/
}
}
|
>>> 생성자에 값을 지정하는 기능까지 추가합니다.
'Customer.java'
'''
// 4. 생성자에서 값 지정 기능 추가
public Customer(int id, String name) {
this.id = id;
this.name = name;
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.0;
}
'''
=====
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
|
package oop.inheritance;
public class Customer {
/*
- 고객 명단
- 번호
- 이름
- 등급
- 포인트 // 등급에 따라 다릅니다.
- 할인률 // 등급에 따라 다릅니다.
*/
// 1. 멤버 변수 선언
int id;
String name;
String grade;
int point;
double ratio;
// 2. 생성자 선언
public Customer() {
// 생성자를 호출시, 기본 데이터를 초기화합니다.
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.0;
}
// 3. 정보 출력 확인
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", grade="
+ grade + ", point=" + point + ", ratio=" + ratio + "]";
}
// 4. 생성자에서 값 지정 기능 추가
public Customer(int id, String name) {
/*
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.0;
- 생성자가 많아지는 경우 모든 생성자의 데이터를 변경해야 합니다.
>>> this()로 작성합니다.
*/
this();
this.id = id;
this.name = name;
}
}
|
'CustomerApp.java'
=====
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
|
package oop.inheritance;
//모든 클래스는 패키지 안에 생성합니다.
public class CustomerApp {
// 모든 함수_메소드는 클래스 안에 작성합니다.
public static void main(String[] args) {
// Java의 모든 코드는 함수 안에 작성합니다.
// 생성자 호출
Customer cust1 = new Customer();
// 값 지정
cust1.id = 1010;
cust1.name = "cus1";
// 출력.toString();
System.out.println(cust1.toString());
/*
Customer [id=1010, name=cus1, grade=SILVER, point=0, ratio=0.0]
*/
Customer cust2 = new Customer(1020,"cus2");
System.out.println(cust2.toString());
/*
Customer [id=1020, name=cus2, grade=SILVER, point=0, ratio=0.0]
*/
}
}
|
- 특정 기간동안 값을 수정하고 싶은 경우 오버로딩을 사용합니다.
'''
// 5. 특정 기간 point 적용
public Customer(int id, String name, int point) {
this(id,name);
/*
- 다른 메소드에서 이미 선언한 경우 가져올 수 있게 합니다.
- this(매개변수)가 있는 생성자를 호출합니다.
- this 가 있는 생성자를 호출합니다.
*/
this.point = point;
}
'''
'Customer.java'
=====
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
|
package oop.inheritance;
public class Customer {
/*
- 고객 명단
- 번호
- 이름
- 등급
- 포인트 // 등급에 따라 다릅니다.
- 할인률 // 등급에 따라 다릅니다.
*/
// 1. 멤버 변수 선언
int id;
String name;
String grade;
int point;
double ratio;
// 2. 생성자 선언
public Customer() {
// 생성자를 호출시, 기본 데이터를 초기화합니다.
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.0;
}
// 3. 정보 출력 확인
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", grade="
+ grade + ", point=" + point + ", ratio=" + ratio + "]";
}
// 4. 생성자에서 값 지정 기능 추가
public Customer(int id, String name) {
/*
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.0;
- 생성자가 많아지는 경우 모든 생성자의 데이터를 변경해야 합니다.
>>> this.()로 작성합니다.
*/
this();
this.id = id;
this.name = name;
}
// 5. 특정 기간 point 적용
public Customer(int id, String name, int point) {
this(id,name);
/*
- 다른 메소드에서 이미 선언한 경우 가져올 수 있게 합니다.
- this(매개변수)가 있는 생성자를 호출합니다.
- this 가 있는 생성자를 호출합니다.
*/
this.point = point;
}
}
|
'CustomerApp.java'
'''
Customer cust3 = new Customer(1030,"cus3",500);
System.out.println(cust3.toString());
'''
=====
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
|
package oop.inheritance;
//모든 클래스는 패키지 안에 생성합니다.
public class CustomerApp {
// 모든 함수_메소드는 클래스 안에 작성합니다.
public static void main(String[] args) {
// Java의 모든 코드는 함수 안에 작성합니다.
// 생성자 호출
Customer cust1 = new Customer();
// 값 지정
cust1.id = 1010;
cust1.name = "cus1";
// 출력.toString();
System.out.println(cust1.toString());
/*
Customer [id=1010, name=cus1, grade=SILVER, point=0, ratio=0.0]
*/
Customer cust2 = new Customer(1020,"cus2");
System.out.println(cust2.toString());
/*
Customer [id=1020, name=cus2, grade=SILVER, point=0, ratio=0.0]
*/
Customer cust3 = new Customer(1030,"cus3",500);
System.out.println(cust3.toString());
/*
Customer [id=1030, name=cus3, grade=SILVER, point=500, ratio=0.0]
*/
}
}
|
- 가격 계산
- 할인은 없습니다.
- 보너스가 올라갑니다.
'Customer.java'
'''
// 6. 가격계산
public int calcPrice(int price) {
this.point += price * this.ratio;
// 포인트 = 포인트 + (구매한 가격 * 기존 할인률)
return price;
}
'''
=====
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
|
package oop.inheritance;
public class Customer {
/*
- 고객 명단
- 번호
- 이름
- 등급
- 포인트 // 등급에 따라 다릅니다.
- 할인률 // 등급에 따라 다릅니다.
*/
// 1. 멤버 변수 선언
int id;
String name;
String grade;
int point;
double ratio;
// 2. 생성자 선언
public Customer() {
// 생성자를 호출시, 기본 데이터를 초기화합니다.
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.1; // 0.0일 경우 계산이 불가능합니다.
}
// 3. 정보 출력 확인
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", grade="
+ grade + ", point=" + point + ", ratio=" + ratio + "]";
}
// 4. 생성자에서 값 지정 기능 추가
public Customer(int id, String name) {
/*
this.grade = "SILVER";
this.point = 0;
this.ratio = 0.0;
- 생성자가 많아지는 경우 모든 생성자의 데이터를 변경해야 합니다.
>>> this.()로 작성합니다.
*/
this();
this.id = id;
this.name = name;
}
// 5. 특정 기간 point 적용
public Customer(int id, String name, int point) {
this(id,name);
/*
- 다른 메소드에서 이미 선언한 경우 가져올 수 있게 합니다.
- this(매개변수)가 있는 생성자를 호출합니다.
- this 가 있는 생성자를 호출합니다.
*/
this.point = point;
}
// 6. 가격계산
public int calcPrice(int price) {
this.point += price * this.ratio;
// 포인트 = 포인트 + (구매한 가격 * 기존 할인률)
return price;
}
}
|
'CustomerApp.java'
'''
// 계산
cust2.calcPrice(3000); // 5000원짜리 상품을 구매했습니다.
System.out.println(cust2.toString());
'''
=====
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
|
package oop.inheritance;
//모든 클래스는 패키지 안에 생성합니다.
public class CustomerApp {
// 모든 함수_메소드는 클래스 안에 작성합니다.
public static void main(String[] args) {
// Java의 모든 코드는 함수 안에 작성합니다.
// 생성자 호출
Customer cust1 = new Customer();
// 값 지정
cust1.id = 1010;
cust1.name = "cus1";
// 출력.toString();
System.out.println(cust1.toString());
/*
Customer [id=1010, name=cus1, grade=SILVER, point=0, ratio=0.0]
*/
Customer cust2 = new Customer(1020,"cus2");
System.out.println(cust2.toString());
/*
Customer [id=1020, name=cus2, grade=SILVER, point=0, ratio=0.0]
*/
Customer cust3 = new Customer(1030,"cus3",500);
System.out.println(cust3.toString());
/*
Customer [id=1030, name=cus3, grade=SILVER, point=500, ratio=0.0]
*/
// 계산
cust2.calcPrice(3000); // 5000원짜리 상품을 구매했습니다.
System.out.println(cust2.toString());
}
}
|
'Java' 카테고리의 다른 글
Java_48_오버라이드 (0) | 2023.10.12 |
---|---|
Java_47_상속 (0) | 2023.10.11 |
Java_45_클래스_중첩 클래스 (0) | 2023.10.08 |
Java_44_클래스_지정 예약어 (0) | 2023.10.07 |
Java_43_지정 예약어 (0) | 2023.10.06 |