Effective Java Programming Language Guide phần 4 ppt

18 267 0
Effective Java Programming Language Guide phần 4 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

[...]... The Java programming language provides two mechanisms for defining a type that permits multiple implementations: interfaces and abstract classes The most obvious difference between the two mechanisms is that abstract classes are permitted to contain implementations for some methods while interfaces are not A more important difference is that to implement 65 Effective Java: Programming Language Guide. .. doc comment The description begins with the phrase, “This 61 Effective Java: Programming Language Guide implementation.” This phrase should not be taken to indicate that the behavior may change from release to release It connotes that the description concerns the inner workings of the method Here's an example, copied from the specification for java. util.AbstractCollection: public boolean remove(Object... itself Finally, if you decide to implement Serializable in a class designed for inheritance and the class has a readResolve or writeReplace method, you must make the readResolve or 64 Effective Java: Programming Language Guide writeReplace method protected rather than private If these methods are private, they will be silently ignored by subclasses This is one more case where an implementation detail.. .Effective Java: Programming Language Guide There are a number of obvious violations of this principle in the Java platform libraries For example, a stack is not a vector, so Stack should not extend Vector Similarly, a property list is not a hash table so... toIndex==fromIndex, this operation has no effect.) This method is called by the clear operation on this list and its sublists Overriding this method to take advantage of the internals of the list 62 Effective Java: Programming Language Guide implementation can substantially improve the performance of the clear operation on this list and its subLists This implementation gets a list iterator positioned before fromIndex... depends on any initialization performed by the subclass constructor, then the method will not behave as expected To make this concrete, here's a tiny class that violates this rule: 63 Effective Java: Programming Language Guide public class Super { // Broken - constructor invokes overridable method public Super() { m(); } } public void m() { } Here's a subclass that overrides m, which is erroneously invoked... alternative is a bloated class hierarchy containing a separate class for every supported combination of attributes If there are n attributes in the type system, there are 2n possible 66 Effective Java: Programming Language Guide combinations that you might have to support This is what's known as a combinatorial explosion Bloated class hierarchies can lead to bloated classes containing many methods that... example is an Adapter [Gamma98, p.139] that allows an int array to be viewed as a list of Integer instances Because of all the translation back and forth between int values and Integer 67 Effective Java: Programming Language Guide instances, the performance is not terribly good Note that a static factory is provided and that the class is an inaccessible anonymous class (Item 18) hidden inside the static... class may have to provide hooks into its internal workings in the form of judiciously chosen protected methods or, in rare instances, protected fields For example, consider the removeRange method from java. util.AbstractList: protected void removeRange(int fromIndex, int toIndex) Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive Shifts any... a class implements some interface that captures its essence, such as Set, List, or Map, then you should feel no compunction about prohibiting subclassing The wrapper class pattern, described in Item 14, provides a superior alternative to inheritance for altering the functionality If a concrete class does not implement a standard interface, then you may inconvenience some programmers by prohibiting

Ngày đăng: 12/08/2014, 22:22

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan