【问题标题】:Why the functional interface is not implemented by the class?为什么类没有实现功能接口?
【发布时间】:2021-01-14 22:31:40
【问题描述】:

我在教程中看到了以下代码。我的问题是为什么 LambdaExpressionExample2 类没有实现接口 Drawable?我熟悉班级之间的作文。这里也是这样吗?谢谢。

@FunctionalInterface  //It is optional  
interface Drawable{  
    public void draw();  
}  
  
public class LambdaExpressionExample2 {  
    public static void main(String[] args) {  
        int width=10;  
          
        //with lambda  
        Drawable d2=()->{  
            System.out.println("Drawing "+width);  
        };  
        d2.draw();  
    }  
}  

【问题讨论】:

  • 如果 LambdaExpressionExample2 实现了接口(而不是 lambda 表达式),那么它就不是 lambda 表达式的示例,这大概是代码的重点。
  • 因为 1. LambdaExpressionExample2 没有 draw 方法。 2.你看不到LambdaExpressionExample2 implements Drawable
  • LambdaExpressionExample2 只是主类,即代码的入口点(每个 Java 程序都需要这样的入口点)。所以与功能接口无关。

标签: java lambda interface functional-programming composition


【解决方案1】:

你也可以在主类中实现:

public class LambdaExpressionExample2 implements Drawable{

    @Override
    public void draw() {
        System.out.println("Implemented it in main class !!!");
    }

    public static void main(String[] args) {

        LambdaExpressionExample2 lm = new LambdaExpressionExample2();
        lm.draw();

    }

 }

但这不会是 lambda。

或者您可以定义另一个实现,例如:

public class DrawableImpl implements Drawable {
    @Override
    public void draw() {

        System.out.println("I have implemented Drawable !!!");

    }
}

public class LambdaExpressionExample2{

    public static void main(String[] args) {

        DrawableImpl dm = new DrawableImpl();
        dm.draw();

    }

    }

但这也不是 lambda。 您也可以使用如下匿名类(旧式):

public class LambdaExpressionExample2 {

    public static void main(String[] args) {
        Drawable anonymus_class = new Drawable() {
            @Override
            public void draw() {
                System.out.println("Anonymus class");
            }

        };
        anonymus_class.draw();
    }
}

如果你比较所有,你会发现 lambda 表示法是最精确和直观的。

【讨论】:

  • 谢谢大家。那么这是一个规则,为了使用 Lambda 表达式,函数接口不在类中实现?
  • 来自 Wiki -“在计算机编程中,匿名函数(函数文字、lambda 抽象或 lambda 表达式)是一个未绑定到标识符的函数定义。”所以,我的示例中的代码不是 lambda。使用 lambda 时,您只需创建、使用并在完成后丢弃它。
  • 明白。我的困惑是关于类签名。通常当一个类使用接口时,它的签名看起来像这样,例如:“public class abc implements xyz”。但是在示例中缺少“implements”关键字,我想知道为什么类没有使用implements关键字实现接口。
【解决方案2】:

接口用于 lambda 函数本身,而不是 LambdaExpressionExample2 类。

要了解我的意思,让我们看看可以实现此功能的其他方法。

  1. 您可以使用匿名内部类,而不是使用 lambda。在 lambda 函数成为 Java 8 之前,您必须这样做。
public static void main(String[] args) {
    int width=10;

    // with anonymous inner class
    Drawable d1 = new Drawable() {
        @Override
        public void draw() {
            System.out.println("Anonymous Class: Drawing " + width);
        }
    };

    d1.draw();
}

在这里您可以看到匿名类通过提供draw 方法的实现来实现Drawable 接口。

  1. 我们可以使用实现Drawable 接口的具体类
@FunctionalInterface  
interface Drawable{
    public void draw();
}

class ConcreteDrawable implements Drawable {
    @Override
    public void draw() {
        System.out.println("Concrete Class: Drawing");
    }
}

public class LambdaExpressionExample2 {
    public static void main(String[] args) {

        // with concrete class
        Drawable d1 = new ConcreteDrawable();
        d1.draw();
    }
}

在此示例中,我们不再可以访问 draw 方法中的本地 width 变量。但是可以看到ConcreteDrawable类也提供了Drawable接口的实现。

  1. 或者最后我们可以使用 lambda 函数。
public static void main(String[] args) {
    int width = 10;
    
    // with lambda
    Drawable d1 = () -> { System.out.println("Lambda: Drawing " + width); };
    
    d1.draw();
}

这里的 lambda 函数通过提供draw 方法的实现来实现Drawable 接口。我们可以在这里使用 lambda 函数的唯一原因是 Drawable 接口只声明了一个方法。

所以总而言之,应该实现 Drawable 的不是 LambdaExpressionExample2 类,而是 lambda 函数本身。

【讨论】:

    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2018-10-24
    • 2023-03-16
    • 2010-12-18
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多