6 public class Sum Test public static void
6장. 클래스 : 속성 public class Sum. Test { public static void main(String a 1[]) { int a, b, sum; a = Integer. parse. Int(a 1[0]); b = Integer. parse. Int(a 1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System. out. println("두수의 "); System. out. println("두수의 합은 " + sum + "입니다"); } } 1
1 클래스의 일반 구조 l 실습예제 Box_Sample. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 public class Box_Sample { int width; int height; 속성 int depth; int volume; Box_Sample(int w, int h, int d) { width = w; 생성자 height = h; depth = d; } int volume_compute() { volume = width * height * depth; 메소드 return volume; } } 6장. 클래스 : 속성 기능 5
2 클래스 선언 l 속성과 메소드를 가지는 클래스 형식 6장. 클래스 : 속성 class Box { int width; 속성 int height; int depth; public void volume() { int vol; vol = width * height * depth; System. out. println("Volume is "+vol); } } 메소드 8
2 클래스 선언 l 속성, 생성자, 메소드를 가지는 클래스 형식 6장. 클래스 : 속성 class Box { int width; int height; 속성 int depth; public void Box(int w, int h, int d) { width=w; height=h; depth=d; } public void volume() { //메소드 int vol; vol = width * height * depth; System. out. println("Volume is "+vol); } } 메소드 9
3 객체의 생성 l 객체의 선언과 생성 class Box { int width; int height; int depth; } class My. Box {. . . Box mybox 1; Box mybox 2; mybox 1 = new Box(); mybox 2 = new Box(); . . . 6장. 클래스 : 속성 193 page 13
3 객체의 생성 l 실습예제 Box 1 Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 객체의 속성만 가진 클래스 class Box 1 { int width; int height; int depth; } public class Box 1 Test { public static void main(String args[]) { Box 1 mybox 1 = new Box 1(); Box 1 클래스로부터 객체 생성 Box 1 mybox 2 = new Box 1(); int vol 1, vol 2; 6장. 클래스 : 속성 mybox 1. width = 20; mybox 1. height = 40; mybox 1. depth = 15; 객체의 속성에 값을 배정 mybox 2. width = 10; mybox 2. height = 20; mybox 2. depth = 30; 14
3 객체의 생성 l 실습예제 Box 1 Test. java 19 20 21 22 23 24 25 26 vol 1 = mybox 1. width * mybox 1. height * mybox 1. depth; System. out. println(" 첫번째 박스의 부피는 " + vol 1 + "입니다. "); vol 2 = mybox 2. width * mybox 2. height * mybox 2. depth; System. out. println(" 두 번째 박스의 부피는 " + vol 2 + "입니다. "); } } 6장. 클래스 : 속성 15
4 멤버 변수 4 -1 객체 변수 l 실습예제 Box 2 Test. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 class Box 2 { int width=10; 속성으로 값을 지정 int height=20; int depth=30; } public class Box 2 Test { public static void main(String args[]) { int myint 1 = 100; int myint 2 = myint 1; System. out. println("첫 번째 값 : " + myint 1 + " 두 번째 값 : " + myint 2); myint 1 = 200; System. out. println("첫 번째 값 : " + myint 1 + " 두 번째 값 : " + myint 2); Box 2 mybox 1 = new Box 2(); 6장. 클래스 : 속성 20
4 멤버 변수 4 -1 객체 변수 l 실습예제 Box 2 Test. java 16 17 18 19 20 21 22 23 24 25 26 27 28 객체 변수에 객체 변수를 배정 Box 2 mybox 2 = mybox 1; mybox 1의 width 속성값 변경 mybox 1. width = 20; System. out. println("mybox 1. width : " + mybox 1. width); 같은 값이 출력 System. out. println("mybox 2. width : " + mybox 2. width); mybox 2의 depth 속성값 변경 mybox 2. depth = 123; System. out. println("mybox 1. depth : " + mybox 1. depth); 같은 값이 출력 System. out. println("mybox 2. depth : " + mybox 2. depth); } } 6장. 클래스 : 속성 21
4 멤버 변수 4 -2 클래스 변수 l 실습예제 Box 3 Test. java 01 02 03 04 05 06 07 08 09 10 class Box 3 { int width; int height; int depth; long id. Num; static long box. ID = 0; public void increment() { id. Num = ++box. ID; } } 6장. 클래스 : 속성 클래스 변수 선언 메소드가 호출될 때마다 클래스 변숫값 증가 25
4 멤버 변수 4 -2 클래스 변수 l 실습예제 Box 3 Test. java 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Box 3 Test { public static void main(String args[]) { Box 3 mybox 1 = new Box 3(); Box 3 mybox 2 = new Box 3(); Box 3 mybox 3 = new Box 3(); Box 3 mybox 4 = new Box 3(); mybox 1. increment(); mybox 2. increment(); mybox 3. increment(); mybox 4. increment(); System. out. println("mybox 1의 id 번호 : " + System. out. println("mybox 2의 id 번호 : " + System. out. println("mybox 3의 id 번호 : " + System. out. println("mybox 4의 id 번호 : " + System. out. println("전체 박스의 개수는 " + Box 3. box. ID + "입니다. "); } } 6장. 클래스 : 속성 mybox 1. id. Num); mybox 2. id. Num); mybox 3. id. Num); mybox 4. id. Num); 클래스 변수에 접근할 때는 클래스 명을 통해서 접근 26
4 멤버 변수 4 -4 멤버 변수 접근 방법 l 객체 변수 접근 방법 형식 객체이름. 객체변수 class A { int aa; int bb; int cc; static int s = 0; } class ATest { public static void main(String args[]) { A obja = new A(); obja. aa = 4; obja. bb = obja. aa * 2; obja. cc = A. s; . . 6장. 클래스 : 속성 30
5 멤버 변수 접근 한정자 5 -1 public l 클래스 내의 멤버 변수 접근을 제한할 수 있는 방법으로 접근 한정자를 제공 l public으로 선언된 객체 변수는 항상 접근 가능하다는 것을 의미 class Box 3 { public int width; public int height; public int depth; public long id. Num; static long box. ID = 0; public void increment() { id. Num = ++box. ID; } }. . . . Box 3 mybox 1 = new Box 3(); mybox 1. width = 7; mybox 2. depth = 20; . . . . 6장. 클래스 : 속성 31
5 멤버 변수 접근 한정자 5 -2 private l private로 선언된 객체 변수는 소속된 클래스 내에서만 사용 class Box 3 { private int width; private int height; private int depth; private long id. Num; static long box. ID = 0; public void increment() { id. Num = ++box. ID; } }. . . . Box 3 mybox 1 = new Box 3(); mybox 1. width = 7; 오류 발생 mybox 2. depth = 20; . . . . 6장. 클래스 : 속성 32
- Slides: 39