lundi 24 octobre 2016

Getting the class name of an object in strategy design pattern

i am trying to understand the design pattern, and i need to know how can i retrieve the name of the class in the implemented interface class, as follow:

public interface Flys {

   String fly();

}

// Class used if the Animal can fly

class ItFlys implements Flys{

    public String fly() {

        return getClass().getName()+" is Flying High"; //not working...gives me the ItFlys class name not Dog class name

    }

}

//Class used if the Animal can't fly

class CantFly implements Flys{

    public String fly() {

        return "I can't fly";

    }

}

Code from: http://ift.tt/1NatbMm Thanks

Aucun commentaire:

Enregistrer un commentaire