728x90
Chapter 6 상속
Mini Project - p208
import java.util.*;
abstract class Sprite {
int x=3, y=3;
abstract void move(char c);
}
class Main extends Sprite {
void move(char c) {
if( c=='h') --x;
else if(c=='j') --y;
else if(c=='k') ++y;
else if(c=='l') ++x;
}
}
class Monster extends Sprite {
public Monster() {
x = y =7;
}
void move(char c) {
x += (Math.random()-0.5)>0 ? 1: -1;
y += (Math.random()-0.5)>0 ? 1: -1;
}
}
public class miniproject {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[][] board = new String[10][17];
for(int i=0;i<10;i++) {
for(int j=0;j<17;j++) {
board[i][j] = " ";
}
}
board[7][2] = "G"; //골드 위치
Main me = new Main();
board[me.y][me.x] = "@"; //주인공 위치
Monster m = new Monster();
board[m.y][m.x] = "M"; //몬스터 위치
while(true) {
System.out.print("###################\n");
for(int i=0;i<10;i++) {
System.out.print("#");
for(int j=0;j<17;j++) {
System.out.print(board[i][j]);
}
System.out.print("#\n");
}
System.out.print("###################\n");
System.out.print("왼쪽(h), 위쪽(j), 아래쪽(k), 오른쪽(l): ");
char input = sc.next().charAt(0);
board[me.y][me.x] = " ";
me.move(input);
board[m.y][m.x] = " ";
int my= m.y, mx = m.x;
m.move(input);
if(m.y<10 && m.x<17) {
board[me.y][me.x] = "@";
board[m.y][m.x] = "M";
}else {
board[me.y][me.x] = "@";
board[my][mx] = "M";
}
if(me.y == 7 && me.x ==2) {
System.out.println("골드 획득! \nYOU WIN");
break;
}
if(me.y == m.y && me.x == m.x) {
System.out.println("몬스터에게 잡혔습니다! \nGAME OVER");
break;
}
}
}
}
안하려다가....... 만드니까 재밌긴하네
Exercise
1.
1) Student, GraduateStudent
2) (생략)
3)
class Student {
private int number;
protected String name;
int getNumber() { return number; }
void setNumber(int n) { this.number = n; }
String getName() { return name; }
void getName(String s) { this.name = s; }
}
4)
class Student {
...
public Student() { System.out.println("부모클래스 생성"); }
}
public class GraduateStudent extends Student {
...
public GraduateStudent() {
super(); //명시적인 호출
System.out.println("자식클래스 생성");
}
}
5)
① number가 private로 선언되었으므로 자식 클래스에서는 사용할 수 없다.
2.
클래스 A 생성자
클래스 B 생성자
클래스 C 생성자
-> 부모클래스의 생성자부터 먼저 생성된다.
3.
1) y, z, w
2) ② 부모 클래스 참조 변수로 자식클래스 객체를 가르킬 수 있지만, 그 반대(하향형변환)은 컴파일 에러 발생
4.
묵시적인 부모클래스 생성자 호출을 사용하려면 부모 클래스에 매개변수가 없는 생성자(기본 생성자)가 반드시 정의되어 있어야 한다.
* 이유 : 묵시적인 호출은 기본 생성자를 호출하는데 매개변수가 있는 생성자를 만들었다면 기본 생성자가 없는게 되므로 호출할 수 없어서...?
5.
1) B
2) B
3) super.print();
6.
추상클래스를 상속받는 자식클래스는 추상메소드를 재정의 해야된다.
클래스 B에 추상메소드인 print()를 재정의 하는 것이 필요
Programming Exercise
1.
class Circle {
protected int radius;
public Circle(int r) { radius = r; }
}
class Pizza extends Circle {
String s;
public Pizza(String s, int r) {
super(r);
this.s = s;
}
void print() {
System.out.printf("피자의 종류: %s, 피자의 크기: %d", s, radius);
}
}
public class ch6_1 {
public static void main(String[] args) {
Pizza obj = new Pizza("Pepperoni", 20);
obj.print();
}
}
2.
class Animal {
void walk() {
System.out.println("걸을 수 있음");
}
}
class Bird extends Animal {
void fly() {
System.out.println("날을 수 있음");
}
void sing() {
System.out.println("노래 부를 수 있음");
}
}
public class ch6_2 {
public static void main(String[] args) {
Bird bird = new Bird();
bird.walk();
bird.fly();
bird.sing();
}
}
3.
class Bird inplements Animal {
void walk() {
System.out.println("걸을 수 있음");
}
void fly() {
System.out.println("날을 수 있음");
}
void sing() {
System.out.println("노래 부를 수 있음");
}
}
4.
class Sports {
String getName() { return "아직 결정되지 않음"; }
int getPlayers() { return 0; }
}
class Soccer extends Sports {
String getName() { return "축구"; }
int getPlayers() { return 11; }
}
public class ch6_4 {
public static void main(String[] args) {
Soccer obj = new Soccer();
System.out.printf("경기이름: %s \n경기자수: %d", obj.getName(), obj.getPlayers());
}
}
5.
class ColorRectangle extends Rectangle {
String color;
public ColorRectangle(int width, int height, String color) {
super(width, height);
this.color = color;
}
}
728x90
'Major > Java' 카테고리의 다른 글
[자바로 배우는 리팩토링 입문] 0장 리팩토링이란 (0) | 2022.04.13 |
---|---|
[Power Java Compact] 7장 연습문제 (0) | 2022.01.05 |
[Power Java Compact] 5장 연습문제 (0) | 2021.12.22 |
[Power Java Compact] 4장 연습문제 (3) | 2021.12.20 |
[Java] 문자열 안에서 단어찾기 (0) | 2021.12.20 |