Object-oriented programming is actually very simple--python object-oriented (elementary article)

Source: Internet
Author: User
Tags chop ming

Overview
    • Process oriented: Write base code from top to bottom according to business logic

    • Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called

    • Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."

Today we are going to learn a new way of programming: Object-oriented Programming (object Oriented Programming,oop, OO programming)
Note: Java and C # only support object-oriented programming, while Python is more flexible in that it supports object-oriented programming and functional programming

Creating Classes and objects

Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".

A class is a template that can contain multiple functions and functions in a template.

Objects are instances created from templates that can execute functions in a class through an instance object

    • Class is a keyword that indicates that classes

    • Create the object, and then add parentheses to the class name

PS: A function in a class The first argument must be self (see in detail: Encapsulation of the three main characteristics of a class)
A function defined in a class is called a "method"

Eh, do you have a question here? Using functional programming and object-oriented programming to execute a "method" is easier than an object-oriented approach

    • Object-oriented: "Create Object" "Execute method by Object"

    • Function Programming: "Execute function"

Observing the above-mentioned answers is affirmative, then not absolute, and the different scenarios are different for their programming styles.

Summary: Functional application Scenarios--independent and no shared data between functions

Object-oriented three major features

The three major characteristics of object-oriented are: encapsulation, inheritance and polymorphism.

First, the package

Encapsulation, as the name implies, encapsulates the content somewhere and then calls the content that is encapsulated somewhere.

Therefore, when using object-oriented encapsulation features, you need:

    • Encapsulate content somewhere

    • To invoke the encapsulated content from somewhere

First step: encapsulate the content somewhere


Self is a formal parameter, when executing obj1 = Foo (' Wupeiqi ', 18), self equals obj1

When executing obj2 = Foo (' Alex ', 78), self equals obj2

So, the content is actually encapsulated in objects Obj1 and Obj2, each of which encapsulates the name and age, and the previously said "content is encapsulated somewhere" which is similar to saving in content.

Step two: Call the encapsulated content from somewhere

When the encapsulated content is called, there are two scenarios:

    • Called directly through an object

    • Indirectly called through self

1. Direct invocation of encapsulated content by object

Shows how objects obj1 and Obj2 are saved in memory, so that the encapsulated content can be called according to the Save format: Object. Property name

2. Indirectly invoking the encapsulated content via self

When executing a method in a class, the encapsulated content needs to be indirectly called through self

In summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the object directly or self to obtain the encapsulated content.

Practice One : output The following information at the terminal

    • Xiao Ming, 10 years old, male, go up the hill to chop Wood

    • Xiao Ming, 10 years old, male, drive to northeast

    • Xiao Ming, 10 years old, male, favorite big health care

    • Lao Li, 90-year-old man, go up the hill to chop Wood

    • Lao Li, 90-year-old, male, drive to Tohoku

    • Lao Li, 90 years old, male, favorite big health care

    • Lao Zhang ...


------------Functional Programming-------------

-----------Object-Oriented programming-------------

The above comparison shows that if you use functional programming, you need to pass the same parameters each time the function is executed, if there are many parameters, you need to paste the copy ... , while for object-oriented only need to encapsulate all the required parameters into the current object when the object is created, and then use self indirectly to go to the current object to take the value.

Exercise two : Game Life Program

1. Create three game characters, respectively:

    • Cang Jing, Female, 18, initial combat 1000

    • Tony Wood, Male, 20, initial combat 1800

    • Wave Toto, female, 19, initial combat 2500

2, the game scene, respectively:

    • Bush fights, consumes 200 combat power

    • Self-cultivation, increased 100 combat effectiveness

    • Multiplayer game, consumes 500 combat power

Ii. inheritance

Inheritance, the inheritance in object-oriented is the same as the inheritance in real life, that is, the child can inherit the parent's content.

For example:

Cats can: Meow meow, eat, drink, pull, sprinkle

Dogs can: bark, eat, drink, pull, sprinkle

If we were to create a class for both cats and dogs, we would need to do all of their functions for cats and dogs.

Eating, drinking, pulling, and spreading are the functions of both cats and dogs, and we have written two of them in separate cat and dog classes. If you use the idea of inheritance, implement as follows:

Animals: Eating, drinking, pulling, spreading

Cat: Meow Meow (cat inherits function of animal)

Dog: Barking (dogs inherit the function of animals)

Therefore, for object-oriented inheritance, it is actually the method of extracting multiple classes common to the parent class, and the subclass inherits only the parent class without having to implement each method in one.

Note: In addition to the names of subclasses and parent classes, you may have seen derived and base classes that are only different from subclasses and parent classes.

So the question comes again, how to inherit?

    • Whether multiple classes can be inherited

    • If you have inherited multiple classes that have the same function in each class, then that one will be used?

1. Python classes can inherit multiple classes, and Java and C # can inherit only one class

2. If the Python class inherits more than one class, there are two ways to find the method: Depth first and breadth First

    • When a class is a classic class, multiple inheritance cases are searched in the depth-first way

    • When a class is a new class, in multiple inheritance cases, the breadth-first method is found

Classic class and new class, literally can see an old a new, new inevitably contain with many functions, is also recommended after the wording, from the wording of the words, if the current class or the parent class inherits the object class , then the class is a new class, otherwise it is the classic class.

Classic class: First go to a class to find, if not in Class A, then continue to the class B to find, if there is a class B, then continue to find in class D , if there is a class D, then continue to go to class C , if still not found, The error

New class: First go to class a to find, if not in Class A, then continue to the class B to find, if there is a class B, then continue to the class C , if there is a Class C, then continue to find in class D , If it is still not found, the error

Note: In the above search process, once found, the search process immediately interrupted, and will not continue to find

Three, polymorphic

Pyhon does not support polymorphism and is not polymorphic, the concept of polymorphism is used in strongly typed languages such as Java and C #, while Python advocates "duck type".

over!

Object-oriented programming is actually very simple--python object-oriented (elementary article)

Related Article

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.