There are two solutions to the problem of code redundancy between classes:
1. Inheritance: Describes the relationship between classes and what is what
2. Combination: describes the relationship between classes, what kind of relationship
An object produced by a class that has a property whose value comes from an object of another class
class Date: def __init__(self,year,mon,day): self.year = year self.mon = my self.day = day def tell_birth(self): print ( ' Birth year month day <%s-%s-%s> ' % (self.year, self.mon, self.day)) class OldboyPeople: school = 'oldboy' def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex class OldboyTeacher(OldboyPeople): def __init__(self,name,age,sex,level,salary): super().__init__(name,age,sex) self.level=level self.salary=salary def change_score(self): print('teacher %s is changing score' %self.name) class Oldboystudent(OldboyPeople): def __init__(self,name,age,sex,course,): super().__init__(name,age,sex,) self.course=course def choose(self): print('student %s choose course' %self.name) tea1 = OldboyTeacher ( ' egon ' , 18, ' male ' , 9,3.1 ) date_obj=Date(2000,1,1) # date_obj.tell_birth() tea1.birth=date_obj # print(tea1.birth) # tea1.birth.tell_birth() # tea1.change_score() stu1=Oldboystudent('张三',16,'male','linux') stu1.birth=Date(2002,3,3) stu1.birth.tell_birth()
package
1. What is encapsulation?
Packing is storing a bunch of attributes, and the concept of packing is to hide these attributes
Emphasis: Encapsulation is literally equivalent to hiding, but in fact, encapsulation is definitely not simply hiding
2. Why use packaging
just to hide properties
3. How to use packaging
#How to hide an attribute, just add __ at the beginning of the attribute (be careful not to add __ at the end) #Note : # 1. In fact, this kind of hiding is just a grammatical deformation, not external to internal # is an attribute name Add __ at the beginning (be careful not to add __ at the end), the attribute name will be uniformly deformed in the class definition stage: _ own class name __ attribute name class Foo: __x=1111 #_Foo__x=1111 def __init__(self,y): self. __y =y # self._Foo__y=y def __f1(self): #_Foo__f1 print('Foo.f1') def get_y(self): print(self.__y) # print(self._Foo__y) obj=Foo(22222) # print(obj.x) # print(obj.__x) # obj.__f1() # print(obj.y) # print(obj.__y) # print(Foo.__dict__) # print(obj._Foo__x) # print(obj._Foo__y) # obj._Foo__f1() # obj.get_y() # 2. This grammatical deformation occurs only once in the class definition stage. After the class is defined, the newly added attributes at the beginning of __ have no deformation effect. # Foo.__aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=1 # print(Foo.__dict__) # obj.__bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=2 # print(obj.__dict__) # 3. If the parent class does not want the subclass to override its own method, you can add __ before the method name class Foo: def __f1 (self): # _Foo__f1 print ( ' Foo.f1 ' ) def f2(self): print('Foo.f2') self.__f1() #obj._Foo__f1() class Bar(Foo): def __f1(self): #_Bar__f1 print("Bar.f1") obj = Bar() obj.f2() #The ultimate meaning of encapsulation: clear distinction between inside and outside, hidden from the outside, open to the inside