接口在Java中,接口不是类,而是对类的一组需求描述,这些类要遵从接口描述的统一格式进行定义。
接口中的所有方法自动地属于public,实现接口时,必须把方法声明为public。接口中还可以定义常量。
接口中不能含有实例域,也不能在接口中实现方法。可以将接口看成没有实例域的抽象类,但两者有区别。
- import java.util.Arrays;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Employee[] staff = new Employee[3];
-
- staff[0] = new Employee("Harry Hacker", 35000);
- staff[1] = new Employee("Carl Cracker", 75000);
- staff[2] = new Employee("Tony Tester", 38000);
-
- Arrays.sort(staff);
-
- for (Employee e : staff)
- System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
- }
- }
复制代码- public class Employee implements Comparable<Employee>
- {
- private String name;
- private double salary;
-
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- }
-
- public String getName()
- {
- return name;
- }
-
- public double getSalary()
- {
- return salary;
- }
-
- public void raiseSalary(double byPercent)
- {
- double raise = salary * byPercent / 100;
- salary += raise;
- }
-
- public int compareTo(Employee other)
- {
- return Double.compare(salary, other.salary);
- }
- }
复制代码 可以声明接口变量,接口变量必须引用实现了接口的类对象。
如同类可以被继承,接口也可以被扩展。
接口中的常量被自动设为public static final。
一个类可以实现多个接口,但只能继承一个抽象类。
对象克隆当拷贝一个变量时,原始变量与拷贝变量引用同一个对象。
如果创建一个对象的新的copy,它有自己的内存地址,需要使用clone方法。
如果自定义clone方法,需实现Cloneable接口。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- try
- {
- Employee original = new Employee("John Q. Public", 50000);
- original.setHireDay(2000, 1, 1);
- Employee copy = original.clone();
- copy.raiseSalary(10);
- copy.setHireDay(2002, 12, 31);
- System.out.println("original=" + original);
- System.out.println("copy=" + copy);
- }
- catch (CloneNotSupportedException e)
- {
- e.printStackTrace();
- }
- }
- }
复制代码- import java.util.Date;
- import java.util.GregorianCalendar;
- public class Employee implements Cloneable
- {
- private String name;
- private double salary;
- private Date hireDay;
-
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- hireDay = new Date();
- }
-
- public Employee clone() throws CloneNotSupportedException
- {
- Employee cloned = (Employee) super.clone();
-
- cloned.hireDay = (Date) hireDay.clone();
-
- return cloned;
- }
-
- public void setHireDay(int year, int month, int day)
- {
- Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();
-
- hireDay.setTime(newHireDay.getTime());
- }
-
- public void raiseSalary(double byPercent)
- {
- double raise = salary * byPercent / 100;
- salary += raise;
- }
-
- public String toString()
- {
- return "Employee[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
- }
- }
复制代码 接口与回调
回调是一种常见的程序设计模式,在这种模式中,可以指出某个特定事件发生时应该采取的动作。
例如,可以指出在按下鼠标或选择某个菜单项时应该采取什么行动。
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.Timer;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- ActionListener listener = new TimePrinter();
-
- Timer t = new Timer(10000, listener);
- t.start();
-
- JOptionPane.showMessageDialog(null, "Quit program?");
- System.exit(0);
- }
- }
- class TimePrinter implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- Date now = new Date();
- System.out.println("At the tone, the time is " + now);
- Toolkit.getDefaultToolkit().beep();
- }
- }
复制代码 内部类
内部类既可以访问自身的数据域,也可以访问外围类的数据域。
内部类对象有一个隐式引用,指向了创建它的外部类对象。
这个引用在内部类的定义中不可见,编译器修改了所有内部类的构造器,添加一个外部类引用的参数。
内部类可以是私有类。
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.Timer;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- TalkingClock clock = new TalkingClock(1000, true);
- clock.start();
-
- JOptionPane.showMessageDialog(null, "Quit program?");
- System.exit(0);
- }
- }
- class TalkingClock
- {
- private int interval;
- private boolean beep;
-
- public TalkingClock(int interval, boolean beep)
- {
- this.interval = interval;
- this.beep = beep;
- }
-
- public void start()
- {
- ActionListener listener = new TimePrinter();
- Timer t = new Timer(interval, listener);
- t.start();
- }
-
- private class TimePrinter implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- Date now = new Date();
- System.out.println("At the tone, the time is " + now);
- if (beep)
- Toolkit.getDefaultToolkit().beep();
- }
- }
- }
复制代码 局部内部类不能用public或private进行声明,其作用域被限定在声明这个局部类的块中。
局部类可以访问局部变量,前提是局部变量被声明为final。
匿名内部类的语法格式为:
new SuperType(construction parameters)
{
inner class methods and data
}
匿名类不能有构造器,在实现接口的时候,不能有构造参数。
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.Timer;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- TalkingClock clock = new TalkingClock();
- clock.start(1000, true);
-
- JOptionPane.showMessageDialog(null, "Quit program?");
- System.exit(0);
- }
- }
- class TalkingClock
- {
- public void start(int interval, final boolean beep)
- {
- ActionListener listener = new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- Date now = new Date();
- System.out.println("At the tone, the time is " + now);
- if (beep)
- Toolkit.getDefaultToolkit().beep();
- }
- };
- Timer t = new Timer(interval, listener);
- t.start();
- }
- }
复制代码 静态内部类不会引用外部类对象,无法访问外部类的数据域。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- double[] d = new double[20];
- for (int i = 0; i < d.length; i++)
- d[i] = 100 * Math.random();
- ArrayAlg.Pair p = ArrayAlg.minmax(d);
- System.out.println("min = " + p.getFirst());
- System.out.println("max = " + p.getSecond());
- }
- }
- class ArrayAlg
- {
- public static class Pair
- {
- private double first;
- private double second;
-
- public Pair(double f, double s)
- {
- first = f;
- second = s;
- }
-
- public double getFirst()
- {
- return first;
- }
-
- public double getSecond()
- {
- return second;
- }
- }
-
- public static Pair minmax(double[] values)
- {
- double min = Double.MAX_VALUE;
- double max = Double.MIN_VALUE;
-
- for (double v : values)
- {
- if (min > v) min = v;
- if (max < v) max = v;
- }
-
- return new Pair(min, max);
- }
- }
复制代码
|
|