[NHN Academy]
생성자, 다형성
jnk1m
2022. 7. 22. 08:45
생성자
class Date{
private int year, month, day;
String dayOfWeek;
boolean holiday;
public int getYear(){
return this.year;
}
public int getMonth(){
return this.month;
}
public int getDay(){
return this.day;
}
public Date(int year, int month, int day){
this.year=year;
this.month = month;
this.day = day;
}
}
Date date = new Date(1993,10,05);
date.getDay();
다형성
interface BinaryOp{
public int apply(int right, int left);
}
public class Adder implements BinaryOp{
public int apply(int right, int left){
return right+left;
}
}
//다형성 구현
public class Multiplier implements BinaryOp{
public int apply(int right, int left){
return right*left;
}
}
//오..!! 서브 타입이라서. 인터페이스가 객체로 형변환 되는거라서
void calc(BinaryOp binder, int i ,int j){
System.out.println(binder.apply(i,j));
}
calc(new Adder(),1,2);
calc(new Multiplier(),1,2);