1. What is a combination
Another attribute of the object can be considered a target object or another object properties
Why use a combination? ===> reduce redundant code
the Person class: School = 'Oldboy' class Teacher (the Person): DEF the __init __ (Self, name, Age, Level, Course): the self.name name = self.age = Age self.level = Level #course lesson object is expressed the teacher teaches courses self.course = course, class student (the Person): DEF __init __ (Self, name, Age, course,): self.name = name self.age = Age # course, a course object that represents the students choose courses self. = Course Course class Course,: DEF the __init __ (Self, COURSE_NAME, course_price, course_period): the self.name = COURSE_NAME self.price = course_price self.period = course_period
If there is no Course before the class, the student and teacher data attributes in both course_name, course_price, course_period these three attributes, in order to reduce redundant code, we re-add a Course class, teachers and students can be the course to be invoked as their own property
When did it, when what is what is the relationship between inheritance and when combined with the inheritance, what with the combination of what the relationship
II. Polymorphic
What is a multi-state? Polymorphism is a variety of forms of a class of things, there are many forms of animals, such as humans, dogs, pigs
Animal class: DEF Speak (Self): Pass class Pig (Animal): DEF Speak (Self) Print ( 'hum hum') class Dog (Animal): DEF Speak (Self) Print ( 'bark Wang') class People ( Animal): DEF Speak (Self): Print ( 'Hello say') Pig Pig = () Dog Dog = () people = Pepple () pig.speak () dog.speak () people.speak () # if according to the above this method is called if the program is somewhat lengthy, we can define a function call to speak, because of the above three classes speak a common method, DEF Animal_speak (obj) obj.speak () Animal_speak (Pig) Animal_speak (Dog) Animal_sspeak (people)
Now if there is such a situation, as humans, pigs, dogs barking representation is not the same, how to do it this way, we can borrow the programmer agreement commonly known as the module abc, to achieve unified look at the following example?:
# To achieve unified interface with abc, to constrain the code, as long as I define speak this method, you just inherit me, then you have to speak with this method, if the error will not be accomplished by such constraints polymorphism, as long as this kind of approach can be called polymorphism in fact, I used to constrain what the subclass method Import abc class Animal (the metaclass that = abc.ABCMeta): @ abc.abstractmethod DEF Speak (Self ): Pass class Pig (Animal): DEF xxx (Self) Print ( 'hum hum') class Dog (Animal): DEF yyy (Self) Print ( 'bark Wang') class People (Animal): DEF ZZZ (Self ): Print ( 'Hello say')
Advocating duck typing in Python (as long as it walks like a duck, (object has a binding method), then you are a duck), it has nothing to bind me subclass method should be how to write, are man-made provisions, no provisions like how to write on how to write (duck type does not require inheritance)
3. Package
What is the package? Package itself from meaning to understand, if the package is brought a sack, the sack with puppies, kittens, and then seal the hole in the sack
How to hide, then put things into bag, hidden, not external access
How to hide in code? Hidden inside can only access the property after conceal and hide methods, not external access
Hidden attributes: variable name to hide by __
Concealment methods: the method name to hide by __
# The name hidden class the Person: DEF the __init __ (Self, name, Age) Self .__ name = name self.age = Age
DEF get_name (Self) # refers to the internal
return Self .__ name
P = the Person ( 'panshao', 18 is ) Print (p.age) # 18 Print (p.name) # throws error refers to the external
print (p.get_name ()) # normal print out the name
View property by people .__ dict__ can see the following results
Through the above chart we can clearly see the hidden essence is hidden only by deformation properties, we try to print under people._People__name, the results really got the name.
Property package is to prevent others veered, to ensure safety, the encapsulation method is the complexity of the isolation
__ variable name when the deformation properties of it? As long as within the class to __ variable name to name variables, will be hidden, deformed, placed on the outside is not hidden
class Person: def __init__(self, name, weight, height) self.name = name self.weight = weight self.height = height def bmi(self) return self.weight/(self.height**2) p = Person('bgon', 80, 1.78) print(p.bmi())
We can get the value of bmi by p.bmi (), but carefully think about, because a person's weight is change, bmi will surely follow the changes, and where you can use @property decorator, he can be packaged into data method property, at the time of the call bmi without brackets, but this is still a mistake, that p.bmi can not be re-modified externally
property of the setter and Deleter class the Person: DEF __init __ (Self, name, height, weight): Self .__ name = name Self .__ height = height Self .__ weight = weight the @Property DEF name (Self): return '[My name is :% S] 'Self% .__ name # property decorated with a method name .setter @ name.setter DEF name (Self, NEW_NAME): # Not the isinstance IF (NEW_NAME, STR): IF type (NEW_NAME) iS Not STR: the raise Exception ( 'not change') IF new_name.startswith ( 'SB'): the raise Exception ( 'can not start SB') Self .__ NEW_NAME name = # property decorated with the method name .deleter @ name.deleter name DEF (Self): # Raise Exception ( 'can not be deleted') Print ( 'deleted successfully') # del Self .__ name the p-the Person = ( 'LQZ', 1.82,70) # Print (p.name) # p.name = 'pppp' # the p- = .name 'XXX' # not change, a direct throw exception # p.name = 999 # = p.name 'sb_nick' both methods are powerless # # Print (p.name) del p.name Print (P .name)
IV. Scalable package
Designers # class class Room: DEF __init __ (Self, name, owner, width, length, High): self.name = name self.owner owner = Self .__ width = width Self .__ length = length Self .__ High = High DEF tell_area (self): # provide external interfaces, hiding the internal implementation details at this time we want to ask is the area return width * Self Self .__ .__ length # user >>> r1 = Room ( 'bedroom', 'egon ', 20,20,20) >>> r1.tell_area () # the user interface calls tell_area 400 user # class designer, and easily expand the functionality, and the like all without changing their code class Room: the __init __ DEF (Self, name, owner, width, length, High): the self.name name = self.owner owner = Self .__ width = width Self .__ length = length Self .__ = High High DEF tell_area (Self): # provide external interfaces, hiding the internal implementation, this time we want to ask is the volume, internal logic changed, only the following line needs repair can achieve very short answer, and external calls imperceptible, still use this method, but the function has changed return width * Self Self .__ .__ .__ High Self length * # for still using tell_area human interface, such is no need to change your code, you can use new features >>> r1.tell_area () 8000