In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you an analysis of the principles of java object-oriented programming. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
The core of Java is object-oriented programming. In fact, all Java programs are object-oriented and you have no choice. This is different from C++, where you can choose whether to program or not. Object-oriented programming is inextricably linked to Java, so you must understand its basic principles before you write even the simplest Java program. Therefore, let's start with the concept of object-oriented programming.
1 two paradigms
We know that all computer programs are made up of two types of elements: code and data. In addition, conceptually, a program can be organized and written around its code or data. In other words, some programs are written around "what is happening", while others are written around "who will be affected". These two paradigms determine the construction method of the program. The first method is called process-oriented model (process-oriented model), and the programs written with it have the characteristics of linear execution. The process-oriented model can be thought of as code acting on data, and it is quite successful for procedural languages like C to adopt this model. However, as mentioned in Chapter 1, problems arise when the program becomes larger and more complex.
In order to manage the increasing complexity, the second way, object-oriented programming (object-oriented programming), was conceived. Object-oriented programming organizes programs around its data (that is, objects) and interfaces that are strictly defined for that data. Object-oriented programs actually use data to control access to code. As you will see below, transforming the controlled entity into data can benefit the program in several ways in terms of organizational structure.
2 Abstract
A substantial element of object-oriented programming is abstraction. People deal with complexity through abstraction. For example, instead of thinking of a car as a device made up of tens of thousands of independent parts, people think of a car as an object with its own unique behavior. This abstraction makes it easy to drive a car to the grocery store without being overwhelmed by the complexity of the parts that make up the car. They can ignore the working details of the engine, transmission and braking system and use the car as a whole.
Using hierarchical classification is an effective way to manage abstractions. It allows you to break down complex systems into more manageable pieces in a physical sense. From the outside, the car is an independent object. Once inside, you will see that the car consists of several subsystems: driving system, braking system, sound system, seat belt, heating, portable phone, and so on. Further subdivided, these subsystems are made up of more dedicated components. For example, the sound system consists of a radio, a CD player, and perhaps a tape player. The important lesson from this is that you manage complex cars (or any other complex system) through levels of abstraction.
The hierarchical abstraction of complex systems can also be used in computer programming. The data of traditional process-oriented program can be represented by several component objects after abstraction, and the process steps in the program can be regarded as message collection between these objects. In this way, each object has its own unique behavior characteristics. You can think of these objects as concrete entities and let them react to the news telling them what to do. This is the essence of object-oriented programming.
Object-oriented concepts are at the core of Java, and it is important for programmers to understand how these concepts are translated into programs. You will find that in any major software engineering project, software will inevitably go through a life cycle of concept presentation, growth and aging, while object-oriented programming can make the software calm and adaptable at every stage of the life cycle. For example, once you have defined objects and concise, reliable interfaces to them, you can easily and confidently remove or replace some parts of the old system.
3 three principles of object-oriented programming
All object-oriented programming languages provide mechanisms to help you implement object-oriented models. These mechanisms are encapsulation, inheritance, and polymorphism. Now let's take a look at their concepts.
Encapsulation
Encapsulation is a programming mechanism that binds code and the data it processes, which ensures that both programs and data are free from external interference and misuse. One way to understand encapsulation is to think of it as a black box that prevents externally defined code from accessing internal code and data at will. Access to the code and data in the black box is strictly controlled through a properly defined interface. If you want to compare with something in real life, consider automatic transmission in the car. Automatic transmission contains hundreds of bits of information about the engine, such as what acceleration you are moving at, the slope of the road you are driving, and the current gear. As a user, there is only one way you can influence this complex package: move the gear lever. For example, you cannot affect the transmission by using a turn signal or a windshield wiper. So the gear drive rod is the only interface that connects you to the drive. In addition, any operation within the transmission object will not affect the external object, for example, the gear transmission will not turn on the headlights! Because automatic transmission is encapsulated, any automaker can choose a way to achieve it that suits them. However, from the driver's point of view, they are all used for the same purpose. The same idea can be used for programming. The advantage of encapsulating code is that everyone knows how to access it, but you don't have to worry about its internal implementation details and don't be afraid of the negative effects of improper use.
The basic unit of Java encapsulation is the class. Although the class will be described in more detail in later chapters. It is still necessary to have a brief discussion about it. A class defines the structure and behavior (data and code) that will be shared by a set of objects. Each object of a given class contains the behavior and structure of the class definition as if they were cast from the mold of the same class. For this reason, an object is sometimes thought of as an instances of a class of a class. Therefore, a class is a logical structure, while an object is a physical entity that really exists.
When you create a class, you specify the code and data that make up that class. In general, these elements are called members of the class. Specifically, the data defined by a class is called a member variable (member variables) or an instance variable (instance variables). The code that manipulates the data is called a member method (member methods) or a method for short (methods). If you are familiar with the function +, you can understand it this way: what the Java programmer calls a method is what the programmer calls a function. In programs written entirely in Java, methods define how to use member variables. This means that the behavior and interface of a class are defined by methods that manipulate its instance data.
Since the purpose of a class is to encapsulate complexity, there should be a hidden implementation complexity mechanism within the class. Each method or variable in a class can be marked private (private) or public (public). The public interface of a class represents everything that external users of the class need to know or can know; private methods and data can only be accessed by the member code of one class, and no other code that is not a member of the class can access private methods or variables. Since private members of a class can only be accessed by other parts of the program through the class's public methods, you can guarantee that things you don't want to happen will not happen. Of course, the public interface should be carefully designed not to expose the inner contents of the class too much.
Inherit
Inheritance is the process by which one object acquires the properties of another object. Inheritance is important because it supports the concept of classification by layer. As mentioned earlier, most knowledge can be managed by hierarchy (that is, from top to bottom). For example, noble hounds are part of dogs, dogs are part of mammals, and mammals are part of animals. If we don't use the concept of hierarchy, we have to define all the attributes of each animal separately. With inheritance, an object simply defines properties that make it unique in its class, because it can inherit all common properties from its parent class. So, it can be said that it is the inheritance mechanism that makes it possible for an object to become a specific instance of a more generic class. Let's discuss this process in more detail.
Most people think that the world is made up of objects, which are interrelated according to hierarchical structures such as animals, mammals and dogs. If you want to describe animals in an abstract way, you can describe them in terms of size, intelligence, and the type of skeletal system. Animals also have definite behavior, and they also need to eat, breathe, and sleep. This description of attributes and behavior is the definition of animals.
If you want to describe a more specific animal class, such as mammals, they will have more specific properties, such as tooth type, breast type, and so on. We say that mammals are a subclass of animals (subclass), while animals are superclass of mammals.
Because mammals are animals that need to be more precisely defined, they can inherit all attributes from animals. A deeply inherited subclass inherits all the attributes of each of its ancestors at the class level (class hierarchy).
The interaction between inheritance and encapsulation. If a given class encapsulates some attributes, any of its subclasses will have the same properties and add their own unique attributes. This is a key concept that object-oriented programs grow linearly rather than geometrically in complexity. The new subclass inherits all the attributes of its ancestors. It does not interact unexpectedly with most of the rest of the system.
Polymorphisms
Polymorphism (from Greek, meaning "multiple forms") is a feature that allows an interface to be used by multiple similar actions, and which action is relevant to the application. Let's take a LIFO stack as an example. Suppose you have a program that requires three different types of stacks. One stack is for integer values, one for floating-point values, and one for characters. Although the data types stored in the stack are different, the algorithm for implementing each stack is the same. If you use a non-object-oriented language, you need to create three different stack programs, each with a name. However, if you use Java, because of its polymorphism, you can create a generic stack assembly that shares the same name.
The concept of polymorphism is often described as "one interface, multiple methods". This means that a common interface can be designed for a set of related actions. Polymorphism allows the same interface to be used by multiple actions of the same class, thus reducing the complexity of the program. It is the compiler's task to choose the specific specific action (that is, the method) that is applied to each situation, and the programmer does not have to choose it manually. You just need to remember and use the universal interface.
Take a dog as an analogy. A dog's sense of smell is polymorphic. If a dog smells a cat, it will bark and run after it. If the dog smells the food, it will secrete saliva and run to the bowl containing the food. In both cases, the same olfactory organ is working, and the difference lies in what smell is smelled, that is, there are two different types of data acting on the dog's nose! This general concept can also be used when using methods in a Java program.
Interaction of polymorphism, encapsulation and inheritance
If used properly, programs that are more robust and scalable can be written in a programming environment consisting of polymorphism, encapsulation and inheritance than in a process model-oriented environment. A well-designed class hierarchy is the basis for reusing programs that you have spent time and effort to improve and test. Encapsulation allows you to upgrade and migrate programs without destroying the code that depends on the class's common interface. polymorphism helps you write programs that are clear, easy to understand, easy to read, and easy to modify.
In the first two real-life examples, cars are a more comprehensive illustration of the advantages of object-oriented design, and it is interesting to use dogs as analogies to introduce inheritance. Generally speaking, cars are very similar to procedures, and all drivers can quickly master the skills of driving different types of vehicles (subtypes) by virtue of inheritance. Whether it's a school bus that picks up students, or a Mercedes private car, a Porsche or a family car, drivers can almost always find the steering wheel, brake and accelerator and know how to operate it. After a period of driving, most people even know the difference between manual and automatic, because they fundamentally understand the superclass of these two gears-transmission.
People always see packaged features on cars. Brakes and pedals hide incredible complexity, but the interface is so simple that your feet can operate them! The size of the engine, brake, and tire has no effect on how you define the interface of the pedals.
The final attribute, polymorphism, is fully reflected in the ability of automakers to offer multiple choices based on the same vehicle. For example, the braking system can be divided into positive lock and reverse lock, the steering wheel can be divided into with or without power, and the engine can be divided into 4, 6 or 8 cylinders. No matter what the setting, you have to put your foot on the brake to stop, turn the steering wheel to turn, and press the clutch to brake. The same interface can be used to control many different implementation processes.
As you can see, through the principles of encapsulation, inheritance and polymorphism, individual parts make up the object of the car. The same is true in computer programming. Through the use of object-oriented principles, various complex parts of the program can be combined into a consistent, robust, maintainable program as a whole.
The above is the analysis of the principles of java object-oriented programming shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.