728x90
반응형
<목차>
1) 접근 제어자, Encapsulation
1-1) 개념
1-2) 예제 풀이
2) 배열 복사
2-1) 개념
2-2) 예제 풀이
1) 접근 제어자, Encapsulation
1-1) 개념
접근 제어자(= 접근 지정자) : 클래스 내부의 변수나 메서드, 생성자에 대한 접근 권한을 가짐
- public : 외부 클래스 어디에서나 접근
- protected : 같은 패키지 내부와 상속 관계의 클래스에서만 접근
- (default) : 같은 패키지 내부에서만 접근
- private : 같은 클래스 내부에서만 접근
1-2) 예제 풀이
패키지 chapter08.classPart
Encapsulation.class
package chapter08.classPart;
class Member {
private String id;
private String password;
private String phoneNumber;
public Member(String id, String password, String phoneNumber) {
this.id = id;
this.password = password;
this.phoneNumber = phoneNumber;
}
// 읽기 전용 "getter"
public String getID() {
return this.id;
}
// 비밀번호 수정 "setter"
public void setPassword(String password) {
this.password = password;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void memberInfo(String pwCheck) {
if (this.password.equals(pwCheck)) {
System.out.println("[ 회원 정보 ]");
System.out.println(" * ID : " + this.id);
System.out.println(" * PW : " + this.password);
System.out.println(" * Phone: " + this.phoneNumber);
}
}
}
public class Encapsulation {
public static void main(String[] args) {
/*
* 접근 지정자, 접근 제어자
- 클래스 내부의 변수나 메서드, 생성자에 대한 접근 권한을 가짐
- public : 외부 클래스 어디에서나 접근
- protected : 같은 패키지 내부와 상속 관계의 클래스에서만 접근
- (default) : 같은 패키지 내부에서만 접근
- private : 같은 클래스 내부에서만 접근
*/
Member member1 = new Member("홍길동", "123", "01011112222");
/*
// id, password가 public일 경우에만 사용 가능한 코드
member1.id = "이순신";
System.out.println(member1.id);
System.out.println(member1.password);
*/
String id = member1.getID();
System.out.println(id);
member1.setPassword("456");
member1.memberInfo("456");
}
}
Character.class
package chapter08.classPart;
public class Character {
// 캐릭터 정보
private String id; // 아이디
private String job; // 직업
private int level; // 레벨
private int str; // 힘 스탯
private int dex; // 민첩 스탯
private int intel; // 지능 스탯
// 인스턴스 초기화 블록 : 생성자가 많아졌다고 가정하면 각 생성자마다 공통적으로 초기화를 해야되는 부분이 있을 수 있어 공통적으로 몰아가지고 초기화를 시키기 위해 수행하는 코드
{
this.level = 1;
this.str = 1;
this.dex = 1;
this.intel = 1;
}
public Character(String id, String job) {
super();
this.id = id;
this.job = job;
}
public Character(String id, String job, int level, int str, int dex, int intel) {
super();
this.id = id;
this.job = job;
this.level = level;
this.str = str;
this.dex = dex;
this.intel = intel;
}
public void skill() {
if (this.job.equals("마법사")) {
System.out.println("파이어 볼!!");
} else if (this.job.equals("전사")) {
System.out.println("배쉬!!");
} else if (this.job.equals("도적")) {
System.out.println("크리티컬!!");
} else {
System.out.println("기본공격!!");
}
}
@Override
public String toString() { // toString() : 객체의 정보를 문자열로 변환해주는 메소드
return "[" + id + "(" + job + ") Lv" + level + "]님의 스탯 : str(" + str + "), dex(" + dex + "), intel(" + intel + ")";
}
}
Game_Example.class
package chapter08.classPart;
public class Game_Example {
public static void main(String[] args) {
Character cha1 = new Character("삼성동생밤", "마법사");
System.out.println(cha1);
Character cha2 = new Character("휘발유", "도적", 4, 10, 14, 4);
System.out.println(cha2);
cha2.skill();
}
}
2) 배열 복사
2-1) 개념
패키지 chapter06.array
Book.class
package chapter06.array;
public class Book {
private String bookName;
private String author;
public Book() {}
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
public void showBookInfo() {
System.out.println(bookName + ", " + author);
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
ShallowCopy.class
package chapter06.array;
public class ShallowCopy {
public static void main(String[] args) {
// 얕은 복사
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
Book[] bookArray3 = bookArray1;
bookArray1[0] = new Book("태백산맥", "조정래");
bookArray1[1] = new Book("데미안", "헤르만 헤세");
bookArray1[2] = new Book("어떻게 살 것인가", "유시민");
// System 클래스의 arraycopy() 메서드 : 배열의 요소를 복사할 때 사용하는 메서드
// System.arraycopy(복사할 배열, 복사할 첫 위치, 대상 배열, 붙여 넣을 첫 위치, 복사할 요소 개수)
System.arraycopy(bookArray1, 0, bookArray2, 0, 3); // arraycopy()를 통해 주소값만 복사가 된 것이다!
System.out.println("주소값1: " + bookArray1);
System.out.println("주소값2: " + bookArray2);
System.out.println("주소값3: " + bookArray3);
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo();
}
bookArray1[0].setBookName("나목");
bookArray1[0].setAuthor("박완서");
System.out.println("=========bookArray1=========");
for (int i = 0; i < bookArray1.length; i++) {
bookArray1[i].showBookInfo();
System.out.println(bookArray1[i]);
}
System.out.println("=========bookArray2=========");
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo();
System.out.println(bookArray2[i]);
}
System.out.println("=========bookArray3=========");
for (int i = 0; i < bookArray3.length; i++) {
bookArray3[i].showBookInfo();
System.out.println(bookArray3[i]);
}
}
}
DeepCopy.class
package chapter06.array;
public class DeepCopy {
public static void main(String[] args) {
// 깊은 복사
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
bookArray1[0] = new Book("태백산맥", "조정래");
bookArray1[1] = new Book("데미안", "헤르만 헤세");
bookArray1[2] = new Book("어떻게 할 것인가", "유시민");
bookArray2[0] = new Book();
bookArray2[1] = new Book();
bookArray2[2] = new Book();
for (int i = 0; i < bookArray1.length; i++) {
bookArray2[i].setBookName(bookArray1[i].getBookName());
bookArray2[i].setAuthor(bookArray1[i].getAuthor());
}
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo();
}
bookArray1[0].setBookName("나목");
bookArray1[0].setAuthor("박완서");
System.out.println("=====bookArray1=====");
for (int i = 0; i < bookArray1.length; i++) {
bookArray1[i].showBookInfo();
System.out.println(bookArray1[i]);
}
System.out.println("=====bookArray2=====");
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo();
System.out.println(bookArray2[i]);
}
}
}
'Java' 카테고리의 다른 글
Java(12) - 상속, 메서드 오버라이딩 (0) | 2024.04.25 |
---|---|
Java(11) - Theater 예제(극장 좌석 예약 프로그램 만들기) (0) | 2024.04.24 |
Java(9) - 메소드 오버로딩, 클래스와 인스턴스, 생성자, static (0) | 2024.04.22 |
Java(8) - 배열, foreach 문, 메서드, 로또 생성기 (0) | 2024.04.19 |
Java(7) - 배열(Array), 다차원 배열 (0) | 2024.04.18 |