메소드 오버라이딩이란, 부모 클래스에서 정의된 메소드를
자식 클래스에서 상속받은 메소드를 새로 정의해서 사용하는것을 말한다.
오버로딩은 같은 메소드명을 서로 다른 자료형과
파라미터의 개수로 구분해서 다중 정의하는것을 뜻하고,
오버라이딩은 같은 메소드명(같은 자료형과 파라미터의 개수)
을 가진 부모 클래스와 자식 클래스에서 재정의하는것을 뜻한다.
@OverRide는 딱히 적어주지 않아도 자동적으로 오버라이딩은 되나,
오버라이딩 된 함수라는것을 표시해주는 기호이다.
instanceOf 연산자는 객체가 어떤 클래스인지, 어떤 클래스를 상속받고 있는지 확인하는데 사용하는 연산자이다.
사용할 예제
Shape[] shape = {new Circle(10),new Triangle(10, 10),
new Rectangle(10, 10)};
for (Shape s : shape) {
sumArea += s.getArea();
}
소스코드
package timeattack1109;
class Shape {
double getArea() {return 0;}
}
class Circle extends Shape {
int radius;
Circle(int radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius *Math.PI;
}
}
class Triangle extends Shape {
int width;
int height;
Triangle(int width, int height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height / 2.0;
}
}
class RectAngle extends Shape {
int width;
int height;
RectAngle(int width, int height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
}
public class Shapes {
public static void main(String[] args) {
double sumArea=0;
Shape[] shape = { new Circle(10), new Triangle(10, 10), new RectAngle(10, 10) };
for (Shape s : shape) {
sumArea += s.getArea();
}
System.out.println(sumArea);
}
}
출력 결과
464.1592653589793
6.아래를 프로그래밍 하시오.
겜블링 게임에 참여할 선수 숫자>>3
1번째 선수 이름>>영희
2번째 선수 이름>>철수
3번째 선수 이름>>길동
[영희]:<Enter>
3 3 2 아쉽군요!
[철수]:<Enter>
3 3 2 아쉽군요!
[길동]:<Enter>
1 1 1 길동님이 이겼습니다!
Process finished with exit code 0
package gwajeyong;
import java.util.Scanner;
class Player {
private String name; // Player 이름
private int[] arrNum; // 3개의 수를 저장해둘 배열
public Player(String name) {
this.name = name;
}
public String getName() {
return name; // private로 선언된 name을 가져올 함수
}
public boolean game() {
arrNum = new int[3];
for (int i = 0; i < arrNum.length; i++) {
arrNum[i] = (int) (Math.random() * 3 + 1); // 랜덤으로 3개의 값 넣어줌
}
System.out.print(arrNum[0] + " " + arrNum[1] + " " + arrNum[2] + " ");
if ((arrNum[0] == arrNum[1]) && (arrNum[1] == arrNum[2])) {
return true; // 세 숫자가 모두 같은지 검사
}
return false;
}
}
public class GambleGameTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("겜블링 게임에 참여할 선수 숫자>>");
int playerCount = scanner.nextInt();
Player[] Players = new Player[playerCount];
for (int i = 0; i < playerCount; i++) {
Scanner sc = new Scanner(System.in);
System.out.print("이름을 입력해주세요 >> ");
String name = sc.nextLine();
System.out.println((i + 1) + "번째 선수 이름>>" + name);
Players[i] = new Player(name);
}
String buffer = scanner.nextLine();
while (true) {
boolean game_win = false;
for (int j = 0; j < playerCount; j++) {
System.out.print("[" + Players[j].getName() + "]:<Enter>");
buffer = scanner.nextLine();
if (Players[j].game()) {
System.out.println(Players[j].getName() + "님이 이겼습니다.");
game_win = true;
break;
}
System.out.println("아쉽군요!");
}
if(game_win) {
break;
}
}
}
}![](https://velog.velcdn.com/images/alice_hulotte/post/80ae8d3a-72a9-4239-a4bc-8e9c6392e0fa/image.png)