Java 接口的部分实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18590276/
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-12 09:18:14  来源:igfitidea点击:

Partial implementation of an interface

java

提问by Paul Eden

Having only a small amount of time to search for such things, I could not see anything that covered my specific scenario.

只有很少的时间来搜索这些东西,我看不到任何涵盖我的特定场景的东西。

I am working with a 3rd party interface. Let us say for a moment it is as follows:

我正在使用第 3 方界面。让我们暂时说一下:

    public interface interface1
    {
        public String getVar1();

        public void  method1();

        public void method2();

    }

I need to create a number of classes that make use of this interface but the actual method implementation of "method2" would differ on each,where as "method1" and "getVar1" would always be the same code.

我需要创建一些使用这个接口的类,但是“method2”的实际方法实现在每个类上都会有所不同,因为“method1”和“getVar1”总是相同的代码。

Is there any way to create an abstract base class that implements the interface but only forces the implementation of "method2" onto the child classes?

有什么方法可以创建一个抽象基类来实现接口但只将“method2”的实现强制到子类上?

I had tried

我试过了

public abstract @Override void method2

but while this is "acceptable" at design time in Netbeans (don't know if Eclipse acts differently) at compile time it complains that method2 needs to be implemented.

但是,虽然这在 Netbeans 的设计时是“可接受的”(不知道 Eclipse 的行为是否不同)在编译时它抱怨需要实现 method2。

If it is not possible, fair enough, I just need to check.

如果不可能,足够公平,我只需要检查一下。

thanks

谢谢

采纳答案by Tim Cooper

You can do this by creating an abstract class that implements the interface. Any sub-class of this abstract class will be required to implement any interface methods that were not yet defined.

您可以通过创建一个实现该接口的抽象类来做到这一点。这个抽象类的任何子类都需要实现任何尚未定义的接口方法。

public abstract class AbstractInterface implements interface1 {
    @Override
    public String getVar1() {

    }

    @Override
    public void method1() {

    }
}
public class Implementation extends AbstractInterface {
    @Override
    public void method2() {

    }
}

See: Java tutorial on abstract classes.

请参阅:Java 抽象类教程

回答by Nargis

You can have an abstract Classthat provides implementation for "method 1" and "getVar1" but decalre method2 as abstractin this Class.

您可以拥有一个abstract Class为“方法 1”和“getVar1”提供实现的decalre method2 as abstract类,但在此类中。

Now Inherit this Abstract Super Class in all your other Classes!

现在在所有其他类中继承这个抽象超类!

Like

喜欢

public interface interface1 {

    public void getVar1();

    public void method1();

    public void method2();

}

public abstract class test implements interface1 {

    @Override
    public void getVar1() {
        // TODO Auto-generated method stub

    }

    @Override
    public void method1() {
        // TODO Auto-generated method stub

    }

    @Override
    public abstract void method2();


}

public class test1 extends test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    @Override
    public void method2() {
        // TODO Auto-generated method stub

    }

}

回答by micha

You can create an abstract class which does not implement method2()

您可以创建一个不实现的抽象类 method2()

public abstract class AbstractImplementation implements interface1 {
    public String getVar1() { 
      // implementation ..
    }

    public void  method1() {
      // implementation ..
    }

    // skip method2
}

Then create multiple implementations:

然后创建多个实现:

public class Implementation1 extends AbstractImplementation {
    public void method2() {
      // implementation 1
    }
} 

public class Implementation2 extends AbstractImplementation {
    public void method2() {
      // implementation 2
    }
}

...

回答by alexvipul

In this case you have 3 abstract methods in the interface you can override one or two methods and other methods will remain as abstract

在这种情况下,接口中有 3 个抽象方法,您可以覆盖一两个方法,其他方法将保持抽象

abstact class PartialImplementationDemo implements interface1 {
  @Override
  public void method1() {
    // override method1() here
  }

  // leave other methods as abstract
  abstract public void method2();
  abstract public String getVar1();
}

回答by Jayswal Mihir

interface I1
{
    public void  Method1();
    public void Method2();
}
abstract class A implements I1
{
    public void Method1()
    {
         System.out.println("In  Method 1");
    }

    abstract public void Method3();
}
class B extends A    //Inherits class A 
{
    public void Method2()
    {
         System.out.println("In Method 2");
    }
    public void Method3()
    {
         System.out.println("In Method 3");
    }
}
public class Main 
    {
     public static void main(String args[])
        {
            B b1=new B();
            b1.Method1();
            b1.Method2();
            b1.Method3();
        }            
    }