JAVA

인터페이스를 합치는 작업(andThen,compose)

웹개발자준비 2018. 8. 10. 14:20

//인터페이스를 합치는 작업

- 여러 함수형 인터페이스를 하나의 인터페이스로 합치는 작업


1.andThen() : 그리고, 그런 다음

- A.andThen(B) => A실행 후 B실행


2.compose() : 구성하다

- A.compose(B) => B실행 후 A실행


----------------------------------------------------------------------------------------------


//Mouse 클래스

class Mouse implements Comparable{

private String name;

private int price;

public Mouse() {}

public Mouse(String name,int price) {

this.name = name;

this.price = price;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getPrice() {

return price;

}

public void setPrice(int price) {

this.price = price;

}

@Override

public String toString() {

return this.name+ " : " + this.price;

}

@Override

public int compareTo(Object m) { //본인과 다른 어떤것을 비교하기 때문에 

if(m instanceof Mouse) {

Mouse m2 =(Mouse)m;

if(this.name.equals(m2)&&this.price==m2.price) {

return 1;

}else {

return -1;

}

}else {

return -1;

}

}

}


//Student 클래스

class Student{

private String name;

private int kor;

private int eng;

private int math;

public Student() {}

public Student(String name,int kor, int eng, int math) {

this.name = name;

this.kor = kor;

this.eng = eng;

this.math = math;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getKor() {

return kor;

}

public void setKor(int kor) {

this.kor = kor;

}

public int getEng() {

return eng;

}

public void setEng(int eng) {

this.eng = eng;

}

public int getMath() {

return math;

}

public void setMath(int math) {

this.math = math;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return this.name + (this.kor+this.eng+this.math);

}

}


----------------------------------------------------------------------------------------------


요구사항] m1의 이름과 가격을 출력하시오.


Consumer<Mouse> c1 = m -> System.out.println(m.getName());
Consumer<Mouse> c2 = m -> System.out.println(m.getPrice());


Mouse m1 = new Mouse("광마우스",30000);


c1.accept(m1);

c2.accept(m1);