Classes and objects
process-oriented and object-oriented programming
Process-oriented programming: Functional programming, C-programs, etc.
Object-Oriented Programming: C++,java, Python, etc.
classes and objects: Two important concepts in object-oriented
Class: An abstraction of a thing, such as a car model (a description of a class of things, attributes, and methods)
Object: Is an instance of a class, for example: QQ sedan, bus
Example Description:
Car models can abstract the characteristics and behavior of automobiles, and then can be instantiated into a real car to reflect
Definition of Python class:
definition of Python class:
Use the class keyword to define a class and capitalize the first letter of the class name ;
When a programmer needs to create a type that cannot be represented by a simple type, it needs to create a class;
Classes combine the required variables and functions , which are called " encapsulation ";
the structure of the Python class:
>>>class class Name:
... Member variables
... member functions
...
class Creation: an example
#!/usr/bin/python#coding:utf8class Ren: #class关键字, category initial Capitalization name = "Person" #属性 high = "one person tall" #属性 wight = "One person heavy" #属性 def run (self): #成员函数定义方法; print "Run"
#注意类当中定义成员函数时和外边定义函数时有区别, there must be a formal parameter to pass the properties and methods in the class, but it is not necessary to call the arguments.
object is created:
The process of creating an object is called instantiation, and when an object is created, it contains three aspects of the attribute: the handle, property, and method of the object.
Handles are used to distinguish between different objects;
The properties and methods of an object correspond to member variables and member functions in a class
#!/usr/bin/python#coding:utf8class ren: name = ' People ' high = ' one person high ' wight = ' one person heavy ' def run (self): #self Express yourself in category print ' Running ' zhangsan = ren () instantiate an object in the category #从Ren print zhangsan
[[email protected] class]# python class.py<__main__. ren instance at 0x7ff7f80110e0> #instance Instance (object) in the instance # column #对象zhangsan具有属性和方法, the property of Zhangsan is the member variable of Ren in the category, by the function in the category
#属性的访问 (through the object.)
... print zhangsan.high[[email protected] class]# python class.py one person high
#对象属性的重新赋值
... zhangsan.name = "Zhang San" print Zhangsan.name[[email protected] class]# python class.py Zhang San
#注意: An object is instantiated from a category, has a class of properties and methods, and an object is an individual.
Zhangsan.money = "10000" Print Zhangsan.money[[email protected] class]# python class.py 10000
The architecture of the Python object:
-Classic objects: The interior consists of properties and methods, which can be data types or function types.
This property is a method if the property is a function.
-New Object- define new object, must inherit object method Class_name (object)
new objects provide support for class methods and static methods
... class Ren (object):
Properties of the class:
Classes are composed of properties and methods, the properties of the class are the encapsulation of the data, and the method of the class is the encapsulation of the behavior of the class. The properties of a class are divided into private and public properties by scope of use, and the class's property scope depends on the name of the property
public has attributes: the so-called shared attribute is the Span style= "Color:rgb (255,0,0);" The properties of the in class and out of class
#!/usr/bin/python#coding:utf8class ren (object): name = ' People ' high = ' one person high ' wight = ' One person heavy ' def run (self): print self.name #调用内部属性, self stands for itself, Accessing yourself, invoking the class's properties print ' running ' if __name__ == ' __main__ ': zhangsan = ren () zhangsan.name = "Zhang San" print zhangsan.name #外部访问 zhangsan.run () print ren.name # accessing properties by class name
Private Property: cannot be called by a function other than the class
#!/usr/bin/python#coding:utf8class ren (object): name = ' People ' high = ' one person high ' wight = ' one person heavy ' __ money = ' def run (self): print self.name print ' Running ' if __name__ == ' __main__ ': zhangsan = ren () zhangsan.name = "Zhang San" print zhangsan._ _money[[email protected] class]# python class.py traceback (most recent call last): file "class.py", line 15, in <module> print zhangsan.__moneyAttributeError: ' Ren ' object has no attribute ' __money ' #无法调用
#!/usr/bin/python#coding:utf8class ren (object): name = ' People ' high = ' one person high ' wight = ' One person heavy ' __money = ' def run ' (self): print self.name print ' Running ' def say (self): #内部方法中调用 print self.__moneyif __name__ == ' __main__ ': zhangsan = ren () zhangsan.name = "Zhang San" print Zhangsan.say () #通过内部调用方法显示 for data encapsulation, hiding
(can be accessed by Instance._classname_attribute, but only for the mode program)
Print Zhangsan._ren__money
Definition: A member variable that starts with a double underscore is a private property, otherwise it is a public property
built- in properties: A system that is added by default when defining a class, consisting of two underscores before and after a __dict__,__module__
... print ren.__dict__ [[email protected] class]# python class.py people ############################################## ####{' high ': ' \xe4\xb8\x80\xe4\xba\xba\xe9\xab\x98 ', ' __module__ ': ' __main__ ', ' Run ': <function run at 0x7f9ebdb60230>, ' name ': ' \xe4\xba\xba ', ' __dict__ ': <attribute ' __dict__ ' of ' Ren ' objects>, ' __weakref__ ': & Lt;attribute ' __weakref__ ' of ' Ren ' objects>, ' __doc__ ': None, ' wight ': ' \xe4\xb8\x80\xe4\xba\xba\xe9\x87\x8d ', ' say ': <function say at 0x7f9ebdb608c0>, ' _ren__money ': ' 1000 '}
This article from "thinking More than technology" blog, declined reprint!
Python object-oriented