Python Notes Summary week8

Source: Internet
Author: User

Object-oriented section II highlights:

1. Encapsulation, objects nested within objects

2. Pickle,load, remember, you must first import the relevant classes

Review the previous section:

Object-oriented three major features:

Encapsulation, inheritance, polymorphism (multiple types, multiple forms)

For polymorphic, the function of any parameter has the following characteristics:

def func (ARG):     Print (ARG)func (1)func ("Alex") func ([11,22,33])

About the arg parameter:

class A:     Pass class B (A):     Pass class C (A):     Pass # arg parameter, must be a type A or a subclass type def func (A arg):     Print (ARG) # obj = B () # obj = C ()obj = A () func (obj)

Class members in Object-oriented

Fields: Static fields, normal fields

P.S: Static field code is loaded when it is created

class Foo:     # field (static field)    CC = 123        def__init__(self):        # field (normal field)        "  Alex"        def  Show (self):        print(self.name)

classProvince:country=" China"    def __init__(self,name): Self.name=name#in general, you access your own fields#Rules:#Normal fields can only be accessed with objects#static fields are accessed with classes (as a last resort with object access)HN = Province ("Henan") HB= Province ("Hebei")Print(Hn.name)#HenanPrint(Province.country)# ChinaPrint(Hn.country)# China

Method:

All methods belong to the class

1. Common method, at least one self, object execution

2. Static methods, arbitrary parameters, class execution (object execution)

3. Class method, at least one class, class execution (object execution)

classProvince:country=" China"    def __init__(self,name): Self.name=name#common method, use object to invoke execution (method belongs to Class)    defShow (self):Print(123) @staticmethoddefF1 (CLA,ARG1,ARG2):Print(ARG1,ARG2)#static method, executed with the class invocation. (You can write a method as a static method when the value inside the object does not need to be encapsulated within the method)@classmethoddefF2 (CLS):#classCls#class name, () Create object        #CLS ()        Print(CLS) obj= Province ("Henan") obj.show ()#123PROVINCE.F1 (province,1,2)#1 2PROVINCE.F2 ()#<class ' __main__.province ' >
classPager:def __init__(self,all_count): Self.all_count=All_count @propertydefAll_pager (self): A1,A2= Divmod (self.all_count,10)        ifA2 = =0:returnA1Else:            returnA1 +1@all_pager. SetterdefAll_pager (self,value):Print(value) @all_pager. deleterdefAll_pager (self):Print("del All_pager") P= Pager (101)Print(P.all_count)#field, 101#result = P.all_pager () #方法RET =P.all_pagerPrint(ret)# OneP.all_pager = 111#111delP.all_pager#del All_pager

Property:

A nondescript thing.

A form of writing with a method, with access to a field

classfoo:__CC="123"    def __init__(self,name): Self.name=namedefF1 (self):Print(self.name) obj= Foo ('Alex')Print(Obj.name)#AlexOBJ.F1 ()#Alex

classFoo:__CC="123"    def __init__(self,name): Self.name= Name#define a private normal field    defF1 (self):Print(self.name) @staticmethoddeff3 ():Print(Foo.)__CC)#print (FOO.__CC)obj = Foo ("DDD") obj.f3 ()#123FOO.F3 ()#123

Private Common method

classFoo:__CC="123"    def __init__(self,name): Self.__name= Name#define a private normal field    defF1 (self):Print(self.)__name)classBar (Foo):defF2 (self):Print(self.)__name) obj= Bar ("Alex") obj.f1 ()#Alex #通过调用类中的公有方法来执行私有方法

private static method

class Foo:     def __init__ (self,name): Self        . __name = name  # defines a private normal field     @staticmethod    def F2 (num):         Print (num)    @staticmethod    def  f3 (num):        print(num) FOO.F3 (Ten)  #

classFoo:#Construction Method    def __init__(self,name,age): Self.name=name Self.age= Age#destructor Method    def __del__(self):Pass    def __call__(self):Print("Pager")    def __str__(self):return("%s-%d"%(self.name,self.age))" "obj = Foo () obj () #对象 () Execution Call#foo () ()" "obj1= Foo ('Alex', 74) Obj2= Foo ('Eric', 93)Print(OBJ1,OBJ2)#alex-74 eric-93ret=Str (OBJ1)Print(ret)#alex-74#gets the data encapsulated in the objectret = obj1.__dict__Print(ret)#{' name ': ' Alex ', ' age ':

Python Notes Summary week8

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.