Lecture 4: Interface Design

Tham khảo bài thuyết trình 'lecture 4: interface design', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Lecture 4: Interface Design concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but a set of requirements for classes that want to conform to the interface . public interface Comparable { int compareTo(Object otherObject); } this requires that any class implementing the Comparable interface contains a compareTo method, and this method must take an Object parameter and return an integer Interface declarations The declaration consists of a keyword interface, its name, and the members Similar to classes, interfaces can have three types of members constants (fields) methods nested classes and interfaces Interface member – constants An interface can define named constants, which are public, static and final (these modifiers are omitted by convention) automatically. Interfaces never contain instant fields. All the named constants MUST be initialized An example interface Interface Verbose { int SILENT = 0; int TERSE = 1; int NORMAL = 2; int VERBOSE = 3; void setVerbosity (int level); int getVerbosity(); } Interface member – methods They are implicitly abstract (omitted by convention). So every method declaration consists of the method header and a semicolon. They are implicitly public (omitted by convention). No other types of access modifiers are allowed. They can’t be final, nor static Modifiers of interfaces itself An interface can have different modifiers as follows public/package(default) abstract all interfaces are implicitly abstract omitted by convention To implement interfaces in a class Two steps to make a class implement an interface 1. declare that the class intends to implement the given interface by using the implements keyword class Employee implements Comparable { . . . } 2. supply definitions for all methods in the interface public int compareTo(Object otherObject) { Employee other = (Employee) otherObject; if (salary . | Lecture 4: Interface Design concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but a set of requirements for classes that want to conform to the interface . public interface Comparable { int compareTo(Object otherObject); } this requires that any class implementing the Comparable interface contains a compareTo method, and this method must take an Object parameter and return an integer Interface declarations The declaration consists of a keyword interface, its name, and the members Similar to classes, interfaces can have three types of members constants (fields) methods nested classes and interfaces Interface member – constants An interface can define named constants, which are public, static and final (these modifiers are omitted by convention) automatically. Interfaces never contain instant fields. All the named constants MUST be initialized An example interface Interface Verbose { int SILENT = 0; .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU XEM NHIỀU
TÀI LIỆU MỚI ĐĂNG
Đã 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.