TAILIEUCHUNG - Introduction to Programming Using Java Version 6.0 phần 4

Với một vấn đề, làm thế nào bạn có thể đến với một chương trình để giải quyết Bây giờ chúng ta đã xem xét các thành phần và bố trí, đó là thời gian để đặt chúng lại với nhau thành một số chương trình hoàn thiện. Chúng tôi bắt đầu với một bản demo đơn giản sử dụng một JLabel, JComboBox, và một vài của JSliders, tất cả đặt trong một | CHAPTER 5. OBJECTS AND CLASSES 215 public interface Drawable public void draw Graphics g This looks much like a class definition except that the implementation of the draw method is omitted. A class that implements the interface Drawable must provide an implementation for this method. Of course the class can also include other methods and variables. For example public class Line implements Drawable public void draw Graphics g . . . do something----presumably draw a line . . . other methods and variables Note that to implement an interface a class must do more than simply provide an implementation for each method in the interface it must also state that it implements the interface using the reserved word implements as in this example public class Line implements Drawable . Any class that implements the Drawable interface defines a draw instance method. Any object created from such a class includes a draw method. We say that an object implements an interface if it belongs to a class that implements the interface. For example any object of type Line implements the Drawable interface. While a class can extend only one other class it can implement any number of interfaces. In fact a class can both extend one other class and implement one or more interfaces. So we can have things like class FilledCircle extends Circle implements Drawable Fillable The point of all this is that although interfaces are not classes they are something very similar. An interface is very much like an abstract class that is a class that can never be used for constructing objects but can be used as a basis for making subclasses. The subroutines in an interface are abstract methods which must be implemented in any concrete class that implements the interface. You can compare the Drawable interface with the abstract class public abstract class AbstractDrawable public abstract void draw Graphics g The main difference is that a class that extends AbstactDrawable cannot extend any other class while a

Đã 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.