티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/82
https://mrcocoball.tistory.com/83
interface 인터페이스 이름{
public static final float pi = 3.14F;
public void makeSomething();
}
public interface Calc {
double PI = 3.14; // 상수 필드 선언
int ERROR = -99999999; // 상수 필드 선언
// 추상 메소드
int add(int num1, int num2);
int substract(int num1, int num2);
int time(int num1, int num2);
int divide(int num1, int num2);
}
//추상 클래스, Calc를 implement
public abstract class Calculator implements Calc {
@Override // 추상 메소드 재정의하여 구체화
public int add(int num1, int num2) {
return num1 + num2;
}
@Override // 추상 메소드 재정의하여 구체화
public int substract(int num1, int num2) {
return num1 - num2;
}
}
public class CompleteCalc extends Calculator { // Calculator를 상속
@Override // 추상 메소드 재정의하여 구체화
public int time(int num1, int num2) {
return num1 * num2;
}
@Override // 추상 메소드 재정의하여 구체화
public int divide(int num1, int num2) {
if (num2 == 0)
return ERROR;
else
return num1 / num2;
}
public void showInfo() {
System.out.println("모두 구현 완료");
}
}
public class CalculatorTest {
public static void main(String[] args) {
Calc calc = new CompleteCalc(); // 인터페이스를 구현한 클래스는 인터페이스 형으로 선언한 변수로 형변환 가능
int num1 = 10;
int num2 = 2;
System.out.println(num1 + "+" + num2 + "=" + calc.add(num1, num2));
System.out.println(num1 + "-" + num2 + "=" + calc.substract(num1, num2));
System.out.println(num1 + "*" + num2 + "=" + calc.time(num1, num2));
System.out.println(num1 + "/" + num2 + "=" + calc.divide(num1, num2));
}
}
Calc calc = new CompleteCalc();
Calculator implements Calc, Calc2, Calc3 ...
public interface Calc {
double PI = 3.14; // 상수 필드 선언
int ERROR = -99999999; // 상수 필드 선언
// 추상 메소드
int add(int num1, int num2);
int substract(int num1, int num2);
int time(int num1, int num2);
int divide(int num1, int num2);
default void description() {
System.out.println("정수의 사칙연산을 제공합니다.");
myMethod(); // 재정의 불가
}
static int total(int[] arr) { // 배열을 받아서 배열 내부 저장
int total = 0;
for (int num : arr) {
total += num;
}
myStaticMethod(); // 재정의 불가
return total;
}
private void myMethod() {
System.out.println("myMethod");
}
private static void myStaticMethod() {
System.out.println("myStaticMethod");
}
}
Calc.정적 메소드로 인스턴스 생성 없이 바로 Calc의 정적 메소드를 호출하는 코드
int[] arr = {1,2,3,4,5};
Calc.total(arr); // 정적 메소드, 구현 클래스 인스턴스 생성 없이도 가능, 인터페이스 이름.정적 메소드로 호출
System.out.println(Calc.total(arr));
public interface Buy {
void buy();
default void order() {
System.out.println("buy order");
}
}
public interface Sell {
void sell();
default void order( ) {
System.out.println("sell order");
}
}
public class Customer implements Buy, Sell {
@Override
public void sell() {
System.out.println("판매합니다");
}
@Override
public void buy() {
System.out.println("구매합니다");
}
@Override
public void order() {
System.out.println("주문합니다");
}
public void hello() {
System.out.println("안녕");
}
}
public class CustomerTest {
public static void main(String[] args) {
Customer customer = new Customer();
customer.buy();
customer.sell();
customer.order();
customer.hello();
Buy buyer = customer;
buyer.buy();
buyer.order(); // Customer에서 재정의했으므로 Buy의 order 호출 안됨
Sell seller = customer;
seller.sell();
seller.order(); // Customer에서 재정의했으므로 Sell의 order 호출 안됨
}
}
public class Shelf {
protected ArrayList<String> shelf; // ArrayList 타입의 변수 shelf
public Shelf() {
shelf = new ArrayList<String>(); // shelf 라는 이름으로 ArrayList 생성
}
public ArrayList<String> getShelf() { // getShelf() 로 ArrayList 타입의 shelf 리턴
return shelf;
}
public int getCount() {
return shelf.size(); // shelf의 길이를 리턴
}
}
public interface Queue {
void enQueue(String title); // 책 이름 title을 적어서 책을 보관
String deQueue(); // 책의 이름 String 타입으로 꺼냄
int getSize();
}
public class BookShelf extends Shelf implements Queue {
@Override
public void enQueue(String title) {
shelf.add(title); // title을 받아서 책을 저장
}
@Override
public String deQueue() {
return shelf.remove(0); // 맨 첫번째 책을 지우면서 책 이름을 반환
}
@Override
public int getSize() {
return getCount();
}
}
public class BookShelfTest {
public static void main(String[] args) {
Queue bookQueue = new BookShelf(); // 인터페이스 타입으로 BookShelf 형 변환
bookQueue.enQueue("러브라이브1");
bookQueue.enQueue("러브라이브2");
bookQueue.enQueue("러브라이브3");
bookQueue.enQueue("러브라이브4");
bookQueue.enQueue("러브라이브5");
System.out.println(bookQueue.getSize());
System.out.println(bookQueue.deQueue());
System.out.println(bookQueue.deQueue());
System.out.println(bookQueue.deQueue());
System.out.println(bookQueue.deQueue());
System.out.println(bookQueue.deQueue());
}
}