728x90
생성자 만들어보기
package ch04;
public class Bus {
// 속성(상태)
int busNumber;
int count;
int money;
double weight;
// 생성자 만들어 보기
// 생성자는 리턴 타입이 없다. 메서드와 구분하세요!
// 반드시 클래스 이름과 동일 해야 한다.
// 1. - 기본 생성자는 컴퍼일러가 자동으로 만들어 준다.
// 단 !! (사용자 정의 생성자가 없을 경우)
public Bus() {
}
// 개발자가 직접 생성자를 명시하는 것을
// 사용자 정의 생성자라고 부른다.
public Bus(int busNumber) {
this.busNumber = busNumber;
}
public Bus(double count) {
}
public Bus(int busNumber, int count) {
this.busNumber = busNumber;
this.count = count;
}
public Bus(int busNumber, int count, int money) {
this.busNumber = busNumber;
this.count = count;
this.money = money;
}
// 행위(기능)
public void take(int m) {
money += m;
count++;
}
public void showInfo() {
System.out.println("버스 번호 : " + busNumber);
System.out.println("승객 수 : " + count);
System.out.println("금액 : " + money);
System.out.println("==================");
}
} // end of class
생성자 사용해보기
package ch04;
public class BusMainTest1 {
// 메인 함수
public static void main(String[] args) {
// bus 100 <-- stack
// 객체 <-- heap 메모리 영역에 올라 간다.
Bus bus100 = new Bus();
bus100.busNumber = 100;
bus100.count = 0;
bus100.money = 0;
// 생성자 사용해 보기 ( 사용자 정의 생성자)
Bus bus200 = new Bus(200);
bus200.showInfo();
// 매개변수 순서대로 값을 입력 해주어야 한다.
Bus bus300 = new Bus(300, 1);
bus300.showInfo();
Bus bus400 = new Bus(400, 1, 1300);
bus400.showInfo();
} // end of main
} // end of class
버스 번호 : 200
승객 수 : 0
금액 : 0
==================
버스 번호 : 300
승객 수 : 1
금액 : 0
==================
버스 번호 : 400
승객 수 : 1
금액 : 1300
==================
생성자 만들고 사용하기 연습
package ch04;
public class Subway {
// 1. 멤버 변수를 설계
String subwayName;
double subwayTime;
int money;
// 2. 생성자 2개 이상 설계
public Subway() {
}
public Subway(String subwayName, double subwayTime) {
this.subwayName = subwayName;
this.subwayTime = subwayTime;
}
public Subway(String subwayName, double subwayTime, int money) {
this.subwayName = subwayName;
this.subwayTime = subwayTime;
this.money = money;
}
// 3. 지하철에 맞는 메서드를 설계
public void take(int m) {
money += m;
}
public void showInfo() {
System.out.println(subwayName + "에 " + subwayTime + " 분에 열차가 도착합니다.");
System.out.println(" 요금은" + money + " 원 입니다.");
}
} // end of class
package ch04;
public class SubwayMainTest {
public static void main(String[] args) {
// 사용자 정의 생성자를 하나만 설꼐한다면
// Subway를 인스턴스화 시키는 방법은 오직
// 하나만 존재하게 된다.
Subway subway1 = new Subway("서면역", 10.46, 1300);
subway1.showInfo();
} // end of main
} // end of class
서면역에 10.46 분에 열차가 도착합니다.
요금은1300 원 입니다.
package ch04;
// 설계 하는 쪽 코드
public class Human {
// 속성
String name;
int height;
double weight;
boolean isMan;
// 생성자
public Human(String name) {
this.name = name;
// !! 생성자 영역안에서 필요하다면
// 식을 넣거나, 값을 초기화 하거나
// 또는 다른 메서드를 호출할 수 있다.
isMan = true;
if (this.height == 0) {
this.height = 180;
}
showInfo();
}
public Human(String name, boolean isMan) {
this.name = name;
this.isMan = isMan;
showInfo();
}
// 행위
public void showInfo() {
System.out.println("이름 :" + name);
System.out.println("키 :" + height);
System.out.println("몸무계 :" + weight);
System.out.println("성별 남자인가? :" + isMan);
System.out.println("================");
}
} // end of class
package ch04;
// 사용하는 쪽 코드
public class HumanMainTest {
// 메인 함수
public static void main(String[] args) {
Human human1 = new Human("야스오");
Human human2 = new Human("소나", false);
// System.out.println("야스오 생성 완료");
} // end of main
} // end of class
이름 :야스오
키 :180
몸무계 :0.0
성별 남자인가? :true
================
이름 :소나
키 :0
몸무계 :0.0
성별 남자인가? :false
================
Bus 클래스
package ch05_1;
public class Bus {
// 상태
int busNumber; // 버스 호선
int count ; // 승객수
int money; // 수익금
// 생성자 - 버스 호선
public Bus(int number) {
this.busNumber = number;
}
// 기능
// 승객을 태우다(버스요금)
public void take(int money) {
this.money += (1_300 * count);
}
// 승객을 하차시키다
public void takeOff(int count) {
this.count = count;
if(this.count <= 0) {
} else {
System.out.println("하차 합니다.");
}
}
} // end of class
Student 클래스
package ch05_1;
public class Student {
//속성
String name; // 학생의 이름
int money; // 학생의 용돈
//생성자 - 이름과 용돈을 받을수 있도록 설계
public Student(String name, int money) {
this.name = name;
this.money = money;
}
// 기능
// 학생이 버스를 탄다
public void takeBus(Bus bus) {
bus.take(1);
this.money -= 1_300;
}
// 학생이 버스를 내리다
public void takeOffBus(Bus bus) {
bus.takeOff(1);
}
// 상태창 기능
public void showInfo() {
System.out.println("===== 상태창 =====");
System.out.println("학생의 남은 용돈 : " + money);
System.out.println("학생의 이름 : " + name);
}
} // end of class
MainTest
package ch05_1;
public class MainTest1 {
public static void main(String[] args) {
// 버스 객체 3개를 만들어 주세요
Bus bus1 = new Bus(1);
Bus bus2 = new Bus(2);
Bus bus3 = new Bus(3);
// 학생 객체 2개를 만들어 주세요
Student studentKim = new Student("김민재", 30000);
Student studentSon = new Student("손흥민", 50000);
// 학생이 버스를 선택해서 승차 및 하차를 시켜 보세요
studentKim.takeBus(bus2);
studentKim.showInfo();
} // end of main
} // end of class
===== 상태창 =====
학생의 남은 용돈 : 28700
학생의 이름 : 김민재
Archer 클래스
package ch05_2;
public class Archer {
String name;
int power;
int hp;
public Archer(String name) {
this.name = name;
this.power = 20;
this.hp = 80;
}
public void attackWarrior(Warrior warrior) {
// 전사를 공격합니다.
warrior.beAttacked(this.power);
System.out.println("궁수가 전사를 공격 합니다.");
}
public void attackWizard(Wizard wizard) {
// 마법사를 공격합니다.
wizard.beAttacked(this.power);
System.out.println("궁수가 마법사를 공격 합니다.");
}
public void beAttacked(int power) {
// 만약 hp가 0 이하라면
if (this.hp <= 0) {
System.out.println("이미 사망 !!!");
this.hp = 0;
} else {
this.hp = this.hp - power;
}
}
public void showInfo() {
System.out.println("===== 상태창 =====");
System.out.println("닉네임 :" + name);
System.out.println("공격력 :" + power);
System.out.println("체력 :" + hp);
}
public void showInfo2() {
System.out.println("궁수의 남은 체력은 " + hp + "입니다");
}
} // end of class
Warrior 클래스
package ch05_2;
// 클래스를 설계한 쪽
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) {
// archer 주소값을 넘겨 받은 상태이다.
archer.beAttacked(this.power);
System.out.println("전사가 궁수를 공격 합니다.");
}
public void attackWizard(Wizard wizard) {
// 마법사를 공격 함
wizard.beAttacked(this.power);
System.out.println("전사가 마법사를 공격 합니다.");
}
public void beAttacked(int power) {
// 내가 공격을 받다
if (this.hp <= 0) {
System.out.println("이미 사망 !!!");
this.hp = 0;
} else {
this.hp = this.hp - power;
}
}
public void showInfo() {
System.out.println("===== 상태창 =====");
System.out.println("닉네임 :" + name);
System.out.println("공격력 :" + power);
System.out.println("체력 :" + hp);
}
public void showInfo2() {
System.out.println("전사의 남은 체력은 " + hp + "입니다");
}
} // end of class
Wizard 클래스
package ch05_2;
public class Wizard {
String name;
int power;
int hp;
public Wizard(String name) {
this.name = name;
this.power = 30;
this.hp = 50;
}
public void attackWarrior(Warrior warrior) {
warrior.beAttacked(this.power);
System.out.println("마법사가 전사를 공격 합니다.");
}
public void attackArcher(Archer archer) {
archer.beAttacked(this.power);
System.out.println("마법사가 궁수를 공격 합니다.");
}
public void beAttacked(int power) {
// 만약 hp가 0 이하라면
if (this.hp <= 0) {
System.out.println("이미 사망 !!!");
this.hp = 0;
} else {
this.hp = this.hp - power;
}
}
public void showInfo() {
System.out.println("===== 상태창 =====");
System.out.println("닉네임 :" + name);
System.out.println("공격력 :" + power);
System.out.println("체력 :" + hp);
}
public void showInfo2() {
System.out.println("마법사의 남은 체력은 " + hp + "입니다");
}
} // end of class
MainTset
package ch05_2;
import java.util.Scanner;
public class MainTest1 {
public static void main(String[] args) {
Warrior warrior1 = new Warrior("전사");
Archer archer1 = new Archer("궁수");
Wizard wizard1 = new Wizard("마법사");
// warrior1.attackArcher(archer1);
// archer1.attackWarrior(warrior1);
// wizard1.attackWarrior(warrior1);
// 궁수에 상태값을 확인해 보자.
archer1.showInfo();
warrior1.showInfo();
wizard1.showInfo();
Scanner num = new Scanner(System.in);
int input = 1;
System.out.println("누구를 생성하겠습니까?");
while (input != 0) {
System.out.println("1. 전사" + " 2. 궁수" + " 3. 마법사" + " 4. 생성취소");
input = num.nextInt();
if (input == 1) {
System.out.println("전사를 생성하였습니다.");
System.out.println("누구를 공격하시겠습니까 ?");
System.out.println("1. 궁수, 2. 마법사");
int a = num.nextInt();
if (a == 1) {
System.out.println("궁수를 공격.");
warrior1.attackArcher(archer1);
archer1.showInfo2();
} else if (a == 2) {
System.out.println("마법사를 공격.");
warrior1.attackWizard(wizard1);
wizard1.showInfo2();
}
} else if (input == 2) {
System.out.println("궁수를 생성하였습니다.");
System.out.println("누구를 공격하시겠습니까 ?");
System.out.println("1. 전사, 2. 마법사");
int b = num.nextInt();
if (b == 1) {
System.out.println("전사를 공격.");
archer1.attackWarrior(warrior1);
warrior1.showInfo2();
} else if (b == 2) {
System.out.println("마법사를 공격.");
archer1.attackWizard(wizard1);
wizard1.showInfo2();
}
} else if (input == 3) {
System.out.println("마법사를 생성하였습니다.");
System.out.println("누구를 공격하시겠습니까 ?");
System.out.println("1. 전사, 2. 궁수");
int c = num.nextInt();
if (c == 1) {
System.out.println("전사를 공격.");
wizard1.attackWarrior(warrior1);
warrior1.showInfo2();
} else if (c == 2) {
System.out.println("궁수를 공격.");
wizard1.attackArcher(archer1);
archer1.showInfo2();
}
} else if (input == 4) {
System.out.println("생성을 취소하였습니다");
}
break;
}
System.out.println("공격이 종료 됩니다.");
} // end of main
} // end of class
===== 상태창 =====
닉네임 :궁수
공격력 :20
체력 :80
===== 상태창 =====
닉네임 :전사
공격력 :10
체력 :100
===== 상태창 =====
닉네임 :마법사
공격력 :30
체력 :50
누구를 생성하겠습니까?
1. 전사 2. 궁수 3. 마법사 4. 생성취소
3
마법사를 생성하였습니다.
누구를 공격하시겠습니까 ?
1. 전사, 2. 궁수
2
궁수를 공격.
마법사가 궁수를 공격 합니다.
궁수의 남은 체력은 50입니다
공격이 종료 됩니다.
728x90
'Example' 카테고리의 다른 글
[E-HTML&CSS] 선택자 예시 (15) | 2023.08.04 |
---|---|
[E-HTML&CSS] 연습문제 + div, span, 시멘틱태그 예시, css 기본선택자 (16) | 2023.08.03 |
[E-HTML] input, select, textarea, lable 예시 + 연습문제 (14) | 2023.08.02 |
[E-HTML] text, list, table, audio, video, image 예시 (15) | 2023.08.01 |
[E-Java] 접근 제어자 예시 (20) | 2023.08.01 |