tailieunhanh - Effective Java Programming Language Guide phần 6

cho phép triển khai nhiều có một trong lợi thế lớn hơn sử dụng giao diện: Nó là dễ dàng hơn để phát triển một lớp trừu tượng hơn là để phát triển một giao diện. Nếu, trong một thông cáo tiếp theo, bạn muốn để thêm một phương pháp mới để một lớp trừu tượng, bạn luôn có thể thêm một phương pháp cụ thể | Effective Java Programming Language Guide Bit-flag variant of int int int int int enum pattern public public public public static static static static final final final final SUIT_CLUBS SUIT_DIAMONDS SUIT_HEARTS SUIT_SPADES 1 2 4 8 public static final int SUIT BLACK SUIT_CLUBS SUIT_SPADES Representing sets of enumerated type constants in this fashion is concise and extremely fast. For sets of typesafe enum constants you can use a general purpose set implementation from the Collections Framework but this is neither as concise nor as fast Set blackSuits new HashSet While sets of typesafe enum constants probably cannot be made as concise or as fast as sets of int enum constants it is possible to reduce the disparity by providing a special-purpose Set implementation that accepts only elements of one type and represents the set internally as a bit vector. Such a set is best implemented in the same package as its element type to allow access via a package-private field or method to a bit value internally associated with each typesafe enum constant. It makes sense to provide public constructors that take short sequences of elements as parameters so that idioms like this are possible new SuitSet A minor disadvantage of typesafe enums when compared with int enums is that typesafe enums can t be used in switch statements because they aren t integral constants. Instead you use an if statement like this if suit else if suit elSe if suit else if suit . else throw new NullPointerException Null Suit suit null The if statement may not perform quite as well as the switch statement but the difference is unlikely to be very significant. Furthermore the need for multiway branches on typesafe enum constants should be rare because they re amenable to automatic method dispatching by the JVM as in the Operator example. Another minor performance .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.