简体   繁体   中英

how to make call to anonymous inner class

I am having following code:

        class MyWonderfulClass {
    void go() {
        Bar b = new Bar();
            b.doStuff(new Foo() {
                public void foof() {
                System.out.println("foofy");
                } // end foof method
        }); // end inner class def, arg, and b.doStuff stmt.
    } // end go()
    } // end class

    interface Foo {
    void foof();
    }
    class Bar {
    void doStuff(Foo f) { }
    }

In line 4, anonymous class is passed as argument. I wanted to know how is the method foof() of that class getting called.

Actually my question arises from the fact because i fail to understand the following code:

    public Employee getEmployeeById(longid){  
            return jdbcTemplate.queryForObject(SELECT_QUERY,  
            new RowMapper<Employee>(){  
                    public Employee mapRow(ResultSetrs,  
                    int rowNum)throwsSQLException{  
                            Employee employee=newEmployee();  
                            employee.setId(rs.getLong("id"));  
                            employee.setFirstName(rs.getString("firstname"));  

                            employee.setLastName(rs.getString("lastname"));  
                            employee.setSalary(rs.getBigDecimal("salary"));  
                            return employee;  
                    }  
            },  
            id);  
    }  

From my main, i call getEmployeeById method, which in turn calls jdbcTemplate.queryForObject. In this method the 2nd argument is anonymous class implementing RowMapper, which has method mapRow. How is the method mapRow being called.

The main difference between your code and the sample is that the sample returns an object which can be acted upon.

In your code if you call go() your inner code will be called as the foof() method is not being called from anywhere.

Object of inner class is passed to doStuff() and then this method applies its logic on that object, and it may call or may not call foof() method, its choice of implementor. Anonynous inner class will not restrict passing its object as a variable.

in your example, foof() is not called magically. it is supposed to be called in the logic of doStuff() .

The doStuff() may looks like:

class Bar {
    void doStuff(Foo f) { 
        System.out.println("I am doing some stuff");
        f.foof();
        System.out.println("finish doing some stuff");
    }
}

In the above case, doStuff() will print a line "I am doing some stuff", and do whatever it is told to do (which is the Foo implementation that is passed in), and print the "finish" line.

I wish the picture will be clear with the above example.

Of course, it can be more complicated than the above example. For example, in jdbcTemplate.queryForObject(), the RowMapper is called for each record the jdbcTemplate retrieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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