In python , the characteristics of an object are also known as attributes (attribute). The behavior it has is also called the method.
Conclusion: Object = attribute + method
In python , classify objects with the same properties and methods as one class
such as humans, animals, plants, etc., these are the concepts of the class.
A class is a template or blueprint for an object, a class is an abstraction of an object, and an object is an instantiation of a class. A class does not represent a specific thing, but an object represents something specific.
>>> class People:
... def speak (self):
... print ("hello!")
...
"' defines a people class that defines a speak method, but does not define a property because the property is not part of a class, but rather belongs to an instance of each class. That is, belongs to the object. So we can set different properties for each instance.
>>> class People: #类
... def speak (self): #方法
... print ("hello!")
...
>>>
>>> Jack = people () #创建jack实例
>>> Tom = people () #创建tom实例
>>> Import Tab #导入table键功能模块
>>> Jack. #输入jack. <tab key, you can see the following methods
jack.__class__ jack.__doc__ jack.__module__ Jack.speak ()
>>> jack.speak () #引用speak方法 hello!
>>> jack.age=39 #添加age属性
>>> jack.height=120 #添加height属性
>>> Jack. jack.__class__ jack.__module__ jack.height jack.__doc__ jack.age jack.speak ()
>>> Jack.height 120
>>> Jack.age 39
‘‘‘
#初始化对象 you create a class, you can define a specific method, named __init__ (), that runs this method whenever an instance of the class is created.
You can pass parameters to the __init__ () method so that when you create the object you can set the property to the value you want __init__ () This method will complete initialization when the object is created.
‘‘‘
>>> class PEO:
... def __init__ (self,name,age,sex):
.. self. Name = Name
.. self. Age = Age
.. self. sex = Sex
... def speak (self):
... print "My name" + self. Name
...
>>> when instantiating objects of this class:
>>> Zhangsan=peo ("Zhangsan", +, ' man ')
>>> print Zhangsan. Age 24
>>> print Zhangsan. Name Zhangsan
>>> print Zhangsan. Sex Man #----------
>>> Print Zhangsan < __main__.peo instance at 0x7fe5041ec248>
‘‘‘
To print it out, you have to use the __str__ method __str__ () This method tells Python exactly what to display when printing an object (print)
‘‘‘
#! /usr/bin/python class PEO:
def __init__ (self,name,age,sex):
Self. Name = Name
Self. Age = Age
Self. sex = Sex
def speak (self):
Print "My name" + self. Name
def __str__ (self):
Msg= ' My name is: ' +self. Name+ "," + "My Age was:" + self. Age + ', ' + ' My sex is: ' +self. Sex
Return msg
SHANGHAI=PEO (' Shanghai ', ' All ', ' man ')
' msg= ' My name is: ' +self. Name+ "," + "My Age was:" + self. Age + ', ' + ' My sex is: ' +self. Sex
Here 23 is age, but is turned into a string because self. Age defines a string if you do not escape 23, you will get an error if you want to escape in the program beforehand, you need to use STR (self. Age) '
Print Shanghai
The parameter "Self" was used many times before
A class is like a blueprint, using a class to create multiple object instances, and the Speak () method, when called, must know which object called it.
The self parameter here tells the method which object to invoke. This is called an instance reference. Zhangsan.speak () is like writing a peo.speak (Zhangsan) "
__author__ = Kayson
Python classes, objects, methods, properties