[Core Java. Volume I. Fundamentals, 8th Edition]-6
来源:互联网 发布:老道消息 知乎 编辑:程序博客网 时间:2024/05/16 08:08
模板类型接口
NOTE: As of Java SE 5.0, the Comparable interface has been enhanced to be a generic type.
public interface Comparable<T> { int compareTo(T other); // parameter has type T }For example, a class that implements Comparable<Employee> must supply a method
int compareTo(Employee other)
You can still use the “raw” Comparable type without a type parameter, but then you have to manually cast the parameter of the compareTo method to the desired type. Allmethods of an interface are automatically public.Just as methods in an interface are automatically public, fields are always public static final.
inner class
内部类可以访问外部类的所有变量
outer class reference of an inner class
OuterClass.this如 if (TalkingClock.this.beep) Toolkit.getDefaultToolkit().beep();
you can write the inner object constructor more explicitly, using the syntax
outerObject .new InnerClass(construction parameters)如 TalkingClock jabberer = new TalkingClock(1000, true);
TalkingClock.TimePrinter listener = jabberer.new TimePrinter();
Accessing final Variables from Outer Methods
Local classes have another advantage over other inner classes. Not only can they access the fields of their outer classes, they can even access local variables! However, those local variables must be declared final. Here is a typical example. Let’s move theinterval and beep parameters from the TalkingClock constructor to the start method.
public void start(int interval, final boolean beep) { class TimePrinter implements ActionListener { public void actionPerformed(ActionEvent event) { if (beep) Toolkit.getDefaultToolkit().beep(); } } }final int[] counter = new int[1];
(The array variable is still declared as final, but that merely means that you can’t have it refer to a different array. You are free to mutate the array elements.)
匿名内部类
When using local inner classes, you can often go a step further. If you want to make only a single object of this class, you don’t even need to give the class a name. Such a class is called an anonymous inner class.
public void start(int interval, final boolean beep) { ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { /////// } }; }
new SuperType(construction parameters) { inner class methods and data }Here, SuperType can be an interface, such as ActionListener; then, the inner class implements that interface. Or SuperType can be aclass; then, the inner class extends that class.
- [Core Java. Volume I. Fundamentals, 8th Edition]-6
- [Core Java. Volume I. Fundamentals, 8th Edition]-7,8
- Core Java(TM), Volume I--Fundamentals (8th Edition)
- [Core Java. Volume I. Fundamentals, 8th Edition]-2,3
- [Core Java. Volume I. Fundamentals, 8th Edition]-3
- [Core Java. Volume I. Fundamentals, 8th Edition]-4
- [Core Java. Volume I. Fundamentals, 8th Edition]-5
- [Core Java. Volume I. Fundamentals, 8th Edition]-11
- <Core Java Volume I Fundamentals 9th Edition> Note 01
- Core Java. Volume I. Fundamentals, 8th Edition 下载地址及读后感
- Core Java Volume I--Fundamentals, 10th Edition.pdf 英文原版 免费下载
- Core Java 2, Volume I:Fundamentals, Seventh Edition
- Core Java Volume I - Fundamentals (10th) 1-8章 阅读笔记
- Core.Java.Volume.I,2 10th.Edition
- Core Java, Volume II--Advanced Features (10th Edition) (Core Series) 电子书(手机)
- Core Java Volume I 读书笔记
- Core.Java.Volume.Fundamentals 阅读笔记(1)
- Core Java, Vol. 2: Advanced Features, 8th Edition
- RHEL6 下搭建 nginx + uwsgi + cgi 平台
- iconv字符编码转换全攻略
- fpga三态总线实现
- 我是怎样自动机入门的
- APIHOOK
- [Core Java. Volume I. Fundamentals, 8th Edition]-6
- GridView组件的使用
- 总结和归纳http协议
- 庞果英雄会——最小操作数
- 技术备忘2
- 安卓开发13:ListView详细解读
- .net连接sqlserver并测试两个数据库
- boost在实际项目中的使用
- 100 Vim commands every programmer should know