search

Home  >  Q&A  >  body text

java - FilenameFilter接口中,accept 方法回调问题

  1. 见下面的程序,这个回调是怎么发生的啊?从程序中怎么看不出逻辑,都没有调用accept ()?

import java . io . File ;
import java . io . * ;
import java . util . * ;
import java . util . regex . * ;


public class DirList
{
        public static void main (String [ ] args)
        {
                File path = new File (".") ; 
                String[ ] list ;
                if ( args . length == 0 )
                {
                        list = path . list ( ) ;
                }
                else 
                {
                        list = path . list (new DirFilter ( args [ 0 ])) ; 
                }
                Arrays . sort (list , String . CASE_INSENSITIVE_ORDER );
                for (String dirItem : list ) 
                    System . out . println (dirItem) ;        
        }
}

class DirFilter implements FilenameFilter 
{
        private Pattern pattern ; 
        public DirFilter ( String regex )
        {
                pattern = pattern . compile ( regex );
        }
        
        public boolean accept ( File dir , String name )
        {
                return pattern . matcher ( name ) . matches (  ) ;
        }
}
迷茫迷茫2807 days ago1494

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 17:29:02

    Simply put, it happens using the conditions defined in your filter when you add it to the list

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:29:02

    accept方法在file.list里面调用的,楼主可以打开File类的源代码,很清楚的看到调用流程,下面是我从File类里面复制出来的listmethod

        public String[] list(FilenameFilter filter) {
            String names[] = list();
            if ((names == null) || (filter == null)) {
                return names;
            }
            List<String> v = new ArrayList<>();
            for (int i = 0 ; i < names.length ; i++) {
                if (filter.accept(this, names[i])) {
                    v.add(names[i]);
                }
            }
            return v.toArray(new String[v.size()]);
        }
    

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:29:02

    The meaning of callback is that you implement an interface (not necessarily an interface), but do not call this interface, but let the party that defines this interface (here should refer to the Java class library) to call the implementation you gave .

    The operation of listing subdirectories that meet the requirements involves several steps, some of which are unchanged (for example, you need to first get all the subdirectories in the current directory. I have not seen the source code, but it should be a system call of the OS) , these are all implemented in the Java class library. Corresponding to this step is the change operation, that is, what conditions do you want the subdirectory to meet. Therefore, the class library designs the changed part as an interface for you to implement, and then you register your callback through File.list(). File.list()去注册你的回调。

    因为你不调用,所以才叫回调呀 - don't call me, I'll call back


    或者更直接一点,accept(File, String)的调用发生在File.list(FilenameFilter)函数内。在函数内,会将参数dirname传递给你给的实现,也就是调用accept(File, String)

    Because you don't call, that's why it's called callback - don't call me, I'll call back#🎜🎜#
    #🎜🎜#Or more directly, the call of accept(File, String) occurs within the File.list(FilenameFilter) function. Within the function, the parameters dir and name will be passed to the implementation you gave, that is, the accept(File, String) method will be called. #🎜🎜#

    reply
    0
  • Cancelreply