【问题标题】:I can't understand casting interface to children that don't have any parents我无法理解没有父母的孩子的投射界面
【发布时间】:2021-07-04 22:05:03
【问题描述】:

我正在学习 lambda,然后是 WebClient 类 所以我正在创建 WebClient 的 bean

ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder().build();
    
exchangeStrategies.messageWriters() 
                      .stream()
                      .filter(LoggingCodecSupport.class::isInstance)
                      .forEach(writer -> ((LoggingCodecSupport)writer).setEnableLoggingRequestDetails(true));

我认为顶部代码和底部代码是相同的

    ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder().build();
    
    exchangeStrategies.messageWriters()
                      .stream()
                      .filter(new Predicate<HttpMessageWriter>() {
                          public boolean test(HttpMessageWriter t) {
                              return LoggingCodecSupport.class.isInstance(t);
                          };
                      })
                      .forEach(new Consumer<HttpMessageWriter>() {
                            public void accept(HttpMessageWriter t) {
                                ((LoggingCodecSupport)t).setEnableLoggingRequestDetails(true);
                            };
                      }); 

我不明白这种转换情况,因为 HttpMessageWriter 是接口 和 LoggingCodecSupport 是类但 LoggingCodecSupport 没有实现 HttpMessageWriter,所以我认为 HttpMessageWriter 不能转换为 LoggingCodecSupport

.forEach(new Consumer<HttpMessageWriter>() {
        public void accept(HttpMessageWriter t) {
                ((LoggingCodecSupport)t).setEnableLoggingRequestDetails(true);
        };
}); 

这是HttpMessageWriter的代码

public interface HttpMessageWriter<T> {
         ....
}

这是LoggingCodecSupport的代码

public class LoggingCodecSupport {
            ....
}

我试图测试这种情况,但它会导致 java.lang.ClassCastException

public class Test4 {
   public static void main(String[] args) {
      Parent parent = new Parent() {};
      ((Child)parent).foo();
    
   }
}

class Child{
   public void foo() {
      System.out.println("foo");
   }
}

interface Parent{

}

帮助我理解这件事

并制作此代码,但它也会产生 java.lang.ClassCastException

public class Test4 {
    public static void main(String[] args) {
        Parent parent = new Parent() {};
        ((Child)parent).foo();
    
    }
}

class Child{
    public void foo() {
        System.out.println("foo");
    }
}

interface Parent{

}

class bar extends Child implements Parent{

}

【问题讨论】:

  • Dude - 你需要一些真正实现你的界面的类(例如“Child”)!此外,除非 Parent 实际上 IS 是孩子,否则您不能将“Parent”转换为“Child”。两个“是”关系可能是“父子类子类”或“父实现子类”。我怀疑你可能想要相反的......
  • 谢谢你的commnets 我理解你的内容,但我不明白这段代码 public void accept(HttpMessageWriter t) { ((LoggingCodecSupport)t).setEnableLoggingRequestDetails(true); };

标签: java spring-boot http casting


【解决方案1】:

假设您使用的是最新的 Spring,这是因为 LoggingCodecSupport 的子类之一实现了 HttpMessageWriter 接口。

你可以看看这门课FormHttpMessageWriter

它扩展了 LoggingCodecSupport 但实现了一个 HttpMessageWriter 接口。这就是为什么它可以在不抛出异常的情况下进行转换。

例子应该是这样的

public class Test4 {
  public static void main(String[] args) {
    Parent bar = new Bar() {};
    ((Child)bar).foo();
  }
}

class Child {
  public void foo() {
    System.out.println("foo");
  }
}

interface Parent {

}

class Bar extends Child implements Parent {

}

我们可以假设HttpMessageWriter是Parent接口,LoggingCodecSupport是Child类,FormHttpMessageWriter是Bar类。

【讨论】:

  • 感谢您的评论。那我就知道你了,那formhttpmessagewriter的存在是不是让HttpMessageWriter转换成Spring的LoggingCodeSupport技术?我尝试制作实现父类并扩展子类的示例类 foo,然后我强制转换 ((Child)parent).foo();但它使 java.lang.ClassCastException
  • 在您的示例中,Parent 没有扩展 Child 类。您应该尝试创建 3 个类。就像@paulsm4 实际上所说的那样。您需要创建一个实现 Parent 接口并扩展 Child 类的示例类。
  • [您需要创建一个实现 Parent 接口并扩展 Child 类的示例类。] 我通过您的建议添加代码并编辑我的问题
  • 那你可以在main方法中试试那个类。尝试父 bar = new Bar();并将其转换为 Child 类,这与问题相同。 (我使用 Bar 是因为为了清楚起见,你应该将你的类名大写)
猜你喜欢
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多