publicclassEmployeeSortTest{ publicstaticvoidmain(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);
// print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); } }
/** * Compares employees by salary * @param other another Employee object * @return a negative value if this employee has a lower salary than * otherObject, 0 if the salaries are the same, a positive value otherwise */
publicclassTimerTest{ publicstaticvoidmain(String[] args) { ActionListener listener = new TimePrinter();
// construct a timer that calls the listener // once every 10 seconds // 每十秒调用一次 listener.actionPerformed,传进去的是一个对象 Timer t = new Timer(10000, listener); // 调用start()才是正式开始运行 t.start(); // 显示一个对话框,这里主要是为了阻塞进程和方便推出,阻塞进程的意思是由这句占用主进程,打印日期和响声的代码并不是这里调用,而是回调调用的 JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } }
classTimePrinterimplementsActionListener { // 实现接口,接口 ActionListener 只有这一个方法 publicvoidactionPerformed(ActionEvent event) { System.out.println("At the tone, the time is " + new Date()); // 这句代码是响一声 Toolkit.getDefaultToolkit().beep(); } }
/** * Set the hire day to a given date. * @param year the year of the hire day * @param month the month of the hire day * @param day the day of the hire day */ publicvoidsetHireDay(int year, int month, int day) { Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime(); // Example of instance field mutation hireDay.setTime(newHireDay.getTime()); }
/** * Starts the clock. */ publicvoidstart() { ActionListener listener = new TimePrinter(); Timer t = new Timer(interval, listener); t.start(); }
// 内部类 TimePrinter publicclassTimePrinterimplementsActionListener { publicvoidactionPerformed(ActionEvent event) { System.out.println("At the tone, the time is " + new Date()); // 这里的 beep 引用的是外围的 beep if (beep) Toolkit.getDefaultToolkit().beep(); } } }
局部内部类,就是在方法内的内部类,只有方法内可以使用,其他方法不可以使用,也是可以访问所在类的属性
优点就是可以访问方法内部的局部变量
匿名内部类,如果内部类只使用了一次,即只有一次实例化,那么可以使用匿名内部类,语法为:
new SuperType(construction prarmeters)
{
innter class methods and data
}
中文版 :
new 父类或者接口名(父类构造器参数)
{
内部类的方法或者属性
}
可以对比一下正常的实例化与匿名内部类的实例化
1 2 3 4 5 6 7 8
// 正常实例化 Person queen = new Person("Mary"); // 匿名内部类 Person count = new Person("Dracula") { publicvoidprintname(){ System.out.println(this.name); } }
// keep program running until user selects "OK" JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } }
/** * A clock that prints the time in regular intervals. */ classTalkingClock { publicvoidstart(int interval, boolean beep) { // 匿名内部类 // 这里是实现了ActionListener 接口 ActionListener listener = new ActionListener() { @Override publicvoidactionPerformed(ActionEvent event){ System.out.println("At the tone, the time is " + new Date()); if (beep) Toolkit.getDefaultToolkit().beep(); } }; Timer t = new Timer(interval, listener); t.start(); } }
publicclassProxyTest{ publicstaticvoidmain(String[] args) { Object[] elements = new Object[1000];
// fill elements with proxies for the integers 1 ... 100 for (int i = 0; i < elements.length; i++) { Integer value = i + 1; // 初始话代理类 InvocationHandler handler = new TraceHandler(value); // 这里是真正的代理 将hook的方法与代理对象绑定,比如这里是Compareable的class,然后代理类就是TreaceHandler Object proxy = Proxy.newProxyInstance(null, new Class[] {Comparable.class}, handler); // 这里赋值时为了从代理调用,而不是直接调用,可以说最基本的改变就是这里 elements[i] = proxy; }
// construct a random integer Integer key = new Random().nextInt(elements.length) + 1;
// search for the key int result = Arrays.binarySearch(elements, key);
// print match if found if (result >= 0) System.out.println(elements[result]); } }