Chapter 5 Java language interface

xiaoxiao2021-03-06  46

Chapter 5 Java language interface

Persons: At home, you can unplug the electric light's plug and then connect your laptop. This is because the lights and laptops implement this interface of this outlet.

What is an interface? A Java interface is a collection of method features, which is of course from a specific method, but they generally come from some methods that have emerged in the system, one interface is only the characteristics. No The implementation of the method, so these methods are implemented in different places, and can have a completely different behavior. In the Java language, the Java interface can also be used to define the constants of publications.

Why use the interface? Since the Java language is a single inherited language, a class can only have a super class. Therefore, in many cases, this specific class may already have a super class, this time you have to add a new The superclass is impossible. If necessary, you have to add this new super class to the existing superclass to form an ultra-super-class: if this super-class position has been occupied, I have to continue to move upwards until the top of moving to the class level structure. This way, the inner design of a specific class has become a modification of all classes in the entire level structure.

The interface is a guarantee for the insertability.

The associated pluggable current object is not associated with which specific class is concerned, but only does this class implements this interface.

Calling Insertability When you need to call other classes, this call is not necessarily a specific class, which can be an interface. This can be called by the current object if the specific class implementation of this interface can be called. * Contact The insertability is consistent.

Java interface common usage. Unilateral method interface

Named interface such as java.io.serializable

The constant interface is used to define constants, as long as this interface can directly call constants, not such use (utils.max)

Chapter VI Special: Abstract Class

What is an abstract class abstract class only provides a type of partial implementation. Abstract classes can have instance variables, as well as one or more constructs. Abstract classes can be abstracting methods and specific methods. A abstract class cannot be instantiated, so abstract class It is used to inherit.

Scott Meyers has pointed out that as long as it is possible, do not inherit from specific classes.

The code reconstruction suggestion abstraction should have as many common code as possible. * That is to say: In a inheritance relationship from abstract classes to multiple specific classes, the common code should be moved to abstract classes. This pair Reuse is easy.

Abstract classes should have as little data * The mobile direction of data is from abstract classes to specific classes. Also move from the high-end to the high-end hierarchy of the inheritance. This ensures saving memory resources.

Abstract programming

When should I use inheritance multiplexing (generally recommended interface)

1. Subclasses are a special type of superclass, not a role of superclars, that is, the difference between "HAS-A" and "IS-A" relationship. HAS-A uses polymerization relationship, IS- A use inheritance relationship.

2. There is never needed to change the subcategory to another class. 3. The subclass has the responsibility of the extension superclass, rather than replacing the override or cancellation of superclatrios.

4. Only inheritance can only be used when it is meaningful in the classification point of view, do not inherit from the tool class.

转载请注明原文地址:https://www.9cbs.com/read-59598.html

New Post(0)