both methods have same erasure, yet neither overrides the other

java 开发学习python 的过程
一起学习,java 开发学习python 的过程
简简单单OnlineZuozuo

both methods have same erasure, yet neither overrides the other


1、编译报错 both methods have same erasure, yet neither overrides the other

编译报错的意思是,两个方法在类型擦除后,具有相同的原生类型参数列表,但是也不能覆盖另一个方法

代码如下

@Getter
public abstract class AbstractChain<T> {

  private List<T> chains = Lists.newArrayListWithExpectedSize(7);

  protected void addChain(T t) {
    chains.add(t);
  }
}
@Getter
public abstract class AbstractDistributionChain<T> extends AbstractChain<T> {


  /**
   * 分销的分段处理器
   */
  public abstract void handler(List<RelationNode> chains);


}

@Slf4j
@Component
public class AgentGradationHandler extends AbstractDistributionChain {


  @Override
  public void handler(List<RelationNode> chains) {
  }
}

错误如图所示
在这里插入图片描述

2、为什么呢

泛型类型在编译后,会做类型擦除,只剩下原生类型。如参数列表中的T类型会编译成Object,但是会有一个Signature。

尽管两个方法具有相同的字节码,但是类型参数信息用 一个新的签名(signature) 属性记录在类模式中。JVM 在装载类时记录这个签名信息,并在运行时通过反射使它可用。

这就导致了这个方法既不能作为覆盖父类方法的方法,也不能作为方法的重载

3、如何解决

只需要在AgentGradationHandler 上添加泛型类型即可,不使用这个泛型也没有关系

@Slf4j
@Component
public class AgentGradationHandler<T> extends AbstractDistributionChain<T> {

  @Override
  public void handler(List<RelationNode> chains) {
  }
}
简简单单OnlineZuozuo CSDN认证博客专家 Spring 架构 分布式
未闻万里蓬莱,而窥先圣遗智。故,以此生筑梦,奔而逐之;以泰山之伟,攀而登之;以静雅素心,处世为人。------zuozuo著

Same or not

07-17
Problem Description Everyone konws how to traveling a tree using the DFS strategy. But we also know that there are many ways to do so. For example, giving a tree as the following picture, we may get three ways: 010011, 001101, 01010011. 0 stands for the down operation while 1 means the up operation. Now we make a constraint: if one node has k direct childs, you can visit a node at most 2*k times, if k == 0, you can visit it only once, in the example, the root has two direct child. Like the example, you can only get two ways: 010011, 001101. Because the way 01010011 will visit the node in yellow four times. Here is the problem: ACboy drawed a tree, but is not very nice, so he won't show you the picture. Instread he will give you two strings indicating that the ways to travel the tree. Of cource, the strings will only contain 0 and 1. And your mission is to tell whether ACboy is telling the truth. For example, he drawed a picture as the following, if he give you 010011 and 001101, then he is telling the truth, but if he give you 010011 and 01010011, you konw that he is telling a lie. Input On the first line of input is a single positive integer n, telling the number of test scenarios to follow.Each test case consists of two lines, each containing a string of the characters '0' and '1' of length at most 3000, both describing a way to travel the tree. Output For each test case output a line containing the word "True" or the word "False", depending on whether ACboy is telling the truth. Sample Input 2 010011 001101 010011 01010011 Sample Output True False
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值