为什么函数式接口没有被类实现?

huangapple go评论64阅读模式
英文:

Why the functional interface is not implemented by the class?

问题

以下是翻译好的部分:

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

@FunctionalInterface  //这是可选的
interface Drawable{  
    public void draw();  
}  

public class LambdaExpressionExample2 {  
    public static void main(String[] args) {  
        int width=10;  
          
        //使用Lambda表达式
        Drawable d2=()->{  
            System.out.println("绘制 "+width);  
        };  
        d2.draw();  
    }  
}  
英文:

I saw the following code in a tutorial. My question is why the interface Drawable is not implemented by the class LambdaExpressionExample2? I am familiar with composition between classes. Is that the case here as well ? Thank you.

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

答案1

得分: 1

你还可以在主类中进行实现:

public class LambdaExpressionExample2 implements Drawable{

    @Override
    public void draw() {
        System.out.println("在主类中实现它!!!");
    }

    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("我已经实现了 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.draw();
    }
}

如果你进行比较,你会发现 Lambda 表示法是最精确和直观的。

英文:

You can implement in main class also:

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

    }

 }

But that won't be lambda.

Or you can define another implementation like :

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

    }

    }

But that is also not lambda.
You can also use anonymous class as below (old style):

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

If you compare all, you will see lambda notation is most precise and intuitive.

答案2

得分: 0

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

为了理解我的意思,让我们看一下你可以实现这个功能的其他方法。

  1. 除了使用 lambda,你还可以使用匿名内部类。在 Java 8 中引入 lambda 函数之前,你必须这样做。
public static void main(String[] args) {
    int width=10;

    // 使用匿名内部类
    Drawable d1 = new Drawable() {
        @Override
        public void draw() {
            System.out.println("匿名类:绘制 " + width);
        }
    };

    d1.draw();
}

在这里,可以看到匿名类实现了 Drawable 接口,通过为 draw 方法提供了实现。

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

class ConcreteDrawable implements Drawable {
    @Override
    public void draw() {
        System.out.println("具体类:绘制");
    }
}

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

        // 使用具体类
        Drawable d1 = new ConcreteDrawable();
        d1.draw();
    }
}

在这个例子中,我们在 draw 方法中不再可以访问局部变量 width。但是你可以看到 ConcreteDrawable 类也提供了 Drawable 接口的实现。

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

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

因此,总结起来,应该由 lambda 函数本身实现 Drawable 接口,而不是 LambdaExpressionExample2 类。

英文:

The interface is used for the lambda function itself, not the LambdaExpressionExample2 class.

To see what I mean let's have a look at other ways you could achieve this functionality.

  1. Instead of using a lambda, you could use an anonymous inner class. Before lambda functions became a thing in Java 8, this is how you would have had to do it.
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();
}

Here you can see the anonymous class is implementing the Drawable interface, by providing an implementation for the draw method.

  1. We could use a concrete class that implements the Drawable interface
@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();
    }
}

In this example, we no longer have access to the local width variable in the draw method. But you can see that the ConcreteDrawable class also provides an implementation of the Drawable interface.

  1. Or finally we can use a lambda function.
public static void main(String[] args) {
    int width = 10;
    
    // with lambda
    Drawable d1 = () -> { System.out.println("Lambda: Drawing " + width); };
    
    d1.draw();
}

Here the lambda function is implementing the Drawable interface, by giving an implementation of the draw method. The only reason why we can use a lambda function here is that the Drawable interface only declares one method.

So in summary, it is not the LambdaExpressionExample2 class that should implement Drawable but instead the lambda function itself.

huangapple
  • 本文由 发表于 2020年9月29日 18:12:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64117469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定