赛派号

煮茶壶十大品牌排名第一名 Interface (Java)

Concept in the Ja computer programming language

An interface in the Ja programming language is an abstract type that is used to declare a behior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final). All methods of an Interface do not contain implementation (method bodies) as of all versions below Ja 8. Starting with Ja 8, default[1]: 99  and static[1]: 7  methods may he implementation in the interface definition.[2] Then, in Ja 9, private and private static methods were added. At present,[when?] a Ja interface can he up to six different types.[clarification needed] Such interfaces exist in C#.

Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class. Object references in Ja may be specified to be of an interface type; in each case, they must either be null, or be bound to an object that implements the interface.

One benefit of using interfaces is that they simulate multiple inheritance. All classes in Ja must he exactly one base class, the only exception being ja.lang.Object (the root class of the Ja type system); multiple inheritance of classes is not allowed. However, an interface may inherit multiple interfaces and a class may implement multiple interfaces.

Overview[edit]

Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship. For instance, a human and a parrot can both whistle; however, it would not make sense to represent Humans and Parrots as subclasses of a Whistler class. Rather they most likely be subclasses of an Animal class (likely with intermediate classes), but both would implement the Whistler interface.

Another use of interfaces is being able to use an object without knowing its type of class, but rather only that it implements a certain interface. For instance, if one were annoyed by a whistling noise, one may not know whether it is a human or a parrot, because all that could be determined is that a whistler is whistling. The call whistler.whistle() will call the implemented method whistle of object whistler no matter what class it has, provided it implements Whistler. In a more practical example, a sorting algorithm may expect an object of type Comparable. Thus, without knowing the specific type, it knows that objects of that type can somehow be sorted.

For example:

interface Bounceable { double pi = 3.1415; // Note the semicolon // Interface methods are public, abstract and never final. // They are usually meant as prototypes, but default implementations are allowed void setBounce(); }

An interface:

declares only method headers and public constants. cannot be instantiated. can be implemented by a class.[1]: 75  cannot extend a class. can extend several other interfaces.[1]: 87  Usage[edit] Defining an interface[edit]

Interfaces are defined with the following syntax (compare to Ja's class definition):

[visibility] interface InterfaceName [extends other interfaces] { constant declarations abstract method declarations static method declarations }

Example:

public interface Interface1 extends Interface2, Interface3, Interface4;

The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required. Since the interface specifies a set of exposed behiors, all methods are implicitly public.

Thus, a simple interface may be

public interface Predator { boolean chasePrey(Prey p); void eatPrey(Prey p); }

The member type declarations in an interface are implicitly static, final and public, but otherwise they can be any type of class or interface.[3]

Implementing interfaces in a class[edit]

The syntax for implementing an interface uses this formula:

... implements InterfaceName[, another interface, another, ...] ...

Classes may implement an interface. For example:

public class Lion extends Animal implements Predator { @Override public boolean chasePrey(Prey p) { // Programming to chase prey p (specifically for a lion) } @Override public void eatPrey(Prey p) { // Programming to eat prey p (specifically for a lion) } }

If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods, though if any of the abstract class' subclasses do not implement all interface methods, the subclass itself must be marked again as abstract.

Classes can implement multiple interfaces:

public class Frog extends Animal implements Predator, Prey { // ... }

Interfaces can share common class methods:

abstract class Animal implements LikesFood, LikesWater { boolean likes() { return true; } }

However a given class cannot implement the same or a similar interface multiple times:

// Error: repeated interface abstract class Animal implements Shares, Shares ... { // ... }

Interfaces are commonly used in the Ja language for callbacks,[4] as Ja does not allow multiple inheritance of classes, nor does it allow the passing of methods (procedures) as arguments. Therefore, in order to pass a method as a parameter to a target method, current practice is to define and pass a reference to an interface as a means of supplying the signature and address of the parameter method to the target method rather than defining multiple variants of the target method to accommodate each possible calling class.

Subinterfaces[edit]

Interfaces can extend several other interfaces, using the same formula as described below. For example,

public interface VenomousPredator extends Predator, Venomous { // Interface body }

is legal and defines a subinterface. It allows multiple inheritance, unlike classes. Predator and Venomous may possibly define or inherit methods with the same signature, say kill(Prey p). When a class implements VenomousPredator it will implement both methods simultaneously.

Examples[edit]

Some common Ja interfaces are:

Comparable has the method compareTo, which is used to describe two objects as equal, or to indicate one is greater than the other. Generics allow implementing classes to specify which class instances can be compared to them. Serializable is a marker interface with no methods or fields - it has an empty body. It is used to indicate that a class can be serialized. Its Jadoc describes how it should function, although nothing is programmatically enforced See also[edit] Interface (object-oriented programming) Mixin Trait (computer programming) Citations[edit] ^ a b c d Bloch 2018. ^ "Default Methods". Archived from the original on 2017-05-23. Retrieved 2014-06-30. ^ "The Ja Language Specification". ^ Mitchell, John D. (June 1, 1996). "Ja Tip 10: Implement callback routines in Ja". JaWorld. Retrieved 2020-07-14. References[edit] Bloch, Joshua (2018). "Effective Ja: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991. External links[edit] What Is an Interface?

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了