{"id":4484,"date":"2016-06-28T18:08:13","date_gmt":"2016-06-28T15:08:13","guid":{"rendered":"http:\/\/www.fandroid.info\/?page_id=4484"},"modified":"2016-12-13T10:34:50","modified_gmt":"2016-12-13T08:34:50","slug":"primery-metodov-obratnogo-vyzova-callbacks","status":"publish","type":"page","link":"https:\/\/www.fandroid.info\/primery-metodov-obratnogo-vyzova-callbacks\/","title":{"rendered":"\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 (callbacks)"},"content":{"rendered":"

Callback \u00a0\u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435, \u044d\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0438 \u0435\u0451 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u043e\u043c\u0443 \u0442\u043e \u0442\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443, \u043d\u043e \u043d\u0435 \u0436\u0434\u0451\u0442\u0435 \u0435\u0451 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0435 \u00a0\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0430 \u0447\u0442\u043e\u0431\u044b \u043a \u0432\u0430\u043c \u043c\u043e\u0433 \u0432\u0435\u0440\u043d\u0443\u0442\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b, \u0432\u044b \u0434\u043b\u044f \u0432\u043e\u0440\u043a\u0435\u0440\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0435 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441 \u043a\u0430\u0441\u0442\u043e\u043c \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u0430\u043b\u0431\u0435\u043a\u0430 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u0431\u044c\u044f\u0432\u043b\u0435\u043d \u043c\u0435\u0442\u043e\u0434,\u00a0\u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u043e\u0440\u043a\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435. \u0412\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0439.\u00a0\u0412 \u044d\u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b \u043a\u0430\u043b\u0431\u0435\u043a\u043e\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u0438\u0434\u0442\u0438 \u0434\u043e\u043b\u0433\u043e, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u0441\u0442\u0440\u043e, \u043d\u043e \u043a\u0430\u043b\u043b\u0435\u0440 \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0431 \u044d\u0442\u043e\u043c \u0437\u0430\u0431\u043e\u0442\u0438\u0442\u044c\u0441\u044f, \u043e\u043d \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443.<\/p>\n

import java.util.ArrayList;\r\nimport java.util.Random;\r\n \r\n\/**\r\n *\r\n * @author mutagen\r\n *\/\r\npublic class CallBackDemo {\r\n \r\n    public static void main(String[] args) {\r\n \r\n        Caller caller = new Caller();\r\n \r\n        for (int i = 0; i < 10; i++) {\r\n            new Worker(caller).start();\r\n        }\r\n\/\/        \u0438\u0442\u043e\u0433\u043e \u0443 \u043d\u0430\u0441 9 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0438 \u0432\u0435\u0440\u043d\u0443\u043b\u0438 \u043e\u0442\u0432\u0435\u0442 \u043f\u043e \u043a\u0430\u043b\u0431\u0435\u043a\u0443 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e\r\n        System.out.println(caller.getStatuses());\r\n    }\r\n \r\n    static class Caller implements Callback {\r\n \r\n        private ArrayList<Integer> statuses = new ArrayList<>();\r\n \r\n        public ArrayList<Integer> getStatuses() {\r\n            return statuses;\r\n        }\r\n \r\n        @Override\r\n        public void callMeBack(int status) {\r\n\/\/            \u0442\u0443\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0430 \u043a\u0443\u0434\u0430 \u0432\u0435\u0440\u043d\u0443\u0442\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f, \u044f \u0432\u044b\u0431\u0440\u0430\u043b \u043f\u043e\u043f\u0440\u043e\u0449\u0435\r\n            synchronized (statuses) {\r\n                statuses.add(status);\r\n            }\r\n        }\r\n    }\r\n \r\n    static interface Callback {\r\n\/\/        \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0430\u043c \u0431\u0443\u0434\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435\r\n \r\n        void callMeBack(int status);\r\n    }\r\n \r\n    static class Worker extends Thread {\r\n \r\n        private Callback cb;\r\n \r\n        public Worker(Callback cb) {\r\n            this.cb = cb;\r\n        }\r\n \r\n        int pleaseDoMeAFavor() {\r\n            return new Random().nextInt();\r\n        }\r\n \r\n        @Override\r\n        public void run() {\r\n\/\/            \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \r\n            int st = pleaseDoMeAFavor();\r\n\/\/            \u0438 \u0432\u0435\u0440\u043d\u0451\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c\u0443 \u043f\u043e \u043a\u0430\u043b\u0431\u0435\u043a \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443\r\n            cb.callMeBack(st);\r\n        }\r\n    }\r\n}<\/pre>\n

\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u0443\u043d\u043a\u0442 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0436\u0435\u0441\u0442\u043a\u043e \u0438 \u0432 \u044d\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0441\u0442\u0430\u0432\u0438\u043b \u0441\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c, \u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445 \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0435\u043d\u044f\u0442\u044c \u0442\u0430\u043a\u0438\u0435 «\u0432\u0441\u0442\u0430\u0432\u043a\u0438», \u0432\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0434\u043b\u044f 1000 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u0432 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0433\u0440\u043e\u043c\u043d\u0443\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 Java \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u0438.
\n\u042f\u0440\u043a\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u0431\u044d\u043a\u043e\u0432 — Spring-\u043e\u0432\u0441\u043a\u0438\u0435 \u0442\u0435\u043c\u043f\u043b\u0435\u0439\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JDBC — \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u043e\u0431\u0449\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u0430\u043a \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 ResultSet-\u043e\u0432.<\/p>\n

\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0435\u043c\u043f\u043b\u0435\u0439\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n

package callbacks;\r\n \r\nimport java.util.Arrays;\r\nimport java.util.List;\r\n \r\npublic class Template {\r\n \r\n\tprivate ICustomOperation callback;\r\n \r\n\tpublic Template(ICustomOperation callback) {\r\n\t\tthis.callback = callback;\r\n\t}\r\n \r\n\tpublic void setCallback(ICustomOperation callback) {\r\n\t\tthis.callback = callback;\r\n\t}\r\n \r\n\tpublic void processData(List<String> lines) {\r\n\t\tfor (String line : lines) {\r\n\t\t\tSystem.out.println(\"Input: \" + line);\r\n\t\t\tline = \"[\" + line + \"]\";\r\n\t\t\tSystem.out.println(\"Standart operation 1 result : \" + line);\r\n \r\n\t\t\tline = line.replaceAll(\" \", \"_\");\r\n\t\t\tSystem.out.println(\"Standart operation 2 result : \" + line);\r\n \r\n\t\t\tline = callback.evaluate(line); \/\/ \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e\r\n\t\t\tSystem.out.println(\"Custom operation result     : \" + line);\r\n \r\n\t\t\tline = line.toLowerCase();\r\n\t\t\tSystem.out.println(\"Standart operation 3 result : \" + line);\r\n\t\t}\r\n\t}\r\n \r\n}<\/pre>\n

\u0415\u0441\u0442\u044c \u043a\u043e\u043b\u0431\u044d\u043a-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441:<\/p>\n

package callbacks;\r\n \r\npublic interface ICustomOperation {\r\n \r\n\tpublic String evaluate(String input);\r\n \r\n}<\/pre>\n

\u0418 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n

package callbacks;\r\n \r\npublic class CustomOperation implements ICustomOperation {\r\n \r\n\tpublic String evaluate(String input) {\r\n\t\treturn \"CUSTOM: \" + input;\r\n\t}\r\n \r\n}<\/pre>\n
package callbacks;\r\n \r\npublic class AnotherCustomOperation implements ICustomOperation {\r\n \r\n\tpublic String evaluate(String input) {\r\n\t\treturn \"ANOTHER CUSTOM: \" + input;\r\n\t}\r\n \r\n}<\/pre>\n

\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u043a:<\/p>\n

Template template = new Template(new CustomOperation());\r\n\t\tList<String> data = Arrays.asList(new String[] { \"first line\", \"SEcOnD LiNe\" });\r\n\t\ttemplate.processData(data);\r\n \r\n\t\ttemplate.setCallback(new AnotherCustomOperation());\r\n\t\ttemplate.processData(data);<\/pre>\n

\u0415\u0441\u0442\u044c \u0435\u0449\u0451 \u043e\u0434\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435: \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e private \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u0442\u0435 \u0434\u0435\u043b\u0430\u0442\u044c public \u0438\u043b\u0438 protected. \u0412\u044b \u0432\u044b\u0437\u0432\u0430\u0435\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0438\u0437\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0433\u043e \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e private-\u043c\u0435\u0442\u043e\u0434\u0430.\u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:
\npackage callbacks;<\/p>\n

package callbacks;\r\n \r\npublic class PrivateFieldHolder {\r\n \r\n\tprivate OuterCaller outerCaller;\r\n \r\n\tpublic OuterCaller getOuterCaller() {\r\n\t\treturn outerCaller;\r\n\t}\r\n \r\n\tpublic void setOuterCaller(OuterCaller outerCaller) {\r\n\t\tthis.outerCaller = outerCaller;\r\n\t}\r\n \r\n\tpublic void publicMethod() {\r\n\t\tSystem.out.println(\"Holder: public method call\");\r\n \r\n\t\t\/\/ \u0437\u0434\u0435\u0441\u044c \u0445\u0438\u0442\u0440\u0430\u044f \u0448\u0442\u0443\u043a\u0430: \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 \r\n\t\t\/\/ \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043a\u043e\u043b\u043b\u0431\u044d\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439\r\n\t\t\/\/ \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430\r\n\t\t\/\/ \u0442.\u043a. \u044d\u0442\u043e\u0442 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0432\u0438\u0434\u0438\u0442 private-\u043c\u0435\u0442\u043e\u0434, \u0442\u043e \u043c\u044b\r\n\t\t\/\/ \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c callPrivateMethod \u0432\u044b\u0437\u043e\u0432 \u044d\u0442\u043e\u0433\u043e\r\n\t\t\/\/ private-\u043c\u0435\u0442\u043e\u0434\u0430. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \r\n\t\t\/\/ callPrivateMethod \u0432\u043e \u0432\u043d\u0435\u0448\u043d\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f\r\n\t\t\/\/ \u043a private-\u043c\u0435\u0442\u043e\u0434\u0443 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430.\r\n\t\touterCaller.callFromOutsidePrivateMethod(new IPrivateMethodCallback() {\r\n \r\n\t\t\tpublic void callPrivateMethod(String caller) {\r\n\t\t\t\tprivateMethod(caller);\r\n\t\t\t}\r\n\t\t});\r\n \r\n\t}\r\n \r\n\tprivate void privateMethod(String caller) {\r\n\t\tSystem.out.println(caller + \": private method call\");\r\n\t}\r\n \r\n}<\/pre>\n

\u041a\u043e\u043b\u0431\u044d\u043a-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441:<\/p>\n

package callbacks;\r\n \r\npublic interface IPrivateMethodCallback {\r\n \r\n\tpublic void callPrivateMethod(String caller);\r\n \r\n}<\/pre>\n

\u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c private-\u043c\u0435\u0442\u043e\u0434:<\/p>\n

package callbacks;\r\n \r\npublic class OuterCaller {\r\n \r\n\tpublic void callFromOutsidePrivateMethod(IPrivateMethodCallback callback) {\r\n\t\tSystem.out.println(\"Outer method call\");\r\n\t\tcallback.callPrivateMethod(\"Outer caller\");\r\n\t}\r\n \r\n}<\/pre>\n

\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u043a:<\/p>\n

PrivateFieldHolder pfh = new PrivateFieldHolder();\r\n\t\tOuterCaller oc = new OuterCaller();\r\n\t\tpfh.setOuterCaller(oc);\r\n\t\tpfh.publicMethod();<\/pre>\n
\n

<\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0430\u044f<\/a> \u00a0 \u00a0 \u00a0 \u00a0\u041e\u0433\u043b\u0430\u0432\u043b\u0435\u043d\u0438\u0435<\/a>\u00a0 \u00a0 \u00a0 \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

Callback \u00a0\u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435, \u044d\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0438 \u0435\u0451 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u043e\u043c\u0443 \u0442\u043e \u0442\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443, \u043d\u043e \u043d\u0435 \u0436\u0434\u0451\u0442\u0435 \u0435\u0451 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0435 \u00a0\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0430 \u0447\u0442\u043e\u0431\u044b \u043a \u0432\u0430\u043c \u043c\u043e\u0433 \u0432\u0435\u0440\u043d\u0443\u0442\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b, \u0432\u044b \u0434\u043b\u044f \u0432\u043e\u0440\u043a\u0435\u0440\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0435 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441 \u043a\u0430\u0441\u0442\u043e\u043c \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u0430\u043b\u0431\u0435\u043a\u0430 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u0431\u044c\u044f\u0432\u043b\u0435\u043d \u043c\u0435\u0442\u043e\u0434,\u00a0\u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u043e\u0440\u043a\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c […]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":113,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/pages\/4484"}],"collection":[{"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/comments?post=4484"}],"version-history":[{"count":3,"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/pages\/4484\/revisions"}],"predecessor-version":[{"id":5706,"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/pages\/4484\/revisions\/5706"}],"wp:attachment":[{"href":"https:\/\/www.fandroid.info\/wp-json\/wp\/v2\/media?parent=4484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}