A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 流浪lg 中级黑马   /  2016-4-9 23:39  /  560 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

javaGUI部分如何实现用按钮监听自己写好的类功能?新手上路请大师指点

点评

好快啊  发表于 2016-4-9 23:57

5 个回复

倒序浏览
赞!!!!!!!
回复 使用道具 举报
采用回调接口!小例子:
  1. interface PrintInter {
  2.         public void print(String text);
  3. }

  4. class PrintClass {
  5.         public void printer(PrintInter inter){
  6.                 System.out.println("回调前");
  7.                 inter.print("回调中...");
  8.                 System.out.println("回调后");
  9.         }
  10. }

  11. class CallBackDemo {
  12.         public static void main(String[] args) {
  13.                 PrintClass print = new PrintClass();
  14.                 print.printer(new PrintInter(){
  15.                         public void print(String text){
  16.                                 System.out.println(text);
  17.                                 System.out.println("这里可以做任何事情!!");
  18.                         }
  19.                 });
  20.         }
  21. }
复制代码

点评

好神奇,我以为new PrintInter(){...}会报错,这是为什么呢?这算是接口的实例化吗?  发表于 2016-4-12 08:27
回复 使用道具 举报
PrintInter 是接口,不能实例化的。new PrintInter();就是实例化。如果非要new的话就必须要实现接口里的方法。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马