복습
https://shins99.tistory.com/16
[자바 기초] 생성자
복습 https://shins99.tistory.com/12 [자바 기초] 함수와 메서드 복습 https://shins99.tistory.com/11 [자바 기초] 객체 지향 언어란? 복습 https://shins99.tistory.com/9 [자바 기초] 참조 자료형 - Scanner, Random 복습 https://
shins99.tistory.com
💡 학습 목표
1. 객체와 객체간에 상호작용이란?
2. 버스, 학생 클래스 설계하기
3. 매개변수에 참조 타입을 사용하는 의미
4. 메모리 구조도 생각해 보자.
버스 기능 만들어보기
public class Bus {
// ** 속성 **
int busNumber; // 버스 호선
int count; // 승객 수
int money; // 수익 금
// ** 생성자 **
public Bus(int number) {
this.busNumber = number;
}
// ** 기능 **
// 달린다.
// 하차 시키다
// 정보창을 보여주다.
public void run() {
System.out.println("버스가 출발 합니다");
}
// 승차 시키다
public void take(int count) {
//this.count = count + count;
this.count += count;
//this.money = this.money + 1_300;
this.money += (1_300 * count);
}
// 하차 시키다
public void takeOff(int count) {
// 만약 0명이라면 -를 해서는 안된다.
if(this.count <= 0 ) { // 방어적 코드 작성
System.out.println("잘못 입력 하였습니다");
} else {
this.count -= count;
System.out.println(count + " 명의 승객이 내립니다.");
}
}
public void showInfo() {
System.out.println("=== 상태 창 ===");
System.out.println("버스 번호 : " + this.busNumber);
System.out.println("승객 수 " + this.count);
System.out.println("수익 금 : " + this.money);
}
}
💡 상호 작용 시켜 보기 위한 설계
1. 버스 클래스 설계 하기
2. 학생 클래스 설계 하기
3. main 함수에서 실행 시켜 보고 결과 확인 하기
1. 버스 클래스 설계 하기
public class Bus {
// 상태
// 버스 호선
// 승객 수
// 수익 금
// 생성자 - 버스 호선
// 기능
// 승객을 태우다 (버스요금)
// 승객을 하차 시키다.
}
2. 학생 클래스 설계 하기
public class Student {
// 속성
// 학생의 이름, 용돈
// 생성자 - 이름, 용돈을 받을 수 있도록 설계
// 기능
// 학생이 버스를 탄다
// 학생이 버스를 내리다.
// 상태창 기능
}
3. main 함수에서 실행 시켜 보고 결과 확인 하기
public class MainTest1 {
public static void main(String[] args) {
// 버스 객체 3개를 만들어 주세요
// 학생 객체 2개를 만들어 주세요
// 학생이 버스를 선택해서 승차 및 하차를 시켜 보세요
} // end of main
} // end of class
전사 클래스 설계 하기
// 클래스를 설계한 쪽
public class Warrior {
// 참조 타입
String name;
// 기본 데이터 타입
int power;
int hp;
public Warrior(String name) {
this.name = name;
this.power = 10;
this.hp = 100;
}
public void attackArcher(Archer archer) {
// 궁수에게 공격을 함
}
public void attackWizard(Wizard wizard) {
// 마법사를 공격 함
}
// 내가 공격을 받다
public void beAttacked(int power) {
this.hp = hp - power;
}
public void showInfo() {
System.out.println("== 상태창 ==");
System.out.println("닉네임 : " + name);
System.out.println("공격력 : " + power);
System.out.println("체력 : " + hp);
}
} // end of class
궁수 클래스 설계 하기
public class Archer {
String name;
int power;
int hp;
public Archer(String name) {
this.name = name;
this.power = 15;
this.hp = 80;
}
public void attackWarrior(Warrior warrior) {
// 전사를 공격합니다.
}
public void attackWizard(Wizard wizard) {
// 마법사를 공격 합니다.
}
// 내가 공격 당하다
public void beAttacked(int power) {
this.hp = this.hp - power;
}
public void showInfo() {
System.out.println("== 상태창 ==");
System.out.println("닉네임 : " + name);
System.out.println("공격력 : " + power);
System.out.println("체력 : " + hp);
}
}
마법사 클래스 설계하기
public class Wizard {
String name;
int power;
int hp;
public Wizard(String name) {
this.name = name;
this.power = 20;
this.hp = 70;
}
public void attackWarrior(Warrior warrior) {
// 전사에게 공격을 함
warrior.beAttacked(this.power);
System.out.println("마법사가 전사를 공격 합니다.");
if (this.hp <= 0) {
System.out.println("이미 죽었습니다.");
this.hp = 0;
} else {
this.hp = hp - power;
}
}
public void attackArcher(Archer archer) {
// 궁수에게 공격을 함
archer.beAttacked(this.power);
System.out.println("마법사가 궁수를 공격 합니다.");
if (this.hp <= 0) {
System.out.println("이미 죽었습니다.");
this.hp = 0;
} else {
this.hp = hp - power;
}
}
// 내가 공격을 받다
public void beAttacked(int power) {
// this.hp -= power;
if (this.hp <= 0) {
System.out.println("이미 죽었습니다.");
this.hp = 0;
} else {
this.hp = hp - power;
}
}
public void showInfo() {
System.out.println("=== 상태창 ===");
System.out.println("닉네임 : " + name);
System.out.println("공격력 : " + power);
System.out.println("체력 : " + hp);
}
}
Scanner를 이용해 캐릭터 생성하는 프로그램을 만들어보기
import java.util.Scanner;
public class MainTest1_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("캐릭터를 생성하세요.");
System.out.println("1.전사, 2.궁수, 3.마법사, 4.게임종료");
int input = sc.nextInt();
Warrior warrior1;
Archer archer1;
Wizard wizard1;
while (input == 1) {
warrior1 = new Warrior(sc.nextLine());
System.out.println("전사를 생성합니다.");
System.out.println("닉네임을 입력하세요 :");
warrior1.name = sc.nextLine();
System.out.println("공격력을 입력하세요: ");
warrior1.power = sc.nextInt();
System.out.println("체력을 입력하세요: ");
warrior1.hp = sc.nextInt();
warrior1.showInfo();
if (input == 2) {
archer1 = new Archer(sc.nextLine());
System.out.println("궁수를 생성합니다.");
System.out.println("닉네임을 입력하세요 :");
archer1.name = sc.nextLine();
System.out.println("공격력을 입력하세요: ");
archer1.power = sc.nextInt();
System.out.println("체력을 입력하세요: ");
archer1.hp = sc.nextInt();
archer1.showInfo();
} else if (input == 3) {
wizard1 = new Wizard(sc.nextLine());
System.out.println("마법사를 생성합니다.");
System.out.println("닉네임을 입력하세요 :");
wizard1.name = sc.nextLine();
System.out.println("공격력을 입력하세요: ");
wizard1.power = sc.nextInt();
System.out.println("체력을 입력하세요: ");
wizard1.hp = sc.nextInt();
wizard1.showInfo();
} else {
break;
}
System.out.println("게임을 종료합니다.");
}
} // end of main
} // end of class
'[JAVA] > [자바 기초]' 카테고리의 다른 글
[자바 기초] this 키워드 (0) | 2023.08.02 |
---|---|
[자바 기초] 접근 제어 지시자, get(), set() 메서드 (0) | 2023.08.01 |
[자바 기초] 생성자 (0) | 2023.07.31 |
[자바 기초] 조건문 & 반복문 복습해보기 (0) | 2023.07.30 |
[자바 기초] 함수와 메서드 (0) | 2023.07.28 |