简体   繁体   English

java中常用的接口方法实现

[英]Common interface method implementation in java

I have 2 classes that implements a particular interface.我有 2 个实现特定接口的类。
I would like to implement a method that would be shared by the 2 classes.我想实现一个由 2 个类共享的方法。
Can I add that method implementation to the interface class and then make a call to that method from the 2 classes?我可以将该方法实现添加到接口类,然后从 2 个类调用该方法吗?

For eg:例如:

public interface DM 
{
    public static void doSomething() { 
        System.out.println("Hello World");}
    }

    public class A implements DM
    {
        doSomething();
    }

    public class B implements DM
    {
        doSomething();
    }
}

Is this feasible?这可行吗?
What is the proper way to do this?这样做的正确方法是什么?

Yes, if you are using Java 8, you can create a default implementation, like this:是的,如果您使用的是 Java 8,您可以创建一个default实现,如下所示:

public interface DM
{
    default void doSomething() { System.out.println("Hello World");}
}

or, if it should be static:或者,如果它应该是静态的:

public interface DM
{
    static void doSomething() { System.out.println("Hello World");}
}

For more information, see Oracle's documentation on the feature有关更多信息,请参阅有关该功能的 Oracle 文档

Another strategy you could use, if you are able to make more widespread changes to your code, would be to use an abstract class instead of an interface, and have your implementing classes extend that class instead.如果您能够对代码进行更广泛的更改,您可以使用的另一种策略是使用abstract class而不是接口,并让您的实现类extend该类。 Any methods in your interface that you do not want to write defaults for should be marked as abstract .您不想为其编写默认值的接口中的任何方法都应标记为abstract

public abstract class DM
{
    public void doSomething() { System.out.println("Hello World");}
    public abstract void doSomethingElse();
}

public class A extends DM
{
  doSomething();
}

You could also combine the approaches if you want to use interfaces but can't/won't use defaults:如果您想使用接口但不能/不会使用默认值,您也可以结合使用这些方法:

public abstract class DMImpl impelements DM
{
    @Override        
    public void doSomething() { System.out.println("Hello World");}
}

public class A extends DM
{
  doSomething();
}

You can create a default method with Java 8. It has some limitations but good for some commonly shared functionality.您可以使用 Java 8 创建一个default方法。它有一些限制,但对于一些常见的共享功能很有用。

https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

interface DM {

    default public void doSomething() {
      System.out.println("Hi");
    }

}

First of all, interface is not a class .首先, interface不是 class Next, yes, with java 8's default methods , you can add method definitions to interfaces ,接下来,是的,使用 java 8 的默认方法,您可以向接口添加方法定义,

you can try this你可以试试这个

interface DM 
{
    public void doSomething();
}

class A implements DM
    {
        public void doSomething()
        {
            System.out.println("Hello World");
        }
    }

class B implements DM
    {
        public void doSomething()
        {
            System.out.println("Hello World");
        }
    }
class Test111
{
    public static void main(String args[])
    {
        System.out.println("Hello");
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM