Python learning [Day7] object-oriented

Source: Internet
Author: User

Programming Pattern Concepts
    • 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 ..."
Three main characteristics of object-oriented one, encapsulation

First step: encapsulate the content somewhere

Class Teacher:    #定义了一个Teacher对象    #__init__ construction method, the constructor method in the class is automatically executed when the object is created    def __init__ (self, favor, name, age):  #self类中的所有方法必须包含self参数, self means the object itself        Self.favor = favor        self.name = name        self.age =        Age Self.asset = 0    def failure (self): #方法 "" "        teaching Accident        : return:" ""        self.asset-        = 1        Print ("Teaching accident, asset-1")
Tubie = Teacher (' car ', ' Tubie ', ') #创建对象土鳖, encapsulate car,tubie,20 separately into favor,name,age
Print (Tubie.name)

Self is a formal parameter, when executing Tubie = Teacher (' car ', ' Tubie ', 20), self equals tubie

When executing tubie2 = Teacher (' phone ', ' tubie2 ', 25), self equals tubie2

So, the content is actually encapsulated in objects Tubie and Tubie2, each object has a name and an age attribute, which is similar to saving in memory.

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

Print (Tubie.name)

2. Indirectly invoking the encapsulated content via self

Self.asset + = 1

Object-oriented examples
    • 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
classFoo:def __init__(self, name, age, gender): Self.name=name Self.age=Age Self.gender=GenderdefKanchai (self):Print "%s,%s years old,%s, go up the hill to chop Wood"%(Self.name, Self.age, Self.gender)defQudongbei (self):Print "%s,%s-year-old,%s, drive to Tohoku"%(Self.name, Self.age, Self.gender)defDabaojian (self):Print "%s,%s years old,%s, favorite big health care"%(Self.name, Self.age, Self.gender) xiaoming= Foo ('Xiao Ming', 10,'male') Xiaoming.kanchai () Xiaoming.qudongbei () Xiaoming.dabaojian () Laoli= Foo ('Lao Li', 90,'male') Laoli.kanchai () Laoli.qudongbei () Laoli.dabaojian ( )
Exercise 1

Ii. inheritance

1. Single 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

classCat:defmeow meow (self):Print 'Meow Meow'    defEat (self):#Do something    defDrink (self):#Do something    defPull (self):#Do something    defScatter (self):#Do somethingclassDog:defBarking (self):Print 'Meow Meow'    defEat (self):#Do something    defDrink (self):#Do something    defPull (self):#Do something    defScatter (self):#Do something
Logic Code

Animals: Eating, drinking, pulling, spreading

Cat: Meow Meow (cat inherits function of animal)

Dog: Barking (dogs inherit the function of animals)

classAnimals:defEat (self):#Do something    defDrink (self):#Do something    defPull (self):#Do something    defScatter (self):#Do something#write another class name in parentheses after the class, indicating that the current class inherits another classclassCat (animal):defmeow meow (self):Print 'Meow Meow'        #write another class name in parentheses after the class, indicating that the current class inherits another classclassDog (animal):defBarking (self):Print 'Meow Meow'
Logic Code

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.

2. Multiple inheritance

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 still not found, The error

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

Python learning [Day7] object-oriented

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.