Introduction to Object-oriented technology
-
class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
-
Class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
-
data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.
-
method Overrides: if the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
-
instance variable: A variable defined in a method that acts only on the class of the current instance.
-
inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).
-
instantiation: Creates an instance of a class, the concrete object of the class.
-
method: a function defined in a class.
-
object: An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
Use the class statement to create a new class, such as the following instance: Class ClassName (object): Def __init__ (Self,name,age,sex,zhiye): Self.name=name self . age=age self.sex=sex Self.zhiye=zhiye def Tiaochao (self): print ("lz:%s tiaozhao Zuo%s"% (Self.nam E,self.zhiye) self.zhiye= "python" #创建实例对象r1 =classname ("JWH", "Male", "Daza") #访问属性print (R1.name,r1.age,r1.sex, R1.zhiye) >>JWH Male Daza
You can access properties by using the following functions:
-
GetAttr (obj, name[, default]): Accesses the properties of the object.
-
Hasattr (obj,name): Checks if a property exists.
-
SetAttr (Obj,name,value): Sets a property. If the property does not exist, a new property is created.
-
Delattr (obj, name): Deletes the attribute.
Python built-in class properties
-
__DICT__: Properties of the Class (contains a dictionary, consisting of the data properties of the Class)
-
__DOC__: Document string for Class
-
__NAME__: Class name
-
__MODULE__: The module where the class definition resides (the full name of the class is ' __main__.classname ', if the class is in an import module mymod, then classname.__module__ equals Mymod)
-
__BASES__: All parent classes of a class make up elements (containing a tuple of all the parent classes)
The python built-in class property invocation instance is as follows:
ClassName (): (, Name,age,sex,zhiye):. Name=name. age=age sex=sex. Zhiye=zhiye Tiaochao () : (% (. Name,.zhiye)). zhiye= (classname.) (classname.) (classname.) (classname.) (classname.)
Execute the above code to output the result as follows:
__doc__classname__main__ (<class ' object ') {' __dict__ ': <attribute ' __dict__ ' of ' classname ' objects>, ' _ _weakref__ ': <attribute ' __weakref__ ' of ' classname ' objects>, ' __module__ ': ' __main__ ', ' __doc__ ': ' __doc__ ', ' Tiaochao ': <function Classname.tiaochao at 0x00585d20>, ' __init__ ': <function classname.__init__ at 0x00585108 }
Python Object Destruction (garbage collection)
Like the Java language, Python uses a simple technique of reference counting to track objects in memory.
Inside Python is a record of how many references each object in use has.
An internal tracking variable, called a reference counter.
When the object is created, a reference count is created, and when the object is no longer needed, that is, the reference count of the object becomes 0 o'clock, and it is garbage collected. However, recycling is not "immediate", and the interpreter uses the garbage object to reclaim the memory space at the appropriate time.
a = 40 # Creating Objects <40>b = a # increase the count of reference, <40> c = [b] # increase the count of reference . <40> del a # Reduce the count of references <40> b = 100 # reduce references <40> Count c[0] = -1 # reduce the count of references <40>
The garbage collection mechanism not only targets objects with a reference count of 0, but can also handle circular references. Circular references refer to two of objects referencing each other, but no other variables refer to them. In this case, using only the reference count is not enough. The Python garbage collector is actually a reference counter and a cyclic garbage collector. As a supplement to the reference count, the garbage collector also pays attention to objects that are allocated a large amount (and those that are not destroyed by reference counting). In this case, the interpreter pauses to attempt to clean up all unreferenced loops.
destructor __del__, __del__ is called when the object is destroyed, and the __del__ method runs when the object is no longer being used:
#!/usr/bin/python# -*- coding: utf-8 -*-class point: def __ init__ ( self, x=0, y=0): self.x = x self.y = y def __del__ (self): class_name = self.__class__.__name__ print class _name, "Destroy" Pt1 = point () Pt2 = pt1pt3 = pt1print id (PT1), id (PT2), id (PT3) # Print Object Iddel pt1del pt2del pt3
The results of the above example operation are as follows:
3083401324 3083401324 3083401324Point Destruction
Python Object-oriented 1