避免实现接口中存在的方法 - java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20541000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:39:04  来源:igfitidea点击:

avoid implementation of a method which is there in interface - java

javainterfaceabstract-class

提问by CodeNotFound

I have a interface like the below :

我有一个如下所示的界面:

public interface a {
    public void m1();
    public void m2();
    public void m3();
}

public class A implements a {
    public void m3() {
        // implementation code     
    }
}

I want to avoid implementation for the rest of the method. one way is to have all the methods without implementing in the class that tries to implement interface.

我想避免实现方法的其余部分。一种方法是拥有所有方法而不在试图实现的类中实现interface

How do I avoid this. Example code would help me understand better :)

我如何避免这种情况。示例代码将帮助我更好地理解:)

回答by Suresh Atta

Other than what you told, The maximum you can do is make you class A,abstract

除了你所说的,你能做的最大的事情就是让你上课Aabstract

abstract class A implements a{

     public void m3(){

         // implementation code     

         }

回答by Sach

public interface a{

      public void m1();
      public void m2();
      public void m3();

}

public abstract class A implements a{

       public void m3(){

           // implementation code     

           }

}

Declare as abstract class so you will not needed to implement these methods in this class. But you have to implement those method in concrete class

声明为抽象类,因此您不需要在此类中实现这些方法。但是你必须在具体的类中实现这些方法

回答by Ruchira Gayan Ranaweera

You should try Abstractclass. Then you can decide what are the method should implement and what are the method not to.

你应该试试Abstractclass。然后你可以决定哪些方法应该实现,哪些方法不实现。

public abstract class A {

public abstract void m1();

public void m2() {

}

public void m3() {

}

}

}

.

.

public class B extends A{

@Override
public void m1() {   // you have to override m1 only

}
}

回答by Ankur Shanbhag

By default, all the methods in a interface are public abstract. If you do not want to add implementation for a inherited abstractmethod, you need to mark the class as abstract.

默认情况下,接口中的所有方法都是public abstract. 如果不想为继承的abstract方法添加实现,则需要将该类标记为abstract.

public abstract class A implements a {
  // Compiler will not prompt you with an error message.
  // You may add or do not add implementation for a method m3()
}

Note: Abstract classes cannot be instantiated.If you want instantiate it, either add blank implementation or create a subclass of it and implement the method over there.

注意:抽象类不能被实例化。如果要实例化它,请添加空白实现或创建它的子类并在那里实现该方法。

回答by hanish.kh

If you don't want to implement other methods of an interface, use a dummy class which implement this methods and inherit that class in your application. This is called adapter pattern, which is widely used in the adapter classes of swing.

如果您不想实现接口的其他方法,请使用实现此方法并在应用程序中继承该类的虚拟类。这就是所谓的适配器模式,广泛用于swing的适配器类。

回答by Julien

One solution to do that is to create an Abstract class implementing your interface's method without code in them. Then your class A should extend the abstract class and override the right method.

一种解决方案是创建一个抽象类来实现你的接口的方法,而其中没有代码。那么你的类 A 应该扩展抽象类并覆盖正确的方法。

EDIT : this solution makes your interface a bit useless unless it's used in a number of implementations.

编辑:此解决方案使您的界面有点无用,除非它在许多实现中使用。

回答by Samuel

TLDR;

TLDR;

If the interface is given to you, then you have no choice. The creator of the interface forces you to implement all of its methods.

如果接口是给你的,那你就别无选择。接口的创建者强制您实现其所有方法。

If you are writing the interface, then you are probably mistaking. If you want to implement only a subset of the methods, then you would probably be better off with an abstract class.

如果您正在编写界面,那么您可能会误会。如果您只想实现方法的一个子集,那么使用抽象类可能会更好。

In details

详情

An interface declares a behavioral contract. Its purpose is precisely to force all implementing classes to implement all of its methods, thus ensuring the implementing classes are compliant with the contract.

一个接口声明了一个行为契约。其目的正是强制所有实现类都实现它的所有方法,从而确保实现类符合契约。

For example, the following interface:

例如下面的界面:

public interface highlightable {

    public void highlight();

}

declares that every implementing class mustand willimplement the highlight()method. In consequence, as a programmer, knowing that a given class implements the highlightableinterface lets you know that somehow it can be highlighted.

声明每个实现类都必须并且实现该highlight()方法。因此,作为程序员,知道给定的类实现了highlightable接口,可以让您知道它可以以某种方式突出显示。

Ideally, a good interface should indicate the intended purpose of each of its methods as follows:

理想情况下,一个好的接口应该指出其每个方法的预期目的,如下所示:

/**
 * An interface for all things that can be highlighted.
 */
public interface highlightable {

    /**
     * Implementations should make the subject stand out.
     */
    public void highlight();

}

so when a programmer is coding the implementation, it is clear what needs to be done.

因此,当程序员对实现进行编码时,很清楚需要做什么。

回答by shikjohari

You can't to this till java 7. You have to make the class abstract if you want to implement only some of the methods provided in the interface.

在 java 7 之前你不能这样做。如果你只想实现接口中提供的一些方法,你必须使类抽象。

But as of now Java 8 is coming (yet to launch) with the feature of lambdas. The specification says that you can provide a default implementation of a method in the interface itself.

但截至目前,Java 8 即将推出(尚未发布),具有 lambdas 的功能。规范说您可以在接口本身中提供方法的默认实现。

for example

例如

public interface A {
    default void foo(){
       System.out.println("Calling A.foo()");
    }
}

public class Clazz implements A {
}

The code compiles even though Clazz does not implement method foo(). Method foo() default implementation is now provided by interface A.

即使 Clazz 没有实现方法 foo(),代码也会编译。方法 foo() 默认实现现在由接口 A 提供。

回答by Infinite Recursion

Use abstractclass instead of interface.

使用abstract类而不是接口。

In this abstract class,

在这个抽象类中,

  1. Methods which are optionalfor Child class - provide implementation in your abstract class
  2. Methods which are mandatoryfor Child class - declare as abstract in your abstract class, child class must provide implementation
  3. Methods which Child class CAN NOT override- declare as final in your abstract class, and provide implementation
  1. Child 类的可选方法- 在您的抽象类中提供实现
  2. 子类强制的方法- 在抽象类中声明为抽象,子类必须提供实现
  3. Child 类不能覆盖的方法- 在抽象类中声明为 final,并提供实现

Example below:

下面的例子:

abstract class MyClass {
    public void m1(){}; //class A can override, optional
    final public void m2(){}; //class A CANNOT override, has to use implementation provided here
    abstract public void m3(); //class A MUST override, mandatory
}

class A extends MyClass {

    @Override
    public void m3() { //mandatory

    }
}

回答by Alex R

You don't explain why you need this, so let me guess. Perhaps you need a quick way to stub out some functionality for a test. Or you just want something to compile so you can run some unit-tests and come back and finish the rest later. Or perhaps you suspect that some of the methods are superfluous and will never be called in your application.

你没有解释为什么你需要这个,所以让我猜猜。也许您需要一种快速的方法来为测试存根一些功能。或者你只是想编译一些东西,这样你就可以运行一些单元测试,然后回来完成其余的工作。或者您可能怀疑某些方法是多余的并且永远不会在您的应用程序中调用。

This would cover all of the above:

这将涵盖以上所有内容:

public class A implements a {
    public void m3() {
        throw new UnsupportedOperationException("This functionality has not been implemented yet.");     
    }
}