(day8) The Interface --- Data Callback2

Rent a house through an agent

A further comprehence about Interface & polimorphism

public class Agent {
    //Uniformly tenant(房客)
    private OnAgentFinishListener listener;
    // The tenant's name
    public void setListener(OnAgentFinishListener listener){
        this.listener = listener;
    }

    //Begin to rent a house
    public void rentHouse(){
    System.out.println("I'm the agent,who is going to rent a house for you");
    System.out.println("Here finds the house!");

    // Return the house
    listener.onFinish("Sea-view room");
    }


    // Return data uniformly
    public interface OnAgentFinishListener{
        void onFinish(String des);
    }
}
public class Person implements Agent.OnAgentFinishListener {
    private String reply;

    public void needOneHouse() {

        //find an agent to rent a house
        Agent xw = new Agent();
        // Who 's going to rent a house
        xw.setListener(this);
        //Begin to rent a house
        xw.rentHouse();

    }


    @Override
    public void onFinish(String des) {
        Scanner sIn = new Scanner(System.in);
        System.out.println(sIn.nextLine() + " " + des);
    }
}
public class MyClass {
    public static void main(String[] args) {
        Person xd = new Person();
        xd.needOneHouse();
    }
}

你可能感兴趣的:((day8) The Interface --- Data Callback2)