6.1.1 The Interface Concept
In the Java programming language, an interface is not a class but a set of requirements for the classes that want to conform to the interface.
Interface never have instance fields. You can think of an interface as an abstract class with no instance fields.
class Employee implements Comparable<Employee>
{
public int compareTo(Employee other)
{
return Double.compare(salary, other.salary);
}
}
6.1.2 Properties of Interfaces
Interfaces are not classes. You can never use the new operator to instantiate an interface:
x = new Comparable(...); // ERROR
However, you can declare interface variables:
Comparable x; // OK
An interface variable must refer to an object of a class that implements the interface:
x = new Employee(...); // OK provided Employee implements Comparable
Next, you can use instanceof
to check whether an object implements an interface:
if(anObject instanceof Comparable) { ... }
6.1.7 Interfaces and Callbacks
在Java里面,callback需要pass一个object。
/**
@version 1.02 2017-12-14
@author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
public class TimerTest
{
public static void main(String[] args)
{ // construct an object that implements the ActionListener interface
var listener = new TimePrinter();
// construct a timer that calls the listener
// once every second
var timer = new Timer(1000, listener);
timer.start();
// keep program running until the user selects "OK"
JOptionPane.showMessageDialog(null, "Quit program?");
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();
}
}
6.1.8 The Comparator Interface
Comparator Interface in Java with Examples
// Java program to demonstrate working of Comparator
// interface
import java.io.*;
import java.lang.*;
import java.util.*;
// A class to represent a student.
class Student {
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name, String address) {
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString() {
return this.rollno + " " + this.name + " "
+ this.address;
}
}
class Sortbyroll implements Comparator<Student> {
// Used for sorting in ascending order of roll number
public int compare(Student a, Student b) {
return a.rollno - b.rollno;
}
}
class Sortbyname implements Comparator<Student> {
// Used for sorting in ascending order of name
public int compare(Student a, Student b) {
return a.name.compareTo(b.name);
}
}
// Driver class
class Main {
public static void main(String[] args)
{
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student(111, "bbbb", "london"));
ar.add(new Student(131, "aaaa", "nyc"));
ar.add(new Student(121, "cccc", "jaipur"));
System.out.println("Unsorted");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new Sortbyroll());
System.out.println("\nSorted by rollno");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new Sortbyname());
System.out.println("\nSorted by name");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
}
}