部分实现接口

时间:2013-09-03 10:45:35

标签: java

只有少量时间搜索这些内容,我看不到任何涉及我特定情况的内容。

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

    public interface interface1
    {
        public String getVar1();

        public void  method1();

        public void method2();

    }

我需要创建一些使用此接口的类,但“method2”的实际方法实现在每个上都不同,其中“method1”和“getVar1”将始终是相同的代码。

有没有办法创建一个实现接口的抽象基类,但只强制将“method2”实现到子类上?

我曾尝试过

public abstract @Override void method2

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

如果不可能,那么公平,我只需要检查。

感谢

5 个答案:

答案 0 :(得分:16)

您可以通过创建实现接口的抽象类来完成此操作。此抽象类的任何子类都需要实现尚未定义的任何接口方法。

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

    }

    @Override
    public void method1() {

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

    }
}

请参阅:Java tutorial on abstract classes

答案 1 :(得分:2)

您可以创建一个不实现method2()

的抽象类
public abstract class AbstractImplementation implements interface1 {
    public String getVar1() { 
      // implementation ..
    }

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

    // skip method2
}

然后创建多个实现:

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

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

...

答案 2 :(得分:0)

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

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

喜欢

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

    }

}

答案 3 :(得分:0)

在这种情况下,您可以在界面中使用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();
}

答案 4 :(得分:0)

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();
        }            
    }