“Sets” of a particular enum type but with generics

后端 未结 4 1987
北荒
北荒 2021-01-31 16:24

Let\'s say I have an abstract class

public abstract class Trainer{}

I have specific trainers like :

pub         


        
4条回答
  •  旧时难觅i
    2021-01-31 16:35

    Despite design analysis, I think the most your-situation (closest to your already created design) answer is:

    • your DogTrainer.Trainables is TrainingActions
    • your HorseTrainer.Trainables is TrainingActions

    Generally, your Trainables is already a TrainingActions

    so you can just provide Set> completed, because you already have your animal information in T:

    abstract class Trainer {
        Set> completed = new HashSet>();
    
        public void add(TrainingActions t ) {
            completed.add( t );
        }
    }
    

    And you have exactly what you want.

    Usage:

    DogTrainer tr = new DogTrainer();
    tr.add( DogTrainer.Trainables.BITE );
    

    If you are sure you want to enforce an Enum for your interface:

    public  & TrainingActions> void add( E t ) {
       completed.add( t );
    }
    

提交回复
热议问题