1. Definition
__metaclass__=typeclass person:def __init__ (self,name,age): Self.name=nameself.age=agedef setName (self,name): Self.name=namedef GetName (self): return self.namedef setage (self,age): Self.age=agedef getage (self): return SELF.AGEP1 =person (' C + + ', ' $ ') print p1.getname () p1.setname (' Python ') print p1.getname () print P1.name
__init__ () is a constructor
where __metaclass__=type determines the use of modern classes,python3.0 will not have to consider the problem of the new class, the definition of class:
class Person (object):d EF __init__ (self,name,age): Self.name=nameself.age=agedef setName (self,name): self.name= Namedef GetName (self): return self.namedef setage (self,age): Self.age=agedef getage (self): return Self.agep1=person (' C + + ', ' + ') print p1.getname () p1.setname (' Python ') print p1.getname () print P1.name
Obviously the first of each function argument list in the definition of a class should be self,self , which is the object itself, and self is also a flag that distinguishes between methods and object functions.
2. Access Rights
From the above case, you can see that the properties of an object can also be accessed externally.
PS: Students familiar with C + + know that it is very dangerous to expose the object's properties to the outside.
there is no private keyword in python , but a double underscore "__" is used to complete the privatization of properties, not just attributes, but also methods in the class using the same method
class Person (object):d EF __init__ (self,name,age): Self.__name=nameself.__age=agedef setName (self,name): Self.__name =namedef GetName (self): return self.__namedef setage (self,age): Self.__age=agedef getage (self): return self.__agep1= Person (' C + + ', ' $ ') print p1.getname () p1.setname (' Python ') print p1.getname () print P1.__name
We can find that using the methods in the class can continue to access the properties of the object, and accessing outside the class is a failure.
However , there is no special mechanism in Python, but a double-underlined attribute preceded by a single underscore and a class name.
Example:__name->_person__name
class Person (object):d EF __init__ (self,name,age): Self.__name=nameself.__age=agedef setName (self,name): Self.__name =namedef GetName (self): return self.__namedef setage (self,age): Self.__age=agedef getage (self): return self.__agep1= Person (' C + + ', ' $ ') print p1.getname () p1.setname (' Python ') print p1.getname () print P1._person__name
If so, you can continue to access it.
3. Inheritance
Inheritance does not introduce much, as the example above, in parentheses after the class name of the class is the parent class, when there are more than one class for the parent class, use the ', ' interval. The subclass inherits from the parent class, and the constructor uses super ( the function to construct the property method inherited by the parent class, etc.).
class Person (object):d EF __init__ (self,name,age): Self.__name=nameself.__age=agedef setName (self,name): Self.__name =namedef GetName (self): return self.__namedef setage (self,age): Self.__age=agedef getage (self): return Self.__ageclass ITer:d EF __init__ (Self,name,age,lan): Super (Iter,self) __init__ (name,age) self.lan=landef Getlan (self): Return self.landef Setlan (Self,lan): Self.lan=lanp2=iter (' Sun ', $, ' python ') print P2.getlan () print p2.getname ()
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Python Note 4] class (object oriented?) )