Simple to understand object-oriented programming OOP--

OOP: Object-Oriented Programming

A. What is OOP

Object-Oriented Programming: Object-oriented Programming, OOP, object-oriented programming.

Object-oriented programming is a computer programming architecture , a basic principle of his computer program is a single unit can play a role subroutine or object combination.

OOP makes the program more reusability, flexibility and scalability. The core idea is that OOP: encapsulation , inheritance , polymorphism (focusing on classes and objects ).

POP different from the process-centric programming ideas (process-oriented programming), and object-oriented programming is the central idea is to achieve the objectives you want to achieve by calling the object.

Object-oriented thinking:

  1. More in line with our thinking is a habit of thinking;
  2. Complex things can be simplified;
  3. We will become a commander from the performer.

Two. OOP features

1. Packaging

Package, is simply to duplicate code by a method, a new program can be called directly our class, eliminating the need for complicated programming. Such that the package may code for " high cohesion, low coupling ", this state is the basic goal of the package. For the user, the user does not need to know how objects of various operations, users only need to want to operate by calling the object encapsulates the class. This idea package, greatly simplifies the procedure, the code becomes more efficient, reusability higher.

Package there is another purpose, which is to provide both the content does not need are hidden; the property hidden (private key), to provide public access to its methods. This enables the user can not directly access the details of the program, so that the code of the security to be improved.

2. Inheritance

Inheritance is a relationship between two or more of these categories. As the name suggests, it is the inheritance subclass contains all of the features of the parent, but the child class itself can also have their own unique methods (subclass object is a parent object). Inheritance can be divided into single inheritance and inherited: single inheritance is to say, an object inherits only from its corresponding features in another object; multiple inheritance means that one object can inherit from simultaneously to two or more of the objects the required characteristics and capabilities, and do not conflict and so on. In Java code, can only achieve single inheritance , and some other programming languages can achieve multiple inheritance (extends Java can be modified by subclasses, the subclass can inherit non-private members of the superclass).

While achieving inheritance and maintenance of the code reusability, but also makes more dependencies between classes and, that also increases the coupling inherited code (a class to complete an additional function to rely on some class called coupling). And the software design principles are: (a capacity of an individual to complete the function class), high cohesion and low coupling.

In java program, object is the top-level parent of all classes, all classes inherit it directly or indirectly. And the parent class private members of a subclass can not inherit (the parent class is modified private key content). In addition constructor does not participate in inheritance, though the subclass inherits the parent class constructor, but you can access the parent class constructor with super keyword; if the parent class has a constructor, because the subclass inherits the parent class time to use inheritance or parent class data, so before the subclass constructor to initialize the parent class to make the first run. When you create a subclass object to perform the re-structure of the parent class constructor is executed subclass.

3. Polymorphism

Inheritance is a prerequisite of polymorphism

Polymorphism refers to the method calls of the same name, but the result is different. Polymorphism is acting on the process, does not act on a class or object, the object is not an attribute. Generating polymorphic because we need programs to deal with each case separately, polymorphic meet the needs of users at the same time increase the flexibility of the code.

To produce a much states must implement a method override (subclass method declaration and appeared identical parent class (method name, parameter list, return type), occurs overridden methods, for subclasses override the parent method class), or a method overload (allowing a class, a method of the same name appears, as long as the number of parameters or different parameters can type).

Polymorphism improve the scalability of the code at the same time also have inherited characteristics (reusability and maintainability)

Three. OOP advantages

1. studious

The traditional process-oriented programming to the algorithm as the core, separate data and processes, although more reflect the views of the computer, but reduces the readability of the code, the code that is more difficult to understand. And process-oriented programming, the repetition code can be encapsulated, between classes and can be obtained by a number of methods inherited, but also how to deal with the phenomenon state information These characteristics while reducing the burden on the programmer, so that a beginner may be more good understanding of the code, do not face as process-oriented programming algorithms.

2. Stable

When the traditional process-oriented approach to modular technology, a major obstacle is impossible to achieve the same module multiple runs at the same time. Object-oriented techniques to implement the module class, with the class instantiation, using the target block to implement the queue class, class contains various operations queue. To the queue as a template can generate multiple queues objects. Each object has the same methods. But it can be operated independently at the same time, the system for various applications, thus facilitating the process-oriented solution to this development quite difficult problem.

3. Maintain good

During the maintenance of the code, the code object-oriented programming due to the difficulty of maintaining readability higher so it is smaller. Significantly reduced through inheritance redundant code and can easily extend existing code to improve coding efficiency, but also reduces the probability of errors and reduce the difficulty of software maintenance.

4. Strong reusability

Since the code to achieve the packaging, which makes a high reusability of code. Realize the function code itself is divided, but in general there are good association. By polymerization of the object, it can be combined in the package and ensure the realization of the object abstract principles inner and outer extents on the function of the structure, in order to achieve the object of the upgrade from low to high.

Four. OOP defects

1. Low operating efficiency

Due to pursue ideas encapsulated in the preparation of the program, it will create a large number of classes, and classes loaded sacrifice system performance will be such that the speed is reduced. Even if the CPU speed and memory capacity to improve, but the face of large-scale code, operating efficiency issues will still show up.

2. The library is huge

Because libraries are too large for the programmer to master them will take some time, the popularity of the promotion standpoint, the library should be to ensure the corresponding reduction in its fully functional basis.

3. Reliability library

The larger the system will be we can not predict the existence of hidden problems, programmers can not fully guarantee the library each class absolutely correct in various environments, problems occur when using the class, it will affect the subsequent work program members may also be superior to a class of error overthrow all of the original work, which brings difficulties to the programming and maintenance process.

Guess you like

Origin www.cnblogs.com/a-xia/p/11320358.html