Oop mechanism in big talk Programming Language

Source: Internet
Author: User

Oop is the object-oriented programming (Object-Oriented Programming ).
Programming Language, oopl), Program Design Using objects, classes and their related concepts.

The understanding of object-oriented language itself is endless, but the object-oriented programming language has some characteristics that can be explored. Let's briefly talk about our understanding of the characteristics of object-oriented language.

1. You must have the concept of a class. The defined class can be instantiated.

The reason why object-oriented replaces general programming is that she described the real-life situations more accurately in programming languages. The concept and instantiation of classes are all specific manifestations of the real world in programming languages. The advantage of this is that it is easy to maintain, just like in real life, where errors occur and where changes are made, to the greatest extent possible to avoid the embarrassment of "getting started and moving all over the body.

For example, people eat. Here, if we follow the idea of non-object-oriented, We will completely understand how this person eats this meal and what kind of meal this meal is like in a piece of code, all the details will be displayed in a code body. However, the object-oriented thinking does not encourage such writing, and the object-oriented performance is closer to reality. First, define the class of people, and then add various methods (response after hunger, stuttering, stuttering, response after hunger, etc.) and attributes (meal size, this decouples the code. In this way, the amount of code is increased, but the benefits are considerable. Because once the demand changes, non-object-oriented design needs to be changed from the ground up, from this person to the new description of the entire meal Process, consistent description until the end of the entire process. This is not necessary for Object-Oriented objects. If a person changes, you only need to change the person class. Other classes share the same principle.

2. Inheritance and class hierarchy

Another attractive aspect of Object-oriented thinking is the inheritance mechanism. The parent class has any feature subclass that can be selected for inheritance. Child classes do not need to be written from scratch, to improve programming efficiency.

It may be better understood to map to real life. As we all know, all creatures are evolved from simple creatures step by step, and today's various creatures are still evolving (although very slowly ). What is evolution, to put it bluntly, is to inherit the excellent genes of the fathers, and then they are constantly mutating. Isn't that inheritance in object-oriented systems? The parent class method can be inherited effectively (excellent), and useless (inferior) methods will be replaced (overwritten ), you can also have methods or attributes that are unique to you (not available to the parent class. To put it more bluntly, people evolved from monkeys, instead of being made out of nature. In this way, nature only needs to make a slight transformation on the basis of monkeys to generate people, how wise nature is!

3. Objects, message transmission, and methods

The class is ready, and the objects obtained by instantiating the class are also available. The following is to use these objects to complete your operations.

The method of operating the object we are familiar with is "." An object name followed by a "." followed by the method or attribute of the object. This is a message transmission method. Some messages require a set of parameters, which are required by the methods we are familiar. For a class, its definition of the method interface specifies the message transmission protocol of the instance, and it determines the valid range of message transmission. Because a class is constructed prior to an object, a class provides a predictable interaction mode for its instance. To put it bluntly, the method defined in the class can be used by instantiating the class, but it must strictly follow the definition in the class. That is to say, although everyone can eat, you must indicate who (specific to the object) is eating when taking this action, because it makes no sense to eat a person out of thin air.

4. Object Reference

If you are not familiar with the text description of this feature, you must know this in Java or C ++.

Let's talk about what this is. This indicates in oopls that it references itself. This mechanism makes it unnecessary to consider the details of the relationship with the object during the design and implementation of the method, but rather from a higher level of abstraction, that is, design the behavior characteristics of the same type of objects from the perspective of classes.

Let's take an example of eating. We wrote this in the way people eat: put the food in your mouth with your hands. Two attributes are involved, one is the hand and the other is the mouth. Who's hand, who's mouth, can't put the stuff in my hand into your mouth when I eat (that's not to let me eat, that's how I feed you ). Therefore, when defining a person's class, the better way to eat this method is to write this: put my hands in my mouth. Here, "I" is only a code name, and "I" may be Xiao Li or Xiao Wang, depending on the name of the person instantiated. Therefore, we use this to represent myself in the class, so we have this plus "." To reference our own methods or attributes.

5. Overriding)

With the support of a dynamic binding mechanism, the Child class replaces the corresponding implementation in the parent class with the appropriate requirements while inheriting the parent class interface.

Resetting is also called rewriting. If you are not familiar with Chinese characters, you should be familiar with English. In simple terms, the subclass inherits the method of the parent class, but feels that the method of the parent class is defective, and the method content is redefined. In the real world, humans naturally inherit the "eat" method after being mapped to monkeys (which is more accurate to say that it should be a certain kind of animal), but they redefine this method. Generally, norients are raw, while humans have invented many kinds of eating (frying, frying, etc.), which is a reset (rewriting ). The inheritance code is more efficient, because the method of eating in the class of monkeys (or monkeys) may not meet people's requirements, you only need to reset this method, and the rest can be unblocked.

6. Category

Classifier is a parameter polymorphism mechanism widely used in programming languages. It is no exception in oopls. If you have used template classes in C ++ or generics in C #, you will be familiar with this concept. What I understand is a kind of polymorphism mechanism implemented by different parameter types.

Taking eating as an example, a meal can be defined as a class, but it also includes steamed bread and rice, there should be different ways to eat different meals. Therefore, in this method, the parameter rice should be a class, and this method should be implemented according to the specific situation of the class. That is to say, during the meal, you should first determine whether it is rice or steamed bread and then perform the specific operation (using hands or chopsticks ).

7. No instance class

The so-called no-instance class is the abstract class we often say in C ++ and Java. So what is the role of an abstract class?

Abstract classes and abstract methods are also used as we often mention. I would like to explain why abstract classes and abstract methods are used, especially abstract methods. It is just a shell of a method. I think the advantage of doing so is encapsulation and polymorphism.

Also take people for dinner, if people are defined as a common class, then animals can be defined as an abstract class, which contains abstract methods. In this way, when the class inherits the abstract class of the animal, it naturally has the method of eating (encapsulation) however, human eating is different from eating other animals. Therefore, the method of human eating is implemented in the human class (polymorphism ). In this way, the abstract class is a high-level abstraction of the class, fully reflecting the characteristics of object-oriented encapsulation and polymorphism. In the end, it is still for efficient coding to minimize code duplication.

The above seven features are oopl's OOP mechanism. From these seven features, these are specific manifestations of object-oriented encapsulation, inheritance, and polymorphism.

For object-oriented users, we are like a very small child -- I have a long way to go.



[Back To Directory]
[Next]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.