Frog is cry
오버라이딩(day20) 본문
package day21.ex04_Override;
public class Airplane {
public void land() {
System.out.println("착륙합니다.");
}
public void fly() {
System.out.println("일반비행합니다.");
}
public void takeoff() {
System.out.println("이륙합니다.");
}
}
package day21.ex04_Override;
public class SupersonicAirplane extends Airplane{
public static final int NORMAL = 1;
public static final int SUPERSONIC = 2;
public int flyMode = NORMAL;
@Override
public void fly() {
if(flyMode == SUPERSONIC) {
System.out.println("초속비행합니다.");
}else {
super.fly();
}
}
}
package day21.ex04_Override;
public class SupersonicAirplane_Ex {
public static void main(String[] args) {
SupersonicAirplane sa = new SupersonicAirplane();
sa.takeoff();
sa.fly();
sa.flyMode = SupersonicAirplane.SUPERSONIC;
sa.fly();
sa.flyMode = SupersonicAirplane.NORMAL;
sa.fly();
sa.land();
}
}
package day21.ex04_Override;
public class Car {
String name;
int wheel;
void run() {
System.out.println("평범한 차가 출발합니다.");
}
}
package day21.ex04_Override;
public class SportsCar extends Car{
int select;
String name;
@Override
void run() {
if(select == 1) {
this.name = "스포츠카가 출발합니다";
System.out.println(this.name);
}else if(select == 2) {
super.run();
}
}
}
package day21.ex04_Override;
public class SportsCar_Ex {
public static void main(String[] args) {
SportsCar c1 = new SportsCar();
c1.select = 1;
c1.run();
}
}
package day21.ex04_Override;
public class Calculator {
double areaCircle(double r) {
System.out.println("Calculator 객체의 areaCirlce() 실행");
return 3.14159 * r * r;
}
}
package day21.ex04_Override;
public class Computer extends Calculator{
@Override
double areaCircle(double r) {
System.out.println("Computer 객체의 areaCircle() 실행");
return Math.PI * r * r;
}
}
package day21.ex04_Override;
public class Computer_Ex {
public static void main(String[] args) {
int r = 10;
Calculator calc = new Calculator();
System.out.println("원면적 : " + calc.areaCircle(r));
System.out.println();
Computer com = new Computer();
System.out.println("원면적 : " + com.areaCircle(r));
}
}
'JAVA > 국비수업' 카테고리의 다른 글
protected(day21) (0) | 2020.07.28 |
---|---|
final(day21) (0) | 2020.07.28 |
super(day20) (0) | 2020.07.27 |
상속(day20) (0) | 2020.07.27 |
이전복습 (day19) (0) | 2020.07.24 |
Comments