简体   繁体   English

在方法中将接口作为参数传递

[英]passing Interface as parameter in Method

I have seen we can pass any types of arguments in Method. 我已经看到我们可以在Method中传递任何类型的参数。

import java.io.File;
import java.io.FileFilter;

public class LastModified {

    public static File lastFileModified(String dir) {
        File fl = new File(dir);
        File[] files = fl.listFiles(new FileFilter() {
            public boolean accept(File file) {
                return file.isFile();
            }
        });
        long lastMod = Long.MIN_VALUE;
        System.out.println("lastMod:"+lastMod);
        File choice = null;
        for (File file : files) {
            System.out.println("File:"+file);
            if (file.lastModified() > lastMod) {
                choice = file;
                lastMod = file.lastModified();
            }
        }
        return choice;
    }

    public static void main(String[] args) {

        lastFileModified("D:\\TestFiles");

    }

}

Here in listFiles method we are passing Interface thing. 在listFiles方法中,我们正在传递Interface事物。 It seems that Interface object is being created, but as far I know it cannot be done. 似乎正在创建Interface对象,但据我所知它无法完成。 It just refer the object of class which implements that interface. 它只是引用实现该接口的类的对象。

Being said that "It's just a way of saying "this parameter will accept any object that supports this interface. 有人说“这只是一种说法”,此参数将接受支持此接口的任何对象。 It's equivalent to accepting some object of a base class type, even if you're passing in a subclass." NOT CLEARED 这等效于接受基类类型的某些对象,即使您要传递子类也是如此。” NOT CLEARED

Questions: 问题:

1) **new FileFilter()** of which class object is being created here ?
2) If we are using interface in class, then why its not implemented in above class ?
3) If its a one more way to implement, then what if that interface would have 10 declared methods ? So Do we need to define all after  **new FileFilter()** ?

Can anyone help me to understand this ? 谁能帮助我了解这一点? I'm really confused here 我真的很困惑

To answer your questions, let's take one by one 为了回答您的问题,让我们一一讲解

1) new FileFilter() of which class object is being created here ? 1)在这里创建哪个类对象的new FileFilter()

It will be an object of anonymous class. 它将是匿名类的对象。 See Can we create an object of an interface? 请参阅我们可以创建接口的对象吗?

2) If we are using interface in class, then why its not implemented in above class ? 2)如果我们在类中使用接口,那么为什么不在上述类中实现呢?

It does not require to implement from main class. 它不需要从主类执行。 You are just referring a interface in your class which does not have to be implemented. 您只是在类中引用了不必实现的接口。

3) If its a one more way to implement, then what if that interface would have 10 declared methods ? 3)如果是一种实现的方法,那么该接口将有10个声明的方法怎么办? So Do we need to define all after new FileFilter() ? 那么,是否需要在new FileFilter()之后定义所有内容?

Yes in that case, all method needs to be implemented. 是的,在这种情况下,需要实施所有方法。

Anonymous classes can implement interfaces, and that too without the "implements" keyword. 匿名类可以实现接口,而且也可以不用“ implements”关键字。

More stackoverflow links: Can we create an instance of an interface in Java? 更多stackoverflow链接: 我们可以在Java中创建接口的实例吗? , Can we create an object of an interface? 我们可以创建接口的对象吗?

On a side note, You have hit a case of Functional Interface which have been introduced in Java 8. An interface with exactly one abstract method is called Functional Interface. 附带说明一下,您碰到了Java 8中引入的Functional Interface的情况。具有一种抽象方法的接口称为Functional Interface。

Read more about functional interfaces here: https://www.journaldev.com/2763/java-8-functional-interfaces 在此处阅读有关功能接口的更多信息: https : //www.journaldev.com/2763/java-8-functional-interfaces

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

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