简体   繁体   English

接口看不到具体的实现

[英]Interface does not see concrete implementation

I have 我有

ISomeInterface ISome接口

public interface ISomeInterface{
    public void myMethod();
} 

AbstractClass 抽象类

public abstract class AbstractClass implements ISomeInterface{

    public void myMethod(){
    //...here goes implemetations
    }
}

ConcreteClass 具体类

public class ConcreteClass extends AbstractClass {
    //...
}

Compiler prints than ConcreteClass is not abstract and does not override abstract method myMethod in ISomeInterface . ConcreteClass编译器打印的不是抽象的,也不覆盖ISomeInterface抽象方法myMethod

The idea is to give implementation to one abstract class and then inherit it in classes that extend it. 这个想法是给一个抽象类提供实现,然后在扩展它的类中继承它。 I think that ConcreteClass should gets implementation form AbstractClass since it's extending it. 我认为ConcreteClass应该从AbstractClass获得实现,因为它是在对其进行扩展。 Right? 对? What's the matter? 怎么了?

UPDATE 更新

I haven't noticed until now that method is wrong and it has to be myMethod . 直到现在我还没有注意到该method是错误的,它必须是myMethod Anyways, same error. 无论如何,同样的错误。

UPDATE2 更新2

The problem was that in AbstractClass them method had correct name but incorrect signature. 问题在于,在AbstractClass中,它们的方法名称正确,但签名不正确。 After changing it according with interface the problem was solved :) 根据接口更改它后,问题解决了:)

In AbstractClass you are creating a method myMethod() , but your interface method method() is not being implemented in ConcreteClass . AbstractClass您正在创建方法myMethod() ,但您的接口方法method()并未在ConcreteClass中实现。 The names are different. 名称是不同的。

Assuming that your sample code is complete, you need to implement method from your interface. 假设您的示例代码完整,则需要从您的界面中实现method In AbstractClass you have called it myMethod instead. AbstractClass您改为将其命名为myMethod

You can use the @Override annotation on any override methods. 您可以在任何覆盖方法上使用@Override批注。 This will tell you if you have used the wrong parameter types, method name, or an incompatible return type (parameters and return types don't have to be the same; they can, for example, be covariant ). 这将告诉您是否使用了错误的参数类型,方法名称或不兼容的返回类型(参数和返回类型不必相同;例如,它们可以是协变量 )。

Additionally, in Java, we don't tend to prefix interface names with I (unlike the convention in C#). 此外,在Java中,我们不倾向于在接口名称前加上I (与C#中的约定不同)。 Your interface would usually be called SomeInterface and not ISomeInterface . 您的界面通常称为SomeInterface而不是ISomeInterface

Also, the public on methods on interfaces is implicit , so you can leave it out. 同样,接口上方法的public 是隐式的 ,因此您可以省去它。 If you do include it you should probably include the abstract as well to include all of the implicit modifiers. 如果包含它,则可能还应该包含abstract ,以包含所有隐式修饰符。

An updated code sample would be: 更新的代码示例将是:

public interface SomeInterface{
  void method();
} 

public abstract class AbstractClass implements SomeInterface {
  @Override
  public void method(){
      //...here goes implemetations
  }
}

You are probably overriding the wrong method . 可能覆盖了错误的方法 What happens is that you are probably attempting to override a method in your abstract class but what you are actually doing is to just define a new method with a new name. 发生的情况是您可能正在尝试覆盖抽象类中的方法,但实际上是在使用新名称定义一个新方法。 In the interface, the method is named method but in your abstract class your method is named myMethod . 在界面中,该方法名为method但在抽象类中,该方法名为myMethod

So, check this out: 因此,请查看以下内容:

public abstract class AbstractClass implements ISomeInterface{

    // Not the same name as in the interface
    public void myMethod(){
        //...here goes implemetations
    }
}

In order to solve it, simply change the method name in the subclass to the correct name. 为了解决它,只需将子类中的方法名称更改为正确的名称。

public abstract class AbstractClass implements ISomeInterface{
    // Now you have the correct name and inheritance will
    // work as expected
    @Override
    public void method(){
        //...here goes implemetations
    }
}

This is the perfect case for explaining the @Override annotation as well ;) 这也是解释@Override注释的理想情况;)

When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. 覆盖方法时,可能需要使用@Override批注,该注释指示编译器打算覆盖超类中的方法。 If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error. 如果由于某种原因,编译器检测到该方法在超类之一中不存在,则它将生成错误。

When you declare a method with the annotation @Override the overridden method must match the signature of the interface-method (or superclass method). 当使用注解@Override声明方法时,被覆盖的方法必须与接口方法(或超类方法)的签名匹配。 Read more about @Override in the Oracle Docs . 在Oracle文档中了解有关@Override更多信息。

And, if you are not trying to override the method named method in your abstract class you simply need to add that method to your concrete class like this: 而且,如果您不打算在抽象类中覆盖名为method ,则只需要将该方法添加到您的具体类中,如下所示:

public class ConcreteClass extends AbstractClass {
    // Now we are implementing the correct method from the interface
    // If not, there will be a compiler error.
    @Override
    public void method() {

    }
    //...
}

On a side note which may be relevant: methods can have the same name but with different argument lists. 附带说明一下:方法可以具有相同的名称,但具有不同的参数列表。 This is known as overloading (or overloaded methods) which you can read more about in this article . 这就是所谓的重载 (或重载方法),您可以在本文中了解更多信息。

Edit: Since the OP is using Java 5 this question may be interesting. 编辑:由于OP使用Java 5, 这个问题可能很有趣。 The @Override annotation changed between Java 5 and Java 6. In Java 5 it was not allowed to use the @Override annotation when implementing a method from an interface, it was just allowed when overriding a method from a superclass. 在Java 5和Java 6之间更改了@Override注释。在Java 5中,从接口实现方法时不允许使用@Override注释,而从超类覆盖方法时才允许使用@Override注释。

Your interface defines 您的界面定义

public void method();

but your abstract class has 但是你的抽象类有

public void myMethod(){
    //...here goes implemetations
}

Which isn't the same! 哪个不一样! Add the Override annotation to catch that kind of issue at compile time. 添加Override批注以在编译时捕获此类问题。

@Override
public void myMethod(){ // <-- compile error.
    //...here goes implemetations
}

From the linked Javadoc, 在链接的Javadoc中,

Indicates that a method declaration is intended to override a method declaration in a supertype. 指示方法声明旨在覆盖超类型中的方法声明。

好吧,我注意到的第一件事是您的方法在接口和抽象类中的名称不同。

暂无
暂无

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

相关问题 在Kotlin中将接口定义为接口的属性并在接口实现中提供具体的实现 - Defining an interface as a property of an interface in kotlin and providing a concrete implemententation in interface implementation does not work 使用java中的具体实现返回通用接口 - Returning a generic Interface with concrete implementation in java 仅存在于接口的一个具体实现中的 function - A function that are existing only in one concrete implementation of an interface 在接口中创建具体实现的新实例 - 这是反模式吗? - Creating new instance of concrete implementation in interface - is this an antipattern? 将具体的实现连接到blueprint.xml中的接口 - Wire concrete implementation to interface in blueprint.xml Spring - 如何注入具体的接口实现? - Spring - how to inject concrete interface implementation? JPA是如何找到具体实现调用的呢? - How does JPA find the concrete implementation to call? 在没有具体实现的接口的xml配置中创建spring bean - Creating spring bean in xml configuration for an interface without concrete implementation 使用接口而非实现进行编程:为什么在运行时分配对象的具体实现更好? - Programming to an interface instead of an implementation: Why is assigning the concrete implementation of an object at runtime even better? 通过 arguments - 接口实现与实现接口的具体 class 的 object - Passing arguments - interface implementation vs object of concrete class which implements the interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM