简体   繁体   English

界面和一个类。名称冲突:相同的擦除,但都不会覆盖其他

[英]interface and a class. name clash: same erasure, yet neither overrides other

I have an interface, and when I try to implement one of its methods, I get this error : "name clash: enqueue(T#1) in GenericQueue and enqueue(T#2) in IGenericQueue have the same erasure, yet neither overrides the other where T#1 ,T#2 are type variables: T#1 extends Comparable declared in class GenericQueue T#2 extends Comparable declared in interface IGenericQueue " here's the code : 我有一个接口,当我尝试实现其中一个方法时,我得到这个错误:GenericQueue中的“name clash:enqueue(T#1)”和IGenericQueue中的enqueue(T#2)具有相同的擦除,但都没有覆盖T#1,T#2是类型变量的另一个:T#1 extends类GenericQueue中声明的Comparable T#2 extends在接口IGenericQueue中声明的Comparable“这里是代码:

public interface IGenericQueue <T extends Comparable> {
public void enqueue(T j);
..
}

public class GenericQueue<T extends Comparable> implements IGenericQueue {
....

public void enqueue(T j) // the error is in this line.
{
    if(rear == maxSize -1)
        rear = -1; // means you have reached the last element start again ?

    queArray[++rear] = j;
    nItems ++ ;
}
}

Your GenericQueue is implementing the raw interface IGenericQueue , so its T is different than the T in IGenericQueue . GenericQueue正在实施原始接口IGenericQueue ,所以其T比不同TIGenericQueue Add the <T> in the implements clause: implements子句中添加<T>

public class GenericQueue<T extends Comparable> implements IGenericQueue<T> {
//                                                                      ^^^

so you are implementing the generic interface with the same T . 所以你用相同的T实现通用接口。

I was having a similar problem, although I have a more complicated generic class hierarchy following the template pattern for OO programing. 我有一个类似的问题,虽然我有一个更复杂的泛型类层次结构遵循OO编程的模板模式。 Where there is an interface then another interface extending that interface then an abstract class implementing that interface then classes extending the abstract class, but was getting the error "interface and a class. name clash: same erasure, yet neither overrides other" And found that only when I put or after every single class in the hierarchy and in every reference to that class would the error go away. 哪里有一个接口,然后另一个接口扩展该接口,然后是一个实现该接口的抽象类,然后扩展抽象类的类,但是得到错误“接口和类。名称冲突:相同的擦除,但都没有覆盖其他”并发现只有当我在层次结构中的每个类中或者在每个类中引入该类之后,错误才会消失。 For example: 例如:

public interface Set<U> {...}
public interface SetExtended<U> extends Set<U> {...}
public abstract class AbstractSetExtended<U> implements SetExtended<U>{...}
public class Set1<U> extends AbstractSetExtended<U> {...}
public class Set2<U> extends AbstractSetExtended<U> {...}

The template pattern is great for modular design, as well as factoring out common code and good for code reuse. 模板模式非常适合模块化设计,并且可以分解公共代码并有利于代码重用。 To read a little more about the template pattern: https://en.wikipedia.org/wiki/Template_method_pattern 要阅读有关模板模式的更多信息,请访问: https//en.wikipedia.org/wiki/Template_method_pattern

Functional Interface -> First of all, Let's Understand the concept of @FuntionalInterface as we know that JDK-1.8 功能接口 - >首先,让我们了解@FuntionalInterface的概念,因为我们知道JDK-1.8
One New Concept Came known as Lambda Expersion : 一个新概念称为Lambda Expersion:

Lambda Expersion Lambda Expersion

You need to understand first concept about LamdaExpersion then you can easily Get What is Funcation Interface role.. 您需要了解有关LamdaExpersion的第一个概念,然后您可以轻松地获取什么是Funcation Interface角色。

// Java program to demonstrate lambda expressions 
// to implement a user defined functional interface. 

// A sample functional interface (An interface with 
// single abstract method 
interface FuncInterface 
{ 
    // An abstract function 
    void abstractFun(int x); 

    // A non-abstract (or default) function 
    default void normalFun() 
    { 
    System.out.println("Hello"); 
    } 
} 

class Test 
{ 
    public static void main(String args[]) 
    { 
        // lambda expression to implement above 
        // functional interface. This interface 
        // by default implements abstractFun() 
        FuncInterface fobj = (int x)->System.out.println(2*x); 

        // This calls above lambda expression and prints 10. 
        fobj.abstractFun(5); 
    } 
} 

Lambda Expersion work only if your Define Interface have only 1 Method define because java compiler understand only if there is only one method in interface , then you dont need to denfine that method in Your Class, therefore : Anotation comes in picture that, when you want implement Lambda Expersion then use have to use @FunctionInterface in your Interface. Lambda Expersion仅在您的Define Interface只有1个Method定义时工作,因为java编译器只了解接口中只有一个方法,然后您不需要在您的类中定义该方法,因此:Anotation出现在图片中,当您需要时实现Lambda Expersion然后使用必须在您的Interface中使用@FunctionInterface If you, by mistake, write one more method in Your Interface, then Compiler will tell you that this is Functional Interface. 如果您错误地在您的界面中再编写一个方法,那么编译器会告诉您这是功能界面。 Only One method will have to define to use Lamda Experssion in your Application 只有一种方法必须定义在您的应用程序中使用Lamda Experssion

    @FunctionalInterface  
interface sayable{  
    void say(String msg);  
}  
public class FunctionalInterfaceExample implements sayable{  
    public void say(String msg){  
        System.out.println(msg);  
    }  
    public static void main(String[] args) {  
        FunctionalInterfaceExample fie = new FunctionalInterfaceExample();  
        fie.say("Hello there");  
    }  
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实现Comparable,compareTo名称冲突:“具有相同的擦除,但不会覆盖其他” - Implementing Comparable, compareTo name clash: “have the same erasure, yet neither overrides the other” 名称冲突 - 具有相同的擦除但在方法参数generic中不会覆盖另一个 - Name clash - have the same erasure yet neither overrides the other in method parameter generic 无法覆盖onBeforeConvert:“......有相同的删除,但都没有覆盖其他” - Cannot Override onBeforeConvert: “…have the same erasure, yet neither overrides the other” 集具有相同的擦除,但没有一个覆盖另一个错误 - Set have the same erasure, yet neither overrides the other error 两种方法具有相同的擦除,但是都不能覆盖另一种方法 - Both methods have same erasure, yet neither overrides the other 两种方法都有相同的擦除,但都没有覆盖另一个 - Both methods have same erasure yet neither overrides the other Java名称冲突,具有相同的擦除方式,但两者都不隐藏 - Java name clash, have the same erasure, neither hides the other 在实现ConsumerSeekAware接口时,如何使“这两种方法都具有相同的擦除能力,但两者都不能覆盖另一个方法”的警告静音? - How do I silence the “both methods have same erasure yet neither overrides the other” warning while implementing the ConsumerSeekAware interface? SkuDetailsResponseListener() 中的“两种方法都具有相同的擦除功能,但都不会覆盖另一个”方法冲突错误 - 'Both methods have same erasure, yet neither overides the other' method clash error in SkuDetailsResponseListener() 具有相同的擦除,但不能覆盖仅在一种情况下出现的其他警告 - Have same erasure, yet neither overrides the other warning appearing in only ONE situation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM