abstraact 추상클래스명{
abstract 추상메소드명();
};
추상메소드는 자식클래스에서 강제적으로 반드시 재정의 해야한다.
넓이를 무조건 재정의 하여 사용해야하는 모양 클래스를 만들고 각각 네모 클래스, 세모 클래스를 만들어 보자
1. abstract를 붙인 클래스를 만들어주고 재정의 해야하는 메소드를 만들면 된다. 우리는 showArea라는 메서드를 매개변수로 가로 길이와 세로길이를 받고 각각 실수형으로 받자.
public abstract class Shape {
//실수형을 받기 위해 실수형 초기화
double area;
//무조건 재정의 해야하는 메소드
abstract void showArea(double w, double h);
//재정의 되는 것을 막아서 사용만 해야되는 메소드
final void function(){
System.out.println("추상 클래스의 일반 메서드입니다아아아아아");
}
}
class Rect extends Shape{
@Override
void showArea(double w, double h) {
area = w*h; //넓이
System.out.println("넓이" + area+ "cm²");
}
}
class Tri extends Shape{
@Override
void showArea(double w, double h) {
area = (w*h)/2;
System.out.println("넓이" + area+ "cm²");
}
}
public class Board {
public static void main(String[] args) {
//첫번째 방법
Rect re = new Rect();
re.showArea(10, 50);
//new 자식클래스명()를 사용하여 바로 값을 출력
//new Rect().showArea(10, 50);
new Tri().showArea(5, 1);
}
}
public abstract class Public {
abstract void go();
abstract void pay();
abstract void destMsg();
}
package tms;
public class Bus extends Public{
@Override
void go() {
}
@Override
void pay() {
}
@Override
void destMsg() {
}
}
Public 클래스
abstract int pay(int money);
bus 클래스
@Override
int pay(int money) {
money -= feed;
return money;
}
public 클래스
String [] arStation = {"덕소", "상봉", "한남","강남"};
bus클래스
@Override
String go(int btnIndex, int money) {
int index=0;
Random r = new Random();
index = r.nextInt(arStation.length);
}
@Override
String go(int btnIndex, int money) {
int index=0;
String result = "";
Random r = new Random();
while(true) {
index = r.nextInt(arStation.length); //출발지
if(index < btnIndex) break; //index가 btnIndex보다 작아야 랜덤이 결정된다
}
if(money-feed < 0) {
result = "잔액부족";
}else {
}
}
String go(int btnIndex, int money) {
int index=0;
int cnt = 0;
String result = "";
//btnIndex : 목적지, index : 출발지
Random r = new Random();
while(true) {
index = r.nextInt(arStation.length); //출발지
if(index < btnIndex) break; //index가 btnIndex보다 작아야 랜덤이 결정된다
}
cnt = btnIndex-index;
if(money-feed < 0) {
result = "잔액부족";
}else {
result = "" + pay(money) + ",";
result += arStation[index] + ">";
for (int i = 0; i < cnt; i++) {
result += i == cnt-1 ? arStation[index+i+1]: arStation[index+i+1]+">";
}
}
return result;
}
..생략..(go메소드)
}
destMsg(arStation[btnIndex]);
}
return result;
}
@Override
void destMsg(String destination) {
JOptionPane.showMessageDialog(null, destination + "에 도착하였습니다" );
}
public class Traffic {
public static void main(String[] args) {
Bus bus = new Bus();
String [] arTraffic = {"버스", "지하철"};
int choice = 0;
int money = 10000;
while(true) {
choice = JOptionPane.showOptionDialog(null, "대중교통을 선택해주세요", "TMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, arTraffic, null);
if(choice==-1) break;
int btnIndex = JOptionPane.showOptionDialog(null, "도착지를 선택하세요", "TMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, bus.arStation, null);
//버스일때 덕소도착은 막아야겠다
if(choice == 0 && btnIndex ==0) {
JOptionPane.showMessageDialog(null, "도착지는 덕소 다음 정류장부터 가능합니다.");
continue;
}
switch(choice) {
case 0 :
break;
case 1 :
break;
}
}
}
}
String [] result = new String [2];
case 0 :
result = bus.go(btnIndex, money).split(",");
if(result[0].equals("잔액부족")) {
JOptionPane.showMessageDialog(null, "잔액이 부족합니다");
}else {
money = Integer.parseInt(result[0]);
JOptionPane.showMessageDialog(null, "남은 잔액 : " +money + "\n" + result[1]);
}
break;
package tms;
import java.util.Random;
import javax.swing.JOptionPane;
public class Subway extends Public{
int feed = 1500;
@Override
String go(int btnIndex, int money) {
int index=0;
int cnt = 0;
String result = "";
//btnIndex : 목적지, index : 출발지
Random r = new Random();
while(true) {
index = r.nextInt(arStation.length); //출발지
if(index == btnIndex) System.out.println("출발지와 목적지가 같습니다. \n 다시 실행됩니다");
if(index != btnIndex) break; //index가 btnIndex보다 작아야 랜덤이 결정된다
}
cnt = btnIndex-index;
if(money-feed < 0) {
result = "잔액부족";
}else {
result = "" + pay(money) + ",";
result += arStation[index] + ">";
if(cnt > 0) {
for (int i = 0; i < cnt; i++) {
result += i == cnt-1 ? arStation[index+i+1]: arStation[index+i+1]+">";
}
}else {
cnt *= -1;
for (int i = 0; i <cnt; i++) {
result += i == cnt -1 ? arStation[index-i-1] : arStation[index-i-1] + ">";
}
}
destMsg(arStation[btnIndex]);
}
return result;
}
@Override
int pay(int money) {
money -= feed;
return money;
}
@Override
void destMsg(String destination) {
JOptionPane.showMessageDialog(null, destination + "에 도착하였습니다" );
}
}
Subway sub = new Subway();
case 1 :
result = sub.go(btnIndex, money).split(",");
if(result[0].equals("잔액부족")) {
JOptionPane.showMessageDialog(null, "잔액이 부족합니다");
}else {
money = Integer.parseInt(result[0]);
JOptionPane.showMessageDialog(null, "남은 잔액 : " +money + "\n" + result[1]);
}
break;
package tms;
import javax.swing.JOptionPane;
public class Traffic {
public static void main(String[] args) {
Bus bus = new Bus();
Subway sub = new Subway();
String [] arTraffic = {"버스", "지하철"};
int choice = 0;
int money = 10000;
String [] result = new String [2];
while(true) {
choice = JOptionPane.showOptionDialog(null, "대중교통을 선택해주세요", "TMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, arTraffic, null);
if(choice==-1) break;
int btnIndex = JOptionPane.showOptionDialog(null, "도착지를 선택하세요", "TMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, bus.arStation, null);
//버스일때 덕소도착은 막아야겠다
if(choice == 0 && btnIndex ==0) {
JOptionPane.showMessageDialog(null, "도착지는 덕소 다음 정류장부터 가능합니다.");
continue;
}
switch(choice) {
case 0 :
result = bus.go(btnIndex, money).split(",");
if(result[0].equals("잔액부족")) {
JOptionPane.showMessageDialog(null, "잔액이 부족합니다");
}else {
money = Integer.parseInt(result[0]);
JOptionPane.showMessageDialog(null, "남은 잔액 : " +money + "\n" + result[1]);
}
break;
case 1 :
result = sub.go(btnIndex, money).split(",");
if(result[0].equals("잔액부족")) {
JOptionPane.showMessageDialog(null, "잔액이 부족합니다");
}else {
money = Integer.parseInt(result[0]);
JOptionPane.showMessageDialog(null, "남은 잔액 : " +money + "\n" + result[1]);
}
break;
}
}
}
}
package tms;
import java.util.Random;
import javax.swing.JOptionPane;
public class Bus extends Public{
int feed = 1200;
@Override
String go(int btnIndex, int money) {
int index=0;
int cnt = 0;
String result = "";
//btnIndex : 목적지, index : 출발지
Random r = new Random();
while(true) {
index = r.nextInt(arStation.length); //출발지
if(index < btnIndex) break; //index가 btnIndex보다 작아야 랜덤이 결정된다
}
cnt = btnIndex-index;
if(money-feed < 0) {
result = "잔액부족";
}else {
result = "" + pay(money) + ",";
result += arStation[index] + ">";
for (int i = 0; i < cnt; i++) {
result += i == cnt-1 ? arStation[index+i+1]: arStation[index+i+1]+">";
}
destMsg(arStation[btnIndex]);
}
return result;
}
@Override
int pay(int money) {
money -= feed;
return money;
}
@Override
void destMsg(String destination) {
JOptionPane.showMessageDialog(null, destination + "에 도착하였습니다" );
}
}