""" Note: Do not copy the code, please leave a message and point out """ for errors
1 import re
23'''
4 classPerson:5 name='xxx'6 age=2078 p=Person() #p is the instance object
9 print(p.name,p.age)10 p.name="yyy"11 p.gender='male'12print(p.name,p.gender,p.age)13print(Person.name,Person.age)14 #p (instance object) does not affect the content in the Person class for value modification
15 Person.age='30'16print(Person.age)17 #classPerson (class) The content in the Person class will be affected if it is worth modifying
18'''
1920 # access permission(Class attributes)21'''
22 classPerson: #The name and age in Person are public
23 name ='james'24 age=2025 #It is stipulated in python to add two underscores in front to become private
26'''
27'''
28 classPerson:29 __name ='james' #private__name
30 age =203132 def show(self): #self >>Oneself
33 print(self.__name,self.age) #Visit yourself and return normally
34353637 p=Person()38 p.show() #Need to call except show() function, otherwise the execution of show in class will be rejected
39 print(Person.__name) #Access to private attributes in the class returns abnormally
40 print(p.age)41 #The same is true when there is a private function call! Private function, element can only be used in class
42 # The commonly used method is to execute the call of the private properties in the function by calling the public function
43'''
4445 # Case: Writing personal information and establishing object access attributes
46'''
47 classPerson:48 name ='xxx'49 gender ='x'50 age =05152 p=Person()53print(p.name,p.gender,p.age)54print(Person.name,Person.gender,Person.age)5556 p.name ='A'57 p.gender ='Male'58 p.age =205960 Person.name ='B'61 Person.gender ='Female'62 Person.age =216364print(p.name,p.gender,p.age) #p instance object properties
65 print(Person.name,Person.gender,Person.age) #Person class attributes
66'''
6768 # Instance method
69'''
70 classPerson:71 age =2072 def show(self):73print(self.age)7475 p.Person()76 #The instance method has at least one parameter, usually with a variable named (self) as the parameter (name can be customized)
77 # The instance method is called through the instance object, for example: p.show()78 #If you use the class name to call, you need to pass the instance parameters manually, for example: Person.show(p)79 #The instance method is called to pass the instance object to its first parameter
80'''
81'''
82 classPerson:83 name ='james'84 age =128586 def getName(self):87return self.name
8889 def getAge(self):90return self.age
9192 p=Person()93print(p.getName(),p.getAge()) #Instance attribute object call
94 print(Person.getAge(p)) #Class name call
95'''
96'''
97 def show(self,s): #Parameter passing
98 print(self,s)99100 p.show('hi') #'hi'Value passed to s
101 Person.show(p,'hello') #hello to s'''
102103 # Class method
104'''
105 classPerson:106 age =20107 @classmethod
108 def show(cls):109print(cls.age)110111 p=Person()112'''
113 # Class method to use@classmethod to modify, and the first parameter is generally named cls (other names can be named)
114 # Class methods are usually called by the name of the class, for example: Person.show()115 #Class methods can also be called by instance methods, for example: p.show()116 #When a class method is called, the name of the class is passed to its first parameter
117'''
118 classPerson:119 __name ='james'120 __age =12121122 @classmethod
123 def show(self):124print(self)125print(self.__name,self.__age)126127 Person.show()128 p=Person()129 p.show()'''
130131 # Static method
132'''
133 definition:
134 classPerson:135 age =20136 @staticmethod
137 def show():138print(Person.age)139140 p =Person()141142 #Static variable function passed@staticmethod modification
143 # Usually use the name or instance of the class to call the static method, for example: Person.show(),p.show()144 #No parameters are passed to the function when calling a static function
145'''
146'''
147 classPerson:148 __name ='james'149 __age =12150151 @staticmethod
152 def show(): #Static without parameters
153 print(Person.__name)154155 p =Person()156 Person.show()157 p.show()158'''
159160 # Classes, instances, and static methods are different-Person
161'''
162 classPerson:163 name ='xxx'164 gender ='x'165 age =0166 #Instance method
167 def instanceShow(self):168print(self.name,self.gender,self.age)169170 @classmethod #Class method
171 def classShow(cls):172print(cls.name,cls.gender,cls.age)173174 @staticmethod #Static method
175 def staticShow():176print(Person.name,Person.gender,Person.age)177178179 p=Person()180 p.instanceShow() #Instance method call
181 Person.classShow() #Class method call
182 Person.staticShow() #Static method call
183'''
184'''
185 Instance methods are generally called with instance objects
186 Class methods and static methods are generally called by class names
187'''
188189 # Object initialization
190'''
191 Construction method (function)...>Complete function initialization
192 @ Construction method__init__(self,...)Called when the object is generated,
193 Can be used to perform some initialization operations, do not need to be displayed to call, the system executes by default
194 If the user does not define a construction method, the system will execute the default construction method by itself
195196 classPerson:197 def __init__(self,n,a):198 self.name=n
199 aelf.age=a
200201 p=Person('xxx',20)202print(p.name,p.age)203204 @Destruction method__del__(self)Called when the object is released, you can perform some operations to release resources inside
205 No need to show call
206207 classPerson:208 def __init__(self,n,a):209 self.name = n
210 self.age = a
211 def __del__(self):212print('__del__',self.name,self.age)213214 p=Person('xxx',20)215print(p.name,p.age)216''''''217classPerson:218 def __init__(self):219print("__init__",self)220 def __del__(self):221print("__del__",self)222223 p=Person()'''
224225 #__ init__Complete object initialization
226'''
227 classPerson:228 def __init__(self,n,g,a): #n,g,a Reference unit
229 self.name = n
230 self.gender = g
231 self.age = a
232233 def show(self):234print(self.name,self.gender,self.age)235236 p=Person('james','male',20) #After the p object is determined, the initialization (n, a, m) of the p object is completed
237 p.show()238'''
239 # Python stipulates: There can only be one constructor in a class
240 #...__ init__Set default parameters in
241'''
242 classPerson:243 def __init__(self,n='',g='male',a=0):244 self.name = n
245 self.gender = g
246 self.age = a
247248 def show(self):249print(self.name,self.gender,self.age)250251 a =Person('james')252 b =Person('james','fenmale')253 c =Person('james','male',20)254 a.show() #Result: james male 0255 b.show() #Result: james fenmale 0256 c.show() #Result: james male 20257# __init__If the default value is set in the parameter, the original value will be overwritten after the new content is passed.'''
258259 # Case:Date class MyDate
260'''
261 classMyDate:262 __months =[0,31,28,31,30,31,30,31,31,30,31,30,31]263 def __init__(self,y,m,d):264if(y<0):265print("Error:Invalid year")266if(m<1 or m>12):267print("Error:Invalid month")268if(y%400==0 or y%4==0 and y%100!=0):269 MyDate.__months[2]=29270else:271 MyDate.__months[2]=28272273if d<1 or d>MyDate.__months[m]:274print("Error:Invalid date")275 #After the date, month, and year are all legal, create the attributes (content) of the class
276 self.year = y
277 self.months = m
278 self.day = d
279 def show(self,end='\n'):280print("%d-%d-%d"%(self.year,self.months,self.day),end = end)281282 p =MyDate(2018,11,19)283 p.show()284'''
285286 # Class inheritance
287 # Derivation and inheritance: Student: name gender age
288'''
289 classPerson:290 def __init__(self,name,gender,age):291 self.name = name
292 self.gender = gender
293 self.age = age
294295 def show(self,end='\n'):296print(self.name,self.gender,self.age)297298classStudent(Person):299 def __init__(self,name,gender,age,major,dept):300 Person.__init__(self,name,gender,age)301 self.major = major
302 self.dept = dept
303304 def show(self):305 Person.show(self,'')306print(self.major,self.dept)307308 s =Student('james','male',20,'softwart','computer')309 s.show()310'''
311'''
312 result:
313 james male 20314 softwart computer
315'''
316 # Inherited class constructor:
317'''
318 From the definition of the Strdent class above, it can be seen that in addition to the constructor of the derived class, in addition to completing its own new addition
319 major,In addition to the initialization of the dept attribute, the Person constructor of the base class must be called, and the call must be displayed
320 Namely: Person.__init__(self,neme,gender,age)321 Directly call Person through the class name Person__init__Function, and provide the required 4 functions
322 The constructor of the base class will not be automatically called when the class is inherited, and the call must be displayed
323'''
324325 # Inheritance of attribute methods:
326'''
327 If there is an instance method in a base class, you can redefine exactly the same instance method in the inherited class.
328 For example, Person has a show method, and Student has the same show method. They will not be confused
329 We call the show of the Student class overrides the show of the Person
330 —Of course, the instance method of a base class can also not be overridden, the derived class will inherit the instance method of this base class
331- Derived classes can also add their own new instance methods
332'''
333'''
334 classPerson:335 className ='Person' #Own instance attributes
336 def __init__(self,name,gender,age):337 self.name = name
338 self.gender = gender
339 self.age = age
340341 def show(self,end='\n'):342print(self.name,self.gender,self.age)343344 @classmethod #Class method modification
345 def classClassName(cls):346print(cls.className)347348 @staticmethod #Static method decoration
349 def staticClassName():350print(Person.className)351352 def display(self):353print('display')354355classStudent(Person):356 #className ='Student'357 def __init__(self,name,gender,age,major,dept):358 Person.__init__(self,name,gender,age)359 self.major = major
360 self.dept = dept
361362 def show(self):363 Person.show(self,'')364print(self.major,self.dept)365366 s =Student('A','male',20,'software','computer')367 s.show()368print(Student.className) #If there is no Student itself, the upper level (Person) is displayed
369 Student.classClassName()370 Student.staticClassName()371372 s.display() #Instance call
373'''
374 # Result description: Student inherits the instance, static, and class (attribute) methods of Person
375 # result:
376'''
377 A male 20378 software computer
379 Person
380 Person
381 Person
382 display
383'''
384385 # Case: Year Month Day+Minutes and seconds
386387 classMyDate:388 __months =[0,31,28,31,30,31,30,31,31,30,31,30,31]389 def __init__(self,y,m,d):390if(y<0):391print("Error:Invalid year")392if(m<1 or m>12):393print("Error:Invalid month")394if(y%400==0 or y%4==0 and y%100!=0):395 MyDate.__months[2]=29396else:397 MyDate.__months[2]=28398399if d<1 or d>MyDate.__months[m]:400print("Error:Invalid date")401 #After the date, month, and year are all legal, create the attributes (content) of the class
402 self.year = y
403 self.months = m
404 self.day = d
405 def show(self,end='\n'):406print("%d-%d-%d"%(self.year,self.months,self.day),end = end)407408classMyDateTime(MyDate):409 def __init__(self,y,mo,d,h,mi,s):410 MyDate.__init__(self,y,mo,d)411if(h<0 or h>23 or mi<0 or mi>59 or s<0 or s>59):412print("Error:Invalid time")413 self.hour = h
414 self.minute = mi
415 self.second = s
416417 def show(self):418 MyDate.show(self,'\t')419print("%d:%d:%d"%(self.hour,self.minute,self.second))420421422423424 p =MyDateTime(2018,11,19,21,21,54)425 p.show()426427 #Results: 2018-11-1921:21:54
Recommended Posts