Python Learning Notes-Object oriented (Basic)

Source: Internet
Author: User

Previously learned programming is through the process-oriented implementation, for some reuse of code, the further use of functions, enhance the readability and reusability of code. Python also supports object-oriented programming.


There are three main features of object-oriented:

    1. Packaging

    2. Inherited

    3. Polymorphic


First take a look at the encapsulation. The package consists of two points, encapsulating the content somewhere; invoking the encapsulated content


Example 1;


class c1:    def __init__ (self,name,obj):         self.name = name        self.obj =  objclass c2:    def __init__ (self,name,age):         self.name = name        self.age = age     def show (self):         print (Self.name)         return 123class c3:    def  __init__ (SELF, A1):        self.money = 123         SELF.AAA = A1C2_OBJ = C2 (' AA ',  11) # c2_ obj is the C2 type # - name =  "AA" # - age = 11c1_obj = c1 ("Alex",  C2_obj) # c1_obj&nbSP; is the c1  type # - name =  "Alex" # - obj = c2_objc3_obj = c3 ( C1_obj) #  use C3_obj to execute the Show Method Ret = c3_obj.aaa.obj.show () print (ret) print (C3_obj.money)--------- Aa123123

Several caveats:

    • I have defined 3 classes, each with its own construction method __init__, and I instantiate an object for each class, and each object is created to automatically call its own __init__ method to encapsulate different content;

    • Self is a formal parameter, which is equivalent to an instance, such as when C1_obj=c1 (' Alex ', C2_obj), self is equal to C1_obj

    • When we output money this field, C3_obj can call show this method to output money (indirectly called self) or direct output C3_obj.money (direct call)

    • The object itself can also be passed as a parameter to other classes



The concept of encapsulation has, if you remember the previous pickle, we can save the custom structure to a file.

s1.py

Class Foo:def __init__ (self, name): Self.name = name def show (self): print (self.name) Import Pickleobj = Foo (' Alex ') pickle.dump (obj, open (' db ', ' WB '))


If the pickle serialized file is called in another file, the corresponding class must be imported, otherwise it will not be recognized

s2.py

Import picklefrom S1 Import Fooret = pickle.load (open (' db ', ' RB ')) print (ret)---------------<s1. Foo Object at 0x000001e5f421beb8>



Next look at inheritance, subclasses can inherit everything from the parent class.


First look at the example of single inheritance

Class F1: # parent class, Base class Def show (self): print (' show ') def foo (self): print (Self.name) class F2 (F1): # Subclass, Derived class def __init__ (self, name): Self.name = name def bar (self): print (' Bar ') def Show (self): print (' f2.show ') obj = F2 (' Alex ') Obj.show () Obj.foo ()---------F2.showalex

Note The main points:

    • F2 is a subclass of F1

    • When creating an Obj object, he automatically calls F2 's constructor, and when he tries to invoke the Foo () method, he first looks at whether he or she is looking for a parent class. Similarly, when you call Show (), because you already have it, call your own



Look at another example, the same principle, when the subclass object calls the method, self is the sub-class object that points to, so his order is always from the subclass began to look for, can not find to go to the parent class to find

Class S1:def F1 (self): self. F2 () def F2 (self): print ("S1.f2") class S2 (S1): def-F3 (self): self. F1 () def F2 (self): print ("s2.f2") obj = S2 () obj. F3 () obj = S1 () obj.f1 ()-------------S2.f2S1.f2


Next look at Python-specific multiple inheritance, the format is simple C1 (c2,c3) means C1 inherit C2 and C3 at the same time

When a multi-inheriting class inside the call, followed by the principle of 2 points, if it is the left side of the scene, then the left to do a deep traversal to find the right side of the parent, if it is the right side of the scene, there are common ancestors, then through the left to the ancestors, and then through the right parent class until the ancestors


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/88/55/wKiom1fvQP2jqq0lAABVvzUV2Ng304.png "title=" Untitled.png "alt=" Wkiom1fvqp2jqq0laabvvzuv2ng304.png "/>


For example


Class C_2:def F2 (self): print (' C-2 ') class C_1 (c_2): Def F12 (self): print (' C-1 ') class C0 (c_2): def F2 (self): print (' C0 ') class C1 (C0): def-F1 (self): print (' C1 ') class C2 (c_1): Def-F12 (self): print (' C2 ') class C3 (C1,C2): Def f3 (self): Passobj = C3 () obj.f2 ()-----C0



Finally look at polymorphism. Python supports polymorphic primitives, such as passing parameters without specifying a type, which can be any data type, whereas a similar approach in Java or C # requires inheritance to implement the same method passing parameters of different data types.



Class F1:passclass S1 (F1): Def show (self): print (' S1.show ') class S2 (F1): Def show (self): print (' S2.show ') def func (obj): print (Obj.show ()) S1_obj = S1 () func (s1_obj) s2_obj = S2 () func (s2_obj)------------- S1.showNoneS2.showNone


This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1858275

Python Learning Notes-Object oriented (Basic)

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.