Python 31st days ----- class encapsulation, inheritance, polymorphism..., python 31st days
Encapsulation
Encapsulation is better understood. Encapsulation is one of the characteristics of object-oriented, and is the main feature of object and class concepts.
Encapsulation refers to encapsulating objective objects into abstract classes. classes can only perform trusted class or object operations on their own data and methods to hide untrusted information.
1 class dog (object): 2 nationality = 'ch' # public attribute 3 def _ init _ (self, name, food, leven): # constructor, method, initialization Method 4 self. name = name # instance pointer, pointing to attribute object member 5 self. food = food 6 self. leven = leven 7 self. _ haot = 'hhh' # The preceding Double underline is defined as the private attribute 8 9 def get_hoat (self): # The definition method provides the interface 10 return self for private attributes. _ haot11 def say (self): # All methods in the class are public Methods 12 print ('hello, my name is ', self. name) 13 def eat (self, foods): 14 print ("my food is % s, but my eat % s" % (self. food, foods) 15 def leve (self): 16 print ("my leve is", self. leven) 17 18 def _ del _ (self): 19 print ("deleting... ") 20 21 22 d = dog (" liili ", 'Gl '," 5 ") 23 d. say () 24 d. eat ('kkk ') 25 d. leve () 26 print (d. get_hoat () # use a universal interface to access private attributes 27 28 print (d. _ dog _ haot) # forcibly access the private attribute 29 print (d. nationality) 30 31 dog. nationality = 'chan' 32 print (d. nationality) 33 d. nationality = 'us' 34 print (d. nationality)
View Code
Inheritance
Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class.
The new class created by inheritance is called a subclass or a derived class ".
The inherited class is called "base class", "parent class", or "super class ".
1 class studen (object): # define class student base class 2 def _ init _ (self, name, age, clas): # name, age, class 3 self. name = name 4 self. age = age 5 self. clas = clas 6 def talk (self): 7 print ('% stalk one ..... '% self. name) 8 def walk (self): 9 print ('% s walk .... '% self. name) 10 def info_user (self): 11 print ('name is % s, age is % s, clas is % s' % (self. name, self. age, self. clas) 12 13 class clas_one (studen): # inherit from studen14 def _ init _ (self, name, age, clas, score): # refactoring constructor 15 # studen. _ init _ (self, name, age, clas) # inherit first, and then refactor 16 super (clas_one, self ). _ init _ (name, age, clas) # New Class 17 self. score = score # Add new object member 18 def talk (self): # rewrite method 19 print ('is new talk, % s' % self. name) 20 def score_info (self): # Add subclass Method 21 print (self. score, 'scores ') 22 23 p = clas_one ('student 1', 36, 'three shifts a year', 178) 24 p. talk () 25 p. score_info ()
View Code
Polymorphism
1 class Animal (object): 2 def _ init _ (self, name): 3 self. name = name 4 def talk (self): 5 raise NotImplementedError ('error prompted ') 6 7 8 class c (Animal): # inherit Animal 9 def talk (self ): 10 print ('% s 1111' % self. name) 11 12 class d (Animal): # inherit Animal13 def talk (self): 14 print ('% s 2222' % self. name) 15 16 17 def talk_all (obj): # use a function to simulate polymorphism 18 obj. talk () 19 20 c1 = c ('modem') 21 d1 = d ("dog") 22 23 talk_all (c1) 24 talk_all (d1)
View Code