本文介绍了我如何获得这个嵌套泛型参数系统的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让一个相当复杂的系统工作。以下是我正在尝试的基本知识。



规则:

 抽象类Rule 
{
// stuff
}

class ExampleRule extends Rule
{
// stuff
}

处理程序:

 抽象类RuleHandler< T extends Rule> 
{
Class< T> clazz中;
RuleHandler(Class< T> forClass)
{
this.clazz = forClass;
}
abstract void doStuff(T rule);
}

class ExampleRuleHandler扩展了RuleHandler< ExampleRule>
{
ExampleRuleHandler()
{
super(ExampleRule.class);
}
void doStuff(ExampleRule rule)
{
// stuff
}
}

将它们绑在一起:

  class HandlerDispatcher 
{
地图< Class< ;?扩展Rule>,RuleHandler<扩展规则>>处理程序;
void register(RuleHandler< ;? extends Rule> handler)
{
handlers.put(handler.clazz,handler); (规则规则:规则)
{
RuleHandler< ;?

void doStuff(List< Rule>规则)
{
扩展规则> handler = handlers.get(rule.getClass());
handler.doStuff(rule);



$ b class Test
{
void main()
{
HandlerDispatcher hd =新的HandlerDispatcher();
hd.register(new ExampleRuleHandler());


$ / code>

到目前为止,我尝试过不同参数的各种组合(通配符,限制等),还没有得到这个编译没有类型相关的错误。任何见解,解决方案或替代方法都是受欢迎的。

解决方案

观察结果


  • 处理程序是private和final

  • handler.clazz是final

  • RuleHandler< T extends Rule> 隐含 RuleHandler<?> === RuleHandler

  •  抽象类RuleHandler< T extends Rule> 
    {
    final Class< T> clazz中;
    //像前面一样
    }

    class HandlerDispatcher
    {
    private final Map< Class<?>,RuleHandler<>>处理程序;
    void register(RuleHandler<> handler)
    {
    handlers.put(handler.clazz,handler);

    void doStuff(List< Rule> rules)
    {
    for(Rule rule:rules)
    {
    @SuppressWarnings(unchecked)
    RuleHandler< Rule> handler =(RuleHandler< Rule>)handlers.get(rule.getClass());
    handler.doStuff(rule);



    $ b class Test
    {
    void main()
    {
    HandlerDispatcher hd =新的HandlerDispatcher();
    hd.register(new ExampleRuleHandler());

    RuleHandler<?> handler = new ExampleRuleHandler();
    hd.register(handler);
    }
    }


    So I'm trying to get a reasonably complicated system working. Here's the basics of what I'm attempting.

    Rules:

    abstract class Rule
    {
      // stuff
    }
    
    class ExampleRule extends Rule
    {
      // stuff
    }
    

    Handlers:

    abstract class RuleHandler<T extends Rule>
    {
      Class<T> clazz;
      RuleHandler(Class<T> forClass)
      {
        this.clazz = forClass;
      }
      abstract void doStuff(T rule);
    }
    
    class ExampleRuleHandler extends RuleHandler<ExampleRule>
    {
      ExampleRuleHandler()
      {
        super(ExampleRule.class);
      }
      void doStuff(ExampleRule rule)
      {
        // stuff
      }
    }
    

    And tying them together:

    class HandlerDispatcher
    {
      Map<Class<? extends Rule>, RuleHandler<? extends Rule>> handlers;
      void register(RuleHandler<? extends Rule> handler)
      {
        handlers.put(handler.clazz, handler);
      }
      void doStuff(List<Rule> rules)
      {
        for(Rule rule : rules)
        {
          RuleHandler<? extends Rule> handler = handlers.get(rule.getClass());
          handler.doStuff(rule);
        }
      }
    }
    
    class Test
    {
      void main()
      {
        HandlerDispatcher hd = new HandlerDispatcher();
        hd.register(new ExampleRuleHandler());
      }
    }
    

    So far I've attempted various combinations of different parameters (wildcarded, restricted, etc.) and have yet to get this compiling without type-related errors. Any insights, solutions or alternative approaches are welcome.

    解决方案

    Observing that

    • handlers is private and final
    • handler.clazz is final
    • RuleHandler<T extends Rule> implies RuleHandler<?> === RuleHandler<? extends Rule>

    the following code is correct (and compiles)

    abstract class RuleHandler<T extends Rule>
    {
      final Class<T> clazz;
      // as before
    }
    
    class HandlerDispatcher
    {
      private final Map<Class<?>, RuleHandler<?>> handlers;
      void register(RuleHandler<?> handler)
      {
        handlers.put(handler.clazz, handler);
      }
      void doStuff(List<Rule> rules)
      {
        for(Rule rule : rules)
        {
          @SuppressWarnings("unchecked")
          RuleHandler<Rule> handler = (RuleHandler<Rule>) handlers.get(rule.getClass());
          handler.doStuff(rule);
        }
      }
    }
    
    class Test
    {
      void main()
      {
        HandlerDispatcher hd = new HandlerDispatcher();
        hd.register(new ExampleRuleHandler());
    
        RuleHandler<?> handler = new ExampleRuleHandler();
        hd.register(handler);
      }
    }
    

    这篇关于我如何获得这个嵌套泛型参数系统的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:16