6-4 jmu-Java-04面向对象进阶-01-接口-匿名内部类ActionListener (5分)

来自 链接
已有MyStarter类(你无需编写,直接使用),其具有:
构造函数public MyStarter(ActionListener ac)
方法start()启动任务

main方法执行流程:

  1. 输入整数n和字符串x。
  2. 创建MyStarter对象。该对象的任务为输出n个x字符串,并在循环结束后,使用如下代码
     System.out.println(this.getClass().getName());
     System.out.println(Arrays.toString(this.getClass().getInterfaces()));

打印一些标识信息。
注意:MyStarter类的构造函数public MyStarter(ActionListener ac)要接收ActionListener类型的对象,我们需要建立这个对象并在该对象相应的方法中编写相关功能代码。

最后:调用MyStarter对象的start方法启动任务。

裁判测试程序:


public static void main(String[] args) {
    MyStarter starter;
    //这边写上你的代码
    starter.start();
    sc.close();
}
输入样例:
3  
a
输出样例:
a
a
a
//此处有两行标识信息
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
    MyStarter starter;
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String str=sc.next();
    //创建匿名内部类
    ActionListener ac= new ActionListener() {
    	//构建代码块
    	 {
    		for(int i=0;i<n;i++) {
				System.out.println(str);
			}
			 System.out.println(this.getClass().getName());
			 System.out.println(Arrays.toString(this.getClass().getInterfaces()));
    	}
		//重写接口中的方法
		@Override
		public void actionPerformed(ActionEvent e) {
			
		}
    	
    };
    starter=new MyStarter(ac);
    starter.start();
    sc.close();
}



}
//因为有start();方法,又没有重写run();方法,所以猜测它继承了Thread类
class MyStarter extends Thread {
	ActionListener ac;
	public MyStarter(ActionListener ac) {
		super();
		this.ac = ac;
	}
	
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值