public interface MyInterFace{
public abstract void show();
}
public class useInterFace implements MyInterFace{
public void show(){
System.out.println("Hello");
}
}
public class Person{
private MyInterFace myinterface;
Person(){}
Person(MyInterFace myinterface){
this.myinterface = myinterface;
}
public void setInterface(MyInterFace myinterface){
this.myinterface = myinterface;
}
public MyInterFace getInterface(){
return myinterface;
}
public void show(){
myinterface.show();
}
}
public class MyMain{
public static void main(String[] args){
Person person = new Person();
//使用单独定义的实现类
person.setInterface(new useInterFace());
//使用匿名内部类
MyInterFace myinterface = new MyInterFace(){
System.out.println("Hello,World");
};
person.setInterface(myinterface);
//进一步简化
person.setInterface(new MyInterFace(){
System.out.println("Hello,World");
});
person.show();
}
}