[Java]Java Tutorial 读书笔记(Trail Two:Learning the Java Language_4_Classes and Inheritance_2_Managing Inheritance_3_Implementing Nested Classes) |
Managing Inheritance
Understanding Inheritance
子类对于父类的成员来说,可以继承(use)、隐藏(hide)、覆盖(override)。隐藏是对变量来说的;覆盖是对方法来说的。二者在表象上的共同之处在于子类在其内部定义了一个和父类相同名字的成员;对于被隐藏的变量和被覆盖的方法而言都可以通过super来访问。
对于方法的覆盖,要多说两句:
1、The overriding method can have a different throws clause as long as it doesn't declare any types not declared by the throws clause in the overridden method。
2、权限限定符,覆盖方法只能比被覆盖方法更多(the access specifier for the overriding method can allow more access than the overridden method, but not less.)
不能被覆盖的方法:final方法;static方法
必须被覆盖的方法:abstract方法
Being a descendent of Object
Method must be overrided
clone
Object里此方法的实现是先检查被clone的类是否实现了cloneable接口,没有丢出意外(注意:Object本身并没有实现cloneable
clone不能调用new或者constructor,可以用super.clone
equals/hashCode
两者必须一起被重写覆盖
finalize
toString
Method cannot be overrided
getClass
这是一个final方法,返回的是对象的类的运行时表示,返回一个Class对象;Class对象最常使用的就是编译阶段在不知道名字的情况下创建一个类的实例而在知道名字的情况下,可以从class名字得到一个Class对象,比如:
String.class
or
Class.forName("String");
notify notifyAll wait
Writing final classes and methods
在compiler和bytecode level都会提供对final的支持
使用final可能是因为security或者design的考虑
Writing abstract classes and methods
记住一点:abstract只能被继承,不能实例化
Implementing Nested Classes
当嵌套类(nested class)只有在被嵌套类(enclosing class)中才有意义或者它的功能依赖于被嵌套类时,嵌套类才应该被定义
类别:static nested class和inner class
区别:The term "nested class" reflects the syntactic relationship between two classes; that is, syntactically, the code for one class appears within the code of another. In contrast, the term "inner class" reflects the relationship between instances of the two classes.
事实上,nested class可以被定义在任何block当中
两个概念:adapter class和anonymous class
|
阅读全文(1802) | 回复(0) | 编辑 | 精华 |
|
« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
|
Blog信息 |
blog名称:循环递归 日志总数:11 评论数量:6 留言数量:0 访问次数:60259 建立时间:2005年6月18日 | |

|