接口操作实例

import java.util.*;

public class Main{
    public static void main(String[] args)
    {
        var staff=new Employee[3];
        staff[0]=new Employee("lol",3600);
        staff[1]=new Employee("pal",2396);
        staff[2]=new Employee("oll",9200);
        Arrays.sort(staff);//要让一个类使用排序服务就必须让他实现compareTo方法
        for(var i:staff)
        {
            System.out.println(i.getname());
        }
    }
}

class Employee implements Comparable<Employee>{//接口关键字implements
    private String name;
    private double salary;
    public Employee(String name,double salary)
    {
        this.name=name;
        this.salary=salary;
    }
    public double getsalary()
    {
        return this.salary;
    }
    public String getname()
    {
        return this.name;
    }
    public int compareTo(Employee other)
    {
        return Double.compare(this.salary,other.salary);
    }
}

回调函数

import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
public class test {
	public static void  main(String[] args)
	{
		var listener=new TimePrinter();
		var timer=new Timer(1000,listener);//第一个参数是时间间隔,第二个参数为监听器对象
		timer.start();//启动定时器
		JOptionPane.showMessageDialog(null, "Quit program?");//显示一个包含一条提示消息和ok按钮的对话框
		System.exit(0);
	}
}

class TimePrinter implements ActionListener
{
	public void actionPerformed(ActionEvent event)
	{
		System.out.println("At the tone,the time is "+Instant.ofEpochMilli(event.getWhen()));//得到一个更可读的描述
		Toolkit.getDefaultToolkit().beep();//获得默认的工具箱,工具箱包含有关GUI环境的信息  beep()发出一声铃响
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值