In-depth understanding of object-oriented

In-depth understanding of object-oriented

This article has been exclusively licensed to scripts House (ID: jb51net) Public Publication

introduction

First contact with an object-oriented in large semester of the Java programming language designed this course, listening to the teacher talked about all kinds of nouns: classes, objects, interfaces, inheritance, polymorphism, encapsulation ....... It was also a smattering of knowledge, just know these things, time is also used Yihuhuhuapiao, and can not understand their true design content. With the increase of project experience, and gradually appreciate the subtlety lies a little programming, coupled with some experience reading all kinds of books, so ready to write this article in a series, share exchange.

If the programmer compared swordsman, then there are two most important things: the sword and fencing. Sword can be understood as the real deal in a variety of hard skills, such as: Java, MySQL, SpringBoot ......, these things are dead, knowledge point is very clear, in a timely manner with a gap between others, doing something can always catch up; while fencing is program design of the road, such as: requirements analysis, architecture design, these things back and forth relative to live pan a lot, because in this process is no longer a hot machine you are facing, but a wide variety of customers, complex business logic and colleagues of different ideas. So, the difference code programmers is that farmers and programming aspects. Design itself is an art, those who command the army of ten million codes CTO who is also a talented strategist generals artist ah!

Object oriented to face from the machine

Computer program code is the beginning of 0101, to complete the machine in binary thinking to think; but then have the assembler coding form has changed, but the essence has not changed, programmers focus is still the machine itself, the program to members of the CPU, registers and other hardware well known; when the language C language, COBOL and other process-oriented turned out, this is a revolution in essence, the programmer rescued from the binary world, can finally get them. " like people "to think it.

At this time, programmers can focus on the problem itself, to sort out the specific steps to solve the problem, then tell the computer program language can be friends. For a single, specific question whether or not their complexity, the use of process-oriented language can be completely hold live. This is like the general assembly line, a chain, a problem complicated process is nothing more than a little more of it, as a whole is not difficult. But with the development of computer hardware, people want to use transaction processing computer more and more, increasingly complex business logic, process-oriented language then this time to achieve such a chaotic business logic a bit overwhelmed by the. Like many production lines together in the doping, a process dependent B line A line, B line and C-line dependent on a step, and the A line and the C-line are also associated with; ordinary pipeline such messy look competent It will certainly be a large head. At this point it is an object-oriented programming language required to kill four of the big.

This object-oriented design is more suitable for the simulation of the real world, more suitable for handling a systemic problem. It is no longer a specific point as the center, but rather focus on the entire surface. Abstract class with a group of common things, with good interface definition exchange rules between different things, inheritance way to make the same kind of thing can be wonderful, with a polymorphic way to make things different styles can be uniform. Each programmer is the code of the world God, object-oriented thinking to design the architecture of the whole world, to face the thought processes of each strain to study the world of civil engineering before they reach the flowers and the world, a Bodhi leaf of realm.

Object-oriented Three Musketeers

class

Birds of a feather flock together. - "Let the Warring States policy three"

It is the cornerstone of the entire class of object-oriented design theory, only to find out it out to the class the whole intellectual edifice has a solid foundation.

First programming aside, just from the point of view of the real world point of view: the gun to defend the motherland of people called the military, people who work on the site called workers in office buildings inside the office of people called white-collar workers. For each category, they each individual is different, but they all have similar characteristics, so we put them classified as a class of people. For the most fundamental idea is to simulate real-world objects, the programming language used to express business logic. So the program can be understood in the class: class is a group of similar things collectively, is their common kind of induction. Classes have properties and methods, from the point of view they belong to the military forces, stationed in the area ...... this is a property of soldiers; they can shoot, can capture ...... this is their method.

In fact everything in the world Jieke into one group, because as long as there are similarities, there are attributed to them according to category. How to design a class that should it? Remember the phrase: cross as many different angles, near and far different level. Angle view of the decision, must be combined with specific needs to consider the design of the class.

Class has properties and methods, then how to divide the properties and methods? We should follow the principles of minimalism. I.e. atomization properties, unitary method. Properties can not be refined to split method can only do one thing. Such decoupling can maximize, to meet the system's scalability and ease of modification.

Objects

For subsequent cognitive class definition, the object is easier to understand.
A specific example is the class Object. To the military, for example, Huang Jiguang, Qiu Shaoyun is the military among a group of people in this specific instance.

In fact, about classes and objects also can delve into, why have class there will be an object? What classes and objects in their role? This time we still have to return to the real world. China a lot of people, then the party and the government on how to manage our organization do? First, different people have different professions, such as doctors, teachers, soldiers, workers and so on. Government to develop according to different occupational categories of the different policies, such as the military ticket priority, teachers visit some of the attractions for free. In fact, according to different occupations have different management strategies is a collation is an abstraction.

Abstract nature is to isolate interference summarize common, reduce the complexity of things from a higher perspective on the issue, in order to better control the overall situation. So class is an abstraction for programmers tool that allows us to better control the entire global project, organization and management of the entire project. Party and state leaders proposed building a moderately prosperous society, put forward the theory of socialist core values, and some ordinary people appear to be empty cliches, because their vision is limited to their own third of an acre, see the specific affairs, and much learning is the heart of the whole country, he said is based on the whole of China, for hundreds of millions of Chinese people have with words. So after much study of China's national conditions highly abstract summarizing to make such a theory!

For programmers, the class is abstract, the object is concrete, have a class to have the perspective of God, to overlook the whole system; hundreds of millions of people have to have the object of labor to create value.

Class .png

interface

"Interface" The word is being abused, the most indiscriminate use of the term. Despite other interface aside, here mainly talk about object-oriented interface. If you look at the definition of a textbook, it's easy to understand what specific interfaces are:

Java interface is a declaration of a series of methods, is a collection of some of the features of the method, an interface features not only the method of implementation, so these methods can be implemented in different classes in different places, but these implementations can have different behavior ( Features). -- Baidu Encyclopedia

But just look at these definitions cold, but it is hard to understand, especially for the novice, why use an interface? What role is the nature of the interface? In order to understand this, still we have to return to the real world again. Think around what interfaces: Micro usb, Type-C, HDMI ......, how to use these interfaces? Because the phone is connected to the power supply, connect the monitor to the computer ......, from which we once abstract Summary: Interface are two different things at a level interaction, it is a set of standards, rules defining the interaction between the two sides should follow. From a procedural point of view: this rule is a collection of related functions point. For chestnut: There are a mouse class, when one day we object mouse class need to interact with the target host class, in which case it should be called to achieve a USB interface, before interacting.

From a design perspective: the interface looks at some specific functions, is a norm and constraint; classes focus on a certain type of things in common, it is a summary and abstract.

Three core features

Package

The concept is easy to understand literally, it is to wrap up a bunch of stuff closed. Now let's delve into what its design content. In my opinion the benefits package is mainly reflected in two things: reduce the complexity of external, internal data protection. In fact, for individual developers, one person wrote the entire project, the second point does not necessarily understand the benefits to. Especially write attributes get () and set () method of the time, always feeling superfluous. But when faced with a systematic project needs people to work together, meaning the package is manifested.

First: can someone specializing in dry, everyone manage their third acre of land on OK, Foreign expose their functionality is implemented on the line, and when others use modules only need to call the appropriate method on the line, do not worry about its implementation.

Second: to prevent bad people do bad things, protect the core business. For example, black withdrawals do process development in a banking system project team, after taking over when the customer needs to be called black money provided by the project manager

public Boolean updateBalance(Float money,Int operation)

This method to modify the balance, rather than the user can access this critical balance of properties directly. If the package is not performed, when the black rage competent day meal, black point wants to engage in the destruction, the code can not be modified to the user to withdraw money, the subtraction operation is not performed, but the user direct access to the balance of the properties which he added some money.

inherit

We inherited this mechanism is estimated to be the most used, we can easily make changes to the parent class and expansion through inheritance. Inherited from the designer's point of view from a specific process to the abstract, architecture phase we built on a main frame main business logic code, built various base class, which is a process of abstraction summarized, detailed design phase of our according to a particular base class and service logic to construct various subclasses, this is a process of expanded concrete.
Flow diagram .png

Polymorphism

Polymorphic literally be readily appreciated that various forms thing. Monkey King seventy change, whether it is turned to stone, old man, by its very nature the trees are still a Monkey King, magic spells are also useful for him. The advantage is that the differences can be masked subclass object, so that programmers can write code versatility, without the need for different subclasses of different writing code, make code easier to expand.

Monkey magic spell itself is a method for the Monkey King, a long incantations monkey unbearable headache, and later became an old man Monkey King, Sun Wukong monk do not specifically go for the old man to study the morphology of a magic spell, only to read the original is still effective. For this magic spell monk who would have the versatility and scalability, no matter how you become a monkey, Monkey can be maintaining the status quo!

to sum up

Swords are stereotypes, but fencing is indeed flexible, though fencing is also a set of rules, but in actual combat is ever-changing, the shape great. The so-called good horse with a good saddle, Internet technology has matured as a developer we have countless sword, but with another powerful sword they have set themselves with a sword in order to mature in the heart of the play its power. As programmers swordsman general, there is a triple way swordsman:

Sword in hand, heart, sword, the sword one person, the first state;

Sword in hand, heart, sword, kills, second state;

Sword in hand, a mind free from the sword, and will not use force to subdue the enemy, which was the third realm!

As a programmer I do not know where we are now in the realm of what?

Published 15 original articles · won praise 8 · views 5953

Guess you like

Origin blog.csdn.net/crxk_/article/details/102615234