I have two classes i.e ChildOne & ChildTwo which are extending base class naed Base and a enum named AgeEnum, there is one method i.e getAge() is common in child classes, Now I want to move that common method in the base class also I want do sothing so that when I call getAge() on any of the objects (ex - ChildOne.getAge()) it should return age of that child define in enum.
-
Base Class
public class BaseClass { ***// Define getAge() here somehow to get the dynamic value of the*** } -
Child One Class
public class ChildOne extends BaseClass { public int getAge(){ return AgeEnum.ChildAge.CHILD_ONE.getValue(); } } -
Child Two Class
public class ChildTwo extends BaseClass { public int getAge(){ return AgeEnum.ChildAge.CHILD_TWO.getValue(); } } -
Enum
public class AgeEnum { public enum ChildAge { CHILD_ONE(12), CHILD_TWO(19); private ChildAge(final int text) { this.text = text; } private final int text; public int getValue() { return text; } } }
So far I have analyzed that if we could do something like this, but how to configure this in enum call, in this case if I call getAge() of child class I will get the value from enum all I have to do is identify the child class and pass enum constant if that class at run time (may be I will get the class instance from the context and get the class name using reflection) :
> public class BaseClass {
> public int getAge(){
> return AgeEnum.ChildAge.<dynamicAgeConstent>.getValue();
> }
> }
Aucun commentaire:
Enregistrer un commentaire