*content
λ€μν μκ³ λ¦¬μ¦μ κ°κ°μ ν΄λμ€λ‘ μΊ‘μννμ¬ νλμ μΆμμ μΈ μ κ·Όμ μ μμ, ν΄λΌμ΄μΈνΈκ° μ μ ν μ λ΅μ μ·¨μ¬ μ ννμ¬ λ‘μ§μ μνν μ μλ ν¨ν΄
Tools
public interface Tools{
public void write();
}
Pencil
public class Pencil implements Tools{
@Override
public void write(){
System.out.println("By pencil");
}
}
BallpointPen
public class BallpointPen implements Tools{
@Override
public void write(){
System.out.println("By BallpointPen");
}
}
FountainPen
public class FountainPen implements Tools{
@Override
public void write(){
System.out.println("By FountainPen");
}
}
Student
public class Student{
private Tools tools;
public void change(Tools tools){
this.tools = tools;
}
public void write(){
if(tools == null){
System.out.println("without tools");
}else{
tools.write();
}
}
}
Main
public class main{
publi static void main(String[] args){
Student student = new Student();
student.write(); //without tools
student.change(new Pencil());
student.write(); //by pencil
student.change(new BallpointPen());
student.write(); //by ballpointpen
}
}
μ₯μ
- νμ₯μ±