First, the package
1.1 package definition
In programming, the package is an abstraction of concrete objects, ie some parts hidden, look in less than an external program, which means other programs can not be called.
To understand the package, inseparable from the "privatization" is the class or some property function is limited to a certain area, the outside can not be called.
1.2 Package usefulness
The main reason is to encapsulate data: protection of privacy (do not want others to know what the package together)
The main reason is the packaging method: isolation Complexity
Note: In the programming language, provide external interfaces (interface understandable to an entrance), it is a function, called interface function, which is the interface concept is also not the same as the aggregate interface represents a set of interface functions.
Two levels of encapsulation 1.3
(1) sack is based, which itself is a package
Internal (2) defined in the class private, use only the class, can not access external
class People: _star = ' Earth ' # single underlined hidden DEF the __init__ (Self, name, ID, Age, the salary): the self.name = name self.id = ID self.age = Age self.salary = the salary DEF get_id (Self): Print ( ' [S% is the ID number S%] ' % (the self.name, self.id)) P1 = People ( ' CZD ' , ' 62,012,393,208 ' , 18,100000 ) print(p1._star) class People: __star='earth' #双下划綫 def __init__(self,name,id,age,salary): self.name=name self.id=id self.age=age self.salary=salary def get_id(self): print('[%s的身份证号是%s]'%(self.name,self.id)) p1=People('czd','62012393208', 18,100000 ) # Print (p1 .__ Star) # error Print (p1._People__star)
This automatic deformation characteristics:
1, __star class defined for internal use only, such as items p1 .__ star, it is the result of deformation by reference.
2, this deformation fact, it is against external deformation is not accessible to the outside through __star name.
3, in the subclass definition __star not covered in the parent class definition __star, as a subclass morphed into: _ subclass name __star, while the parent class morphed into: _ Parent
Class name __star, double the decline that is the beginning of the line to inherit property in the subclass, the subclass is not covered.
Note: For this level package (hidden), we need to define a class function (interface function) in its internal access hidden attributes, then
You can use the external
Second, polymorphism
State concept than 2.1
多态的特性是调用不同的子类将会产生不同的行为,而无需明确知道这个子类实际上是什么。
Polymorphism is actually attached to inherit two meanings: "change" and "extended" itself means you must have a mechanism to choose to change / extend over version, the implementation details polymorphism essentially inherited ;
Method calls the base class of the subclass is instantiated, w1.turn_ice () is called polymorphism;
class H2O: def __init__(self,name,temperature): self.name=name self.temperature=temperature def turn_ice(self): if self.temperature<0: print('[%s]温度太低结冰了'%self.name) elif self.temperature>0 and self.temperature<100: print('[%s]液化成水'%self.name) elif self.temperature>100: Print ( ' [S%] of water vapor becomes too hot ' % the self.name) class Water (H2O): Pass class Ice (H2O): Pass class the Steam (H2O): Pass W1 = Water ( ' water ' , 25 ) I1 = ice ( ' ice ' , -20 ) S1 = the steam ( ' steam ' , 200 is ) # w1.turn_ice () # object during execution of different calls in the same manner # i1.turn_ice () # s1.turn_ice ( ) # or define an interface for calling the above def func(obj): obj.turn_ice() func(w1) #---->w1.turn_ice() func(i1) func(s1)
Third, reflection
3.1 Introduction reflected
Reflection simple point -> is the use of a string to an object (block) in operation (Looking / check / set / delete) members.
- hasattr(object,"name")
- getattr(object,"name",default=None)
- setattr(x,y,v)
- delattr(x,y)
E.g:
class BlackMedium: feture = ' Ugly ' DEF the __init__ (Self, name, addr): the self.name = name self.addr = addr DEF sell_house (Self): Print ( ' [% S] are selling the house, before buying it sucker ' % the self.name) DEF rent_house (Self): Print ( ' [% S] is rent, rent it was sucker ' % the self.name) B1 = BlackMedium ( ' Wan opposite to ' , ' open days Park ' ) Print(hasattr(b1,'name')) print(hasattr(b1,'sell_house')) #显示True print(hasattr(b1,"sell_houseaaa"))#显示False print(getattr(b1,'name')) print(getattr(b1,'rent_house')) func=getattr(b1,'rent_house') func() # print(getattr(b1,'rent_houseaaa')) #没有则报错 print(getattr(b1,'rent_houseaaa ' , ' do not have this property ' )) # data attribute setattr (B1, ' SB ' , ' True ' ) # corresponds = True b1.sb setattr (B1, ' 123 ' , ' CZD ' ) setattr (B1, ' name ' , ' Fuzhou ' ) # function attributes setattr (B1, ' FUNC ' , the lambda X: X +. 1 ) setattr (B1, 'func1 ',lambda self:self.name+'sb') print(b1.__dict__) print(b1.func) print(b1.func(1)) print(b1.func1(b1)) delattr(b1,'sb') #相当于del b1.sb print(b1.__dict__)
3.2 Benefits of reflection
(1) a benefit: pluggable mechanism to achieve
There are two programmers, a czd, one egon, lili when writing programs need to use egon written class, but egon honeymoon went to talk to his girlfriend, has not yet completed his writing class, lili thought reflection using the reflection czd can continue to complete their own code, etc. egon honeymoon come back later to finish the class definition and to achieve czd want.
In sum benefit is reflected, can be defined first interface, the interface will really only be executed after the completion of this plug and play, this is actually a 'late binding', what does that mean? That you can advance to the main logic written (only defines interfaces), then go to achieve the latter function interface
class the FtpClient: ' FTP client, but it is also for realizing specific functions ' DEF the __init__ (Self, addr): Print ( ' Connect to Server [% S] ' % addr) self.addr = addr
# From the FtpClient Module1 Import F1 = the FtpClient ( ' 192.168.1.1 ' ) IF the hasattr (F1, ' GET ' ): func_get = getattr (F1, ' GET ' ) func_get () the else : Print ( ' ----> absent this method ' ) Print ( " handle other logic ' )
(2) two benefits: Dynamic import module (this module based on the reflection member)