Class

Dec 18, 2023
Class

Class : 설계도

인스턴스 : 클래스로부터 만들어지는 각각의 객체
 

접근 제어자

 
접근 제어(Access Control) : 클래스의 멤버에 접근하는 것을 제어
Private : 다른 클래스에서 접근이 안 됨/ 직접 접근을 막음
Public : 다른 클래스에서 접근이 가능
 
src에 있는 모든 static들이 다 메모리에 뜸
 
생성자 : 생성될 때 무조건 실행되는 메서드
눈에 안보이지만 자동 생성되어 있음
package ex04; // 설계도 public class Circle { private int radius; // 생성자 : 생성될 때 무조건 실행되는 메서드 // 눈에 안보이지만 자동생성되어있는 메서드 public Circle(){ // 초기화 코드 radius = 100; } public double getArea(){ return 3.14 * radius * radius; } }
package ex04; public class CircleTest { public static void main(String[] args) { Circle obj = new Circle(); System.out.println(obj.getArea()); } }
notion image
 
초기화 코드 작성
package ex04; // 설계도 public class Circle { private int radius; public Circle(int r){ // 초기화 코드 radius = r; } public double getArea(){ return 3.14 * radius * radius; } }
package ex04; public class CircleTest { public static void main(String[] args) { Circle obj = new Circle(100); System.out.println(obj.getArea()); } }
 

로또


notion image
 
💡
Random() : Lib에 있는 함수 호출
num = r.nextInt(45); : 0 ~ 45까지
num = r.nextInt(45) + 1; : 1 ~ 45까지
 
💡
System.out.println(Arrays.toString(arr)); : 배열 전체 출력하기
 
동일하게 못 만들 때는 예외 처리함(if처리)
notion image
 
1) 중복값 상관없이 6개의 숫자를 뽑아서 배열에 집어넣기
package ex03; import java.util.Arrays; import java.util.Random; public class LottoEx01 { public static void main(String[] args) { int arr[] = new int[6]; // 6칸 배열 Random r = new Random(); int num; num = r.nextInt(45) + 1; // 1 ~ 45까지 arr[0] = num; num = r.nextInt(45) + 1; // 1 ~ 45까지 arr[1] = num; num = r.nextInt(45) + 1; // 1 ~ 45까지 arr[2] = num; num = r.nextInt(45) + 1; // 1 ~ 45까지 arr[3] = num; num = r.nextInt(45) + 1; // 1 ~ 45까지 arr[4] = num; num = r.nextInt(45) + 1; // 1 ~ 45까지 arr[5] = num; System.out.println(Arrays.toString(arr)); } }
 
2) 순서대로 코드짜기(코드를 말로 옮김)
package ex03; import java.util.Arrays; import java.util.Random; public class LottoEx02 { public static void main(String[] args) { int arr[] = new int[6]; // 6칸 배열 Random r = new Random(); // 랜덤함수 int num; boolean isSame; // 1. 6바퀴 돌면서 로또 번호를 추첨 // 2. 첫번째 바퀴는 공을 꺼내서 추첨한 번호를 그대로 사용 // 3. 두번째 바퀴부터는 공을 꺼내서 이전 모든 바퀴의 번호와 비교 // (isSame) : 1번이상 중복이 나오면 true, 다 다르면 false // 4. isSame == true (중복) -> 3번부터 다시 시작 // 5. isSame == false -> 공 집어넣기 -> 3번부터 다시 시작 for (int i = 0; i < arr.length; i++) { // 공 꺼내서 바로 추가 if(i == 0) { num = r.nextInt(45) + 1; // 1 ~ 6까지 arr[i] = num; continue; } while(true){ isSame = false; // 중복된 값 없음 num = r.nextInt(45) + 1; /** * 이전 번호들과 비교 * i == 1(0과 비교) * i == 2(1, 0 비교) * i == 3(2, 1, 0 비교) */ for (int j = i-1; j >=0; j--) { if (arr[j] == arr[i]) { isSame = true; break; } } if(!isSame){ arr[i] = num; break; } } } System.out.println(Arrays.toString(arr)); } }
 
3) 리팩토링
(말로 옮긴 후에 리팩토링을 할 수 있음)
package ex03; import java.util.Arrays; import java.util.Random; public class LottoEx03 { public static void main(String[] args) { int arr[] = new int[6]; // 6칸 배열 Random r = new Random(); // 랜덤함수 int num; for (int i = 0; i < arr.length; i++) { num = r.nextInt(45) + 1; arr[i] = num; for (int j = i-1; j >=0; j--) { if (arr[j] == num) { i--; break; } } } System.out.println(Arrays.toString(arr)); } }
i = 0 → 값을 0번지에 넣음
i = 1 → 0번지와 비교해서 다르면 1번지에 들어감
i =3 → 0, 1번지와 비교해서 같으니까 i-1로 삭제하고 다시 반복됨
 
4) 최종
package ex03; import java.util.Arrays; import java.util.Random; public class LottoGame { static int[] 로또구매() { int arr[] = new int[6]; Random r = new Random(); int num; for (int i = 0; i < 6; i++) { num = r.nextInt(45) + 1; arr[i] = num; for (int j = i - 1; j >= 0; j--) { if (arr[j] == num) { i--; break; } } } Arrays.sort(arr); return arr; } static int[] 로또추첨() { int arr[] = new int[6]; Random r = new Random(); int num; for (int i = 0; i < 6; i++) { num = r.nextInt(45) + 1; arr[i] = num; for (int j = i - 1; j >= 0; j--) { if (arr[j] == num) { i--; break; } } } Arrays.sort(arr); return arr; } static boolean 로또당첨확인(int[] buy, int[] lotto) { boolean isCorrect = true; for (int i = 0; i < 6; i++) { if (buy[i] != lotto[i]) { isCorrect = false; } } return isCorrect; } public static void main(String[] args) { int[] lotto = 로또추첨(); System.out.println("추첨한 로또 번호 : " + Arrays.toString(lotto)); long money = 0; while (true) { money = money + 1000; int[] buy = 로또구매(); if (로또당첨확인(buy, lotto)) { System.out.println("구매한 로또 번호 : " + Arrays.toString(buy)); break; } } System.out.println("로또 구매시 사용된 금액 : " + money + "원"); } }

 

접근자와 설정자

 
정보은닉 : 구현의 세부 사항을 클래스 안에 감추는 것
 
접근자(getters) : 필드 값을 반환
설정자(setters) : 필드 값을 설정
 
💡
접근자와 설정자를 사용하는 이유
  1. 나중에 클래스 업그레이드할 때 편함
  1. 접근자에서 매개변수를 통하여 잘못된 값이 넘어오는 것 > 차단 가능
  1. 필요할 때마다 필드 값을 동적으로 계산하여 반환 가능
  1. 접근자만 제공 > 자동적으로 읽기만 가능한 필드 생성 가능
 
Share article
RSSPowered by inblog