1. 기본값과 예시
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class Triangle
{
int num; // int의 기본값은 0으로 세팅된다.
boolean result; // boolean의 기본값은 false로 세팅된다.
String name; // String의 기본 값은 null로 세팅된다.
}
class Test
{
public static void main(String[] args)
{
int num;
boolean result;
System.out.println(num); // num에 대한 초기화가 이루어지지 않아 에러
System.out.println(result); // result에 대한 초기화가 이루어지지 않아 에러
Triangle t1 = new Triangle(); // Triangle 클래스 호출
System.out.println(t1.num); // int의 기본값은 0으로 세팅된다.
System.out.println(t1.result); // boolean의 기본값은 false로 세팅된다.
System.out.println(t1.name); // String의 기본 값은 null로 세팅된다.
}
}
|
cs |
- int의 기본값은 0
- booleand의 기본값은 false
- string의 기본값은 null
2. 접근 제어 지시자와 접근 허용범위
| 지시자 | 클래스 내부 | 동일 패키지 | 상속받은 클래스 | 이외의 영역 |
| private | O | X | X | X |
| default | O | O | X | X |
| protected | O | O | O | X |
| public | O | O | O | O |
3. 접근 제어 지시자와 접근 허용범위 - 예문
|
1
2
3
4
5
6
7
|
class Number
{
private int num1;
int num2;
protected int num3;
public int num4;
}
|
cs |
- int num2 와 같이 접근 제어 지시자가 기재되지 않은 경우 default의 값을 갖는다.
- num1 : 클래스 내부에서만 접근 가능
- num2 : 클래스 내부와 동일 패키지에서만 접근 가능
- num3 : 클래스 내부와 동일 패키지, 상속받은 클래스에서만 접근 가능
- num4 : 클래스 내부와 동일 패키지, 상속받은 클래스, 이외의 영역까지 어디에서나 접근 가능
4. 접근 제어 지시자 문제
(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
|
class Rectangle
{
int ulx, uly; // 좌 상단 x, y 좌표
int lrx, lry; // 우 하단 x, y 좌표
public void showArea()
{
int xLen = lrx-ulx;
int yLen = lry-uly;
System.out.println("넓이 : " + (xLen*yLen);
}
}
class RectangleTest
{
public static void main(String[] args)
{
Rectangle rec = new Rectangle();
rec.ulx=22;
rec.uly=22;
rec.lrx=10;
rec.lry=10;
rec.showArea();
}
}
|
cs |
- x좌표는 0이상 100이하의 범위를 갖는다. 오른쪽으로 이동할수록 값은 증가한다.
- y좌표는 0이상 100이하의 범위를 갖는다. 아래로 이동할수록 값은 증가한다.
- 우 하단의 x, y좌표 값이 좌 상단의 x, y 좌표값보다 작아서는 안된다.
(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
50
51
52
53
54
55
56
|
class Rectangle
{
private int ulx, uly; // 좌 상단 x, y 좌표
private int lrx, lry; // 우 하단 x, y 좌표
private boolean isProperRange(int pos)
{
if(0<=pos && pos <= 100)
return true;
else
return false;
}
public void setRectanglePoint(int lx, int ly, int rx, int ry)
{
if(lx>=rx || ly>=ry)
{
System.out.println("좌표 지정이 잘못되었습니다.");
return;
}
if( !isProperRange(lx) || !isProperRange(ly) )
{
System.out.println("좌 상단 좌표의 범위가 잘못되었습니다.");
return;
}
if( !isProperRange(rx) || !isProperRange(ry) )
{
System.out.println("우 하단 좌표의 범위가 잘못되었습니다.");
return;
}
ulx = lx;
uly = ly;
lrx = rx;
lry = ry;
}
public void showArea()
{
int xLen = lrx - ulx;
int yLen = lry - uly;
System.out.println("넓이 : " + (xLen*yLen) );
}
}
class RectangleTest
{
public static void main(String[] args)
{
Rectangle rec = new Rectangle();
rec.setRectanglePoint(-3, -1, 2, 7);
rec.showArea();
rec.setRectanglePoint(2, 2, 8, 8);
rec.showArea();
}
}
|
cs |
'Java > Day7' 카테고리의 다른 글
| [Java] class method 클래스 메소드 (0) | 2021.11.08 |
|---|---|
| [Java] access method ( getter & setter 게터앤세터) (0) | 2021.11.08 |
| [Java] class path 클래스 패스 & 패키지 (배치 프로그램 실행하기) (0) | 2021.11.07 |